From dbef7c5713ad1f37e83c1e8951e9ae68079440ca Mon Sep 17 00:00:00 2001 From: Matthew Sackman Date: Thu, 10 Oct 2024 14:39:00 +0100 Subject: [PATCH] cue: Keep Vertex if returned from conversion of Go to CUE By keeping the Vertex, we maintain the vertex's internal state, such as its Structs fields, which is essential for being able to correct sort fields for output. Signed-off-by: Matthew Sackman Change-Id: Ie9faa925dce7fc5bb1fb2a4f68b239b751763308 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1202451 Reviewed-by: Marcel van Lohuizen TryBot-Result: CUEcueckoo Unity-Result: CUE porcuepine --- cue/context.go | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/cue/context.go b/cue/context.go index c0c157e77dd..53e1caf1ac4 100644 --- a/cue/context.go +++ b/cue/context.go @@ -369,8 +369,13 @@ func (c *Context) Encode(x interface{}, option ...EncodeOption) Value { ctx := c.ctx() // TODO: is true the right default? expr := convert.GoValueToValue(ctx, x, options.nilIsTop) - n := &adt.Vertex{} - n.AddConjunct(adt.MakeRootConjunct(nil, expr)) + var n *adt.Vertex + if v, ok := expr.(*adt.Vertex); ok { + n = v + } else { + n = &adt.Vertex{} + n.AddConjunct(adt.MakeRootConjunct(nil, expr)) + } n.Finalize(ctx) return c.make(n) } @@ -390,8 +395,13 @@ func (c *Context) EncodeType(x interface{}, option ...EncodeOption) Value { if err != nil { return c.makeError(err) } - n := &adt.Vertex{} - n.AddConjunct(adt.MakeRootConjunct(nil, expr)) + var n *adt.Vertex + if v, ok := expr.(*adt.Vertex); ok { + n = v + } else { + n = &adt.Vertex{} + n.AddConjunct(adt.MakeRootConjunct(nil, expr)) + } n.Finalize(ctx) return c.make(n) }