Skip to content

Commit

Permalink
fix(go): marshaling tag
Browse files Browse the repository at this point in the history
  • Loading branch information
rot1024 committed Dec 22, 2023
1 parent ec3e80f commit 0d83da5
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 2 deletions.
2 changes: 1 addition & 1 deletion go/marshaling.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ func Marshal(src any, item *Item) {

vft := vf.Type()
var value any
if m, ok := vf.Interface().(MarshalCMS); ok {
if m, ok := vf.Interface().(MarshalCMS); ok && m != nil {
value = m.MarshalCMS()
} else if vft.Kind() == reflect.Slice && vft.Elem().Kind() == reflect.String && vf.Len() > 0 {
st := reflect.TypeOf("")
Expand Down
2 changes: 1 addition & 1 deletion go/marshaling_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func TestMarshal(t *testing.T) {
GGG: []G{{ID: "1", AAA: "ggg"}},
HHH: []*G{{ID: "2", AAA: "hhh"}, nil},
JJJ: &Value{value: "foo"},
KKK: &Tag{ID: "tag"},
KKK: &Tag{Name: "tag"},
}

expected := &Item{
Expand Down
3 changes: 3 additions & 0 deletions go/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ func TagsFrom(j any) []Tag {
}

func (t Tag) MarshalCMS() any {
if t.ID == "" {
return t.Name
}
return t.ID
}

Expand Down

0 comments on commit 0d83da5

Please sign in to comment.