Skip to content

Commit 8f796bf

Browse files
committed
cue: allow top level JSON marshalers in EncodeType
Also add some tests for `EncodeType` as it wasn't called at all in the entire code base. I have verified that the test `TestEncodeType/CUEValue#2` fails on master, but passes with this fix applied. Signed-off-by: Roger Peppe <[email protected]> Change-Id: I039a72d59d75b39c2dc1e5a2cad06c111669b6c2 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/547383 Reviewed-by: Daniel Martí <[email protected]> Unity-Result: CUE porcuepine <[email protected]> TryBot-Result: CUEcueckoo <[email protected]>
1 parent b25fc05 commit 8f796bf

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

cue/context_test.go

+54
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ import (
2121
"cuelang.org/go/cue"
2222
"cuelang.org/go/cue/build"
2323
"cuelang.org/go/cue/cuecontext"
24+
"cuelang.org/go/internal/astinternal"
25+
"cuelang.org/go/internal/cuetest"
2426
"cuelang.org/go/internal/cuetxtar"
27+
"cuelang.org/go/internal/tdtest"
2528
"github.com/go-quicktest/qt"
2629
"golang.org/x/tools/txtar"
2730
)
@@ -138,6 +141,57 @@ bar: [
138141
}
139142
}
140143

144+
func TestEncodeType(t *testing.T) {
145+
type testCase struct {
146+
name string
147+
x interface{}
148+
wantErr string
149+
out string
150+
}
151+
testCases := []testCase{{
152+
name: "Struct",
153+
x: struct {
154+
A int `json:"a"`
155+
B string `json:"b,omitempty"`
156+
C []bool
157+
}{},
158+
out: `{a: int64, b?: string, C?: *null|[...bool]}`,
159+
}, {
160+
name: "CUEValue#1",
161+
x: struct {
162+
A cue.Value `json:"a"`
163+
}{},
164+
out: `{a: _}`,
165+
}, {
166+
name: "CUEValue#2",
167+
x: cue.Value{},
168+
out: `_`,
169+
}, {
170+
// TODO this looks like a shortcoming of EncodeType.
171+
name: "map",
172+
x: map[string]int{},
173+
out: `*null|{}`,
174+
}, {
175+
name: "slice",
176+
x: []int{},
177+
out: `*null|[...int64]`,
178+
}, {
179+
name: "chan",
180+
x: chan int(nil),
181+
wantErr: `unsupported Go type \(chan int\)`,
182+
}}
183+
tdtest.Run(t, testCases, func(t *cuetest.T, tc *testCase) {
184+
v := cuecontext.New().EncodeType(tc.x)
185+
if tc.wantErr != "" {
186+
qt.Assert(t, qt.ErrorMatches(v.Err(), tc.wantErr))
187+
return
188+
}
189+
qt.Assert(t, qt.IsNil(v.Err()))
190+
got := fmt.Sprint(astinternal.DebugStr(v.Eval().Syntax()))
191+
t.Equal(got, tc.out)
192+
})
193+
}
194+
141195
func TestContextCheck(t *testing.T) {
142196
qt.Assert(t, qt.PanicMatches(func() {
143197
var c cue.Context

internal/core/convert/go.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,8 @@ func goTypeToValueRec(ctx *adt.OpContext, allowNullDefault bool, t reflect.Type)
602602
// strict instances and there cannot be any tags that further constrain
603603
// the values.
604604
if t.Implements(jsonMarshaler) || t.Implements(textMarshaler) {
605-
return topSentinel, nil
605+
e = topSentinel
606+
goto store
606607
}
607608

608609
switch k := t.Kind(); k {

0 commit comments

Comments
 (0)