Skip to content

Commit

Permalink
internal/core/convert: fix for embedded structs are not honored
Browse files Browse the repository at this point in the history
Fixes #376.

I added [a comment](cuelang/cue#376 (comment)) to #376 with some questions. Would be great if I could get some feedback on that as well as my proposed fix. My experience with cue is very limited.

Closes #568
cuelang/cue#568

GitOrigin-RevId: 00851951dfd10dd11489c99f1507d2f2c880183c
Change-Id: Ia857cdb14739e232a6028ebc32ee1a6a11494ebf
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/7684
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
  • Loading branch information
stromsvag authored and mpvl committed Nov 18, 2020
1 parent c715b94 commit 3585705
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 5 deletions.
32 changes: 32 additions & 0 deletions encoding/gocode/gocodec/codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,38 @@ func TestDecode(t *testing.T) {
}{{
in: "str",
want: `"str"`,
}, {
in: func() interface{} {
type T struct {
B int
}
type S struct {
A string
T
}
return S{}
}(),
want: `{
A: ""
B: 0
}`,
}, {
in: func() interface{} {
type T struct {
B int
}
type S struct {
A string
T `json:"t"`
}
return S{}
}(),
want: `{
A: ""
t: {
B: 0
}
}`,
}}
c := New(&cue.Runtime{}, nil)

Expand Down
24 changes: 19 additions & 5 deletions internal/core/convert/go.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ var tagsWithNames = []string{"json", "yaml", "protobuf"}

func getName(f *reflect.StructField) string {
name := f.Name
if f.Anonymous {
name = ""
}
for _, s := range tagsWithNames {
if tag, ok := f.Tag.Lookup(s); ok {
if p := strings.Index(tag, ","); p >= 0 {
Expand Down Expand Up @@ -405,18 +408,18 @@ func convertRec(ctx *adt.OpContext, nilIsTop bool, x interface{}) adt.Value {

t := value.Type()
for i := 0; i < value.NumField(); i++ {
t := t.Field(i)
if t.PkgPath != "" {
sf := t.Field(i)
if sf.PkgPath != "" {
continue
}
val := value.Field(i)
if !nilIsTop && isNil(val) {
continue
}
if tag, _ := t.Tag.Lookup("json"); tag == "-" {
if tag, _ := sf.Tag.Lookup("json"); tag == "-" {
continue
}
if isOmitEmpty(&t) && isZero(val) {
if isOmitEmpty(&sf) && isZero(val) {
continue
}
sub := convertRec(ctx, nilIsTop, val.Interface())
Expand All @@ -430,10 +433,21 @@ func convertRec(ctx *adt.OpContext, nilIsTop bool, x interface{}) adt.Value {

// leave errors like we do during normal evaluation or do we
// want to return the error?
name := getName(&t)
name := getName(&sf)
if name == "-" {
continue
}
if sf.Anonymous && name == "" {
arc, ok := sub.(*adt.Vertex)
if ok {
for _, a := range arc.Arcs {
obj.Decls = append(obj.Decls, &adt.Field{Label: a.Label, Value: a.Value})
v.Arcs = append(v.Arcs, a)
}
}
continue
}

f := ctx.StringLabel(name)
obj.Decls = append(obj.Decls, &adt.Field{Label: f, Value: sub})
arc, ok := sub.(*adt.Vertex)
Expand Down
15 changes: 15 additions & 0 deletions internal/core/convert/go_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,21 @@ func TestConvert(t *testing.T) {
reflect.ValueOf(3), "3",
}, {
time.Date(2019, 4, 1, 0, 0, 0, 0, time.UTC), `(string){ "2019-04-01T00:00:00Z" }`,
}, {
func() interface{} {
type T struct {
B int
}
type S struct {
A string
T
}
return S{}
}(),
`(struct){
A: (string){ "" }
B: (int){ 0 }
}`,
}}
r := runtime.New()
for _, tc := range testCases {
Expand Down

0 comments on commit 3585705

Please sign in to comment.