From 96d9f6faddea24576cd17a3cc79c910b976ed7d4 Mon Sep 17 00:00:00 2001 From: Marcel van Lohuizen Date: Wed, 12 Feb 2025 14:05:38 +0100 Subject: [PATCH] internal/value: support more types and rename ConvertToContext It is always tricky to remember how to derive the various contexts from one another. And because this would introduce cyclic dependencies, it is not generally possible to add methods to the types. Finally, exposing methods would also expose internal types on public ones. To make this more convenient, we allow more types in ConvertToContext and rename it to Context, as it is not always a conversion. This also gets rid of Impl. Signed-off-by: Marcel van Lohuizen Change-Id: I2f39034887fcafe7267dc30223c00bad7145345c Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1208686 Reviewed-by: Matthew Sackman TryBot-Result: CUEcueckoo Unity-Result: CUE porcuepine --- encoding/gocode/gocodec/codec.go | 2 +- internal/core/adt/context.go | 5 ----- internal/value/value.go | 21 ++++++++++++++++----- pkg/list/list.go | 3 +-- 4 files changed, 18 insertions(+), 13 deletions(-) diff --git a/encoding/gocode/gocodec/codec.go b/encoding/gocode/gocodec/codec.go index b596889af..3a9354670 100644 --- a/encoding/gocode/gocodec/codec.go +++ b/encoding/gocode/gocodec/codec.go @@ -47,7 +47,7 @@ type Codec struct { // // Note: calling this with a *cue.Runtime value is deprecated. func New[Ctx *cue.Runtime | *cue.Context](ctx Ctx, c *Config) *Codec { - return &Codec{runtime: value.ConvertToContext(ctx)} + return &Codec{runtime: value.Context(ctx)} } // ExtractType extracts a CUE value from a Go type. diff --git a/internal/core/adt/context.go b/internal/core/adt/context.go index 31f173abd..50d61e86b 100644 --- a/internal/core/adt/context.go +++ b/internal/core/adt/context.go @@ -202,11 +202,6 @@ func (n *nodeContext) skipNonMonotonicChecks() bool { return n.ctx.inDisjunct > 0 } -// Impl is for internal use only. This will go. -func (c *OpContext) Impl() Runtime { - return c.Runtime -} - func (c *OpContext) Pos() token.Pos { if c.src == nil { return token.NoPos diff --git a/internal/value/value.go b/internal/value/value.go index 4757b32e9..54516c4f3 100644 --- a/internal/value/value.go +++ b/internal/value/value.go @@ -27,11 +27,22 @@ import ( "cuelang.org/go/internal/types" ) -func ConvertToContext[Ctx *cue.Runtime | *cue.Context](ctx Ctx) *cue.Context { - if ctx, ok := any(ctx).(*cue.Runtime); ok { - (*runtime.Runtime)(ctx).Init() +// Context returns the cue.Context of the given argument. +func Context[Ctx *cue.Runtime | *cue.Context | cue.Value | *adt.OpContext](ctx Ctx) *cue.Context { + switch x := any(ctx).(type) { + case *cue.Runtime: + (*runtime.Runtime)(x).Init() + return (*cue.Context)(x) + case *cue.Context: + return x + case cue.Value: + r, _ := ToInternal(x) + return (*cue.Context)(r) + case *adt.OpContext: + r := x.Runtime.(*runtime.Runtime) + return (*cue.Context)(r) } - return (*cue.Context)(ctx) + panic("unreachable") } func ToInternal(v cue.Value) (*runtime.Runtime, *adt.Vertex) { @@ -42,7 +53,7 @@ func ToInternal(v cue.Value) (*runtime.Runtime, *adt.Vertex) { // Make wraps cue.MakeValue. func Make(ctx *adt.OpContext, v adt.Value) cue.Value { - return (*cue.Context)(ctx.Impl().(*runtime.Runtime)).Encode(v) + return Context(ctx).Encode(v) } // UnifyBuiltin returns the given Value unified with the given builtin template. diff --git a/pkg/list/list.go b/pkg/list/list.go index 2dd678c75..4546fb637 100644 --- a/pkg/list/list.go +++ b/pkg/list/list.go @@ -339,8 +339,7 @@ func MatchN(list []cue.Value, n pkg.Schema, matchValue pkg.Schema) (bool, error) } } - r, _ := value.ToInternal(n) - ctx := (*cue.Context)(r) + ctx := value.Context(n) if err := n.Unify(ctx.Encode(nmatch)).Err(); err != nil { return false, pkg.ValidationError{B: &adt.Bottom{ -- 2.51.2