Skip to content

Commit 8d099aa

Browse files
committed
cmd/cue: Go types implementing a supported interface are themselves supported
Otherwise, the tweaked test case type, which implements yaml.Marshaler, would get discarded as an unsupported struct given that it's only made up of unexported fields. Note that the existing CustomYAML test case was buggy; it did not actually use the correct MarshalYAML method signature, so we weren't testing the fact that it was a custom yaml.Marshaler. This is now fixed, which causes it to generate as "top" as expected. Fixes #3495. Signed-off-by: Daniel Martí <[email protected]> Change-Id: Ie9926daf829e21c9e9f117f1a813edcd9b6f50d3 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1224699 TryBot-Result: CUEcueckoo <[email protected]> Reviewed-by: Roger Peppe <[email protected]>
1 parent 21abc7f commit 8d099aa

File tree

2 files changed

+17
-12
lines changed

2 files changed

+17
-12
lines changed

cmd/cue/cmd/get_go.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,7 @@ func (e *extractor) reportDecl(x *ast.GenDecl) (a []cueast.Decl) {
732732
fallthrough
733733

734734
default:
735-
if !supportedType(nil, typ) {
735+
if !e.supportedType(nil, typ) {
736736
e.logf(" Dropped declaration %v of unsupported type %v", name, typ)
737737
continue
738738
}
@@ -976,7 +976,11 @@ func makeDoc(g *ast.CommentGroup, isDoc bool) *cueast.CommentGroup {
976976
return &cueast.CommentGroup{Doc: isDoc, List: a}
977977
}
978978

979-
func supportedType(stack []types.Type, t types.Type) (ok bool) {
979+
func (e *extractor) supportedType(stack []types.Type, t types.Type) (ok bool) {
980+
if s := e.altType(t); s != nil {
981+
// t implements a supported interface.
982+
return true
983+
}
980984
// handle recursive types
981985
if slices.Contains(stack, t) {
982986
return true
@@ -1015,27 +1019,27 @@ func supportedType(stack []types.Type, t types.Type) (ok bool) {
10151019
case *types.Basic:
10161020
return true
10171021
case *types.Named:
1018-
return supportedType(stack, t.Underlying())
1022+
return e.supportedType(stack, t.Underlying())
10191023
case *types.TypeParam:
1020-
return supportedType(stack, t.Underlying())
1024+
return e.supportedType(stack, t.Underlying())
10211025
case *types.Pointer:
1022-
return supportedType(stack, t.Elem())
1026+
return e.supportedType(stack, t.Elem())
10231027
case *types.Slice:
1024-
return supportedType(stack, t.Elem())
1028+
return e.supportedType(stack, t.Elem())
10251029
case *types.Array:
1026-
return supportedType(stack, t.Elem())
1030+
return e.supportedType(stack, t.Elem())
10271031
case *types.Map:
10281032
if b, ok := t.Key().Underlying().(*types.Basic); !ok || b.Kind() != types.String {
10291033
return false
10301034
}
1031-
return supportedType(stack, t.Elem())
1035+
return e.supportedType(stack, t.Elem())
10321036
case *types.Struct:
10331037
// Eliminate structs with fields for which all fields are filtered.
10341038
if t.NumFields() == 0 {
10351039
return true
10361040
}
10371041
for f := range t.Fields() {
1038-
if f.Exported() && supportedType(stack, f.Type()) {
1042+
if f.Exported() && e.supportedType(stack, f.Type()) {
10391043
return true
10401044
}
10411045
}
@@ -1365,7 +1369,7 @@ func (e *extractor) addFields(x *types.Struct, st *cueast.StructLit) {
13651369
if !ast.IsExported(f.Name()) {
13661370
continue
13671371
}
1368-
if !supportedType(nil, f.Type()) {
1372+
if !e.supportedType(nil, f.Type()) {
13691373
e.logf(" Dropped field %v for unsupported type %v", f.Name(), f.Type())
13701374
continue
13711375
}

cmd/cue/cmd/testdata/script/get_go_types.txtar

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,9 +215,10 @@ func (c *CustomJSON) MarshalJSON() ([]byte, error) {
215215
}
216216

217217
type CustomYAML struct {
218+
unexported int
218219
}
219220

220-
func (c CustomYAML) MarshalYAML() ([]byte, error) {
221+
func (c CustomYAML) MarshalYAML() (any, error) {
221222
return nil, nil
222223
}
223224

@@ -479,7 +480,7 @@ _#internalIdentifier: #Identifier & "internal"
479480

480481
#CustomJSON: _
481482

482-
#CustomYAML: {}
483+
#CustomYAML: _
483484

484485
_#localType: int
485486

0 commit comments

Comments
 (0)