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{