Skip to content

Commit

Permalink
Seralize struct field interface without hash tag
Browse files Browse the repository at this point in the history
  • Loading branch information
krhubert committed Sep 9, 2019
1 parent 4feabe7 commit fe44219
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 6 deletions.
21 changes: 17 additions & 4 deletions hash/structhash/structhash.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,15 +91,22 @@ func write(buf *bytes.Buffer, v reflect.Value) *bytes.Buffer {
buf.WriteString("nil")
return buf
}
if v.CanInterface() {
write(buf, v.Elem())
}
write(buf, v.Elem())
case reflect.Struct:
vt := v.Type()
items := make([]string, 0)
for i := 0; i < v.NumField(); i++ {
sf := vt.Field(i)
to := parseTag(sf)

// NOTE: structhash will allow to process all interface types.
// gogo/protobuf is not able to set tags for directly oneof interface.
// see: https://github.com/gogo/protobuf/issues/623
if to.name == "" && reflect.Zero(sf.Type).Kind() == reflect.Interface {
to.skip = false
to.name = sf.Name
}

if to.skip || to.omitempty && isEmptyValue(v.Field(i)) {
continue
}
Expand Down Expand Up @@ -186,10 +193,16 @@ type tagOptions struct {
// comma-separated options.
func parseTag(f reflect.StructField) tagOptions {
tag := f.Tag.Get("hash")
if tag == "" || tag == "-" {
if tag == "" {
// NOTE: skip - interface with no tags can still be serialzied
return tagOptions{skip: true}
}

if tag == "-" {
// force skip - set name for "-"
return tagOptions{name: "-", skip: true}
}

var to tagOptions
options := strings.Split(tag, ",")
for _, option := range options {
Expand Down
35 changes: 33 additions & 2 deletions hash/structhash/structhash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,37 @@ func TestDump(t *testing.T) {
}{nil},
"{a:nil}",
},
{
struct {
a interface{} `hash:"name:a"`
}{
struct {
b map[string]int `hash:"name:b"`
}{
map[string]int{
"c": 1,
},
},
},
"{a:{b:(\"c\":1)}}",
},
// NOTE: structhash will allow to process all interface types.
// gogo/protobuf is not able to set tags for directly oneof interface.
// see: https://github.com/gogo/protobuf/issues/623
{
struct {
a interface{}
}{
struct {
b map[string]int `hash:"name:b"`
}{
map[string]int{
"c": 1,
},
},
},
"{a:{b:(\"c\":1)}}",
},
{
struct {
A interface{} `hash:"name:A"`
Expand Down Expand Up @@ -112,7 +143,7 @@ func TestDump(t *testing.T) {

for _, tt := range tests {
s := Dump(tt.v)
assert.Equalf(t, []byte(tt.s), s, "type %s: %v", reflect.TypeOf(tt.v), tt.v)
assert.Equalf(t, tt.s, string(s), "type %s: %v", reflect.TypeOf(tt.v), tt.v)
}
}

Expand Down Expand Up @@ -185,7 +216,7 @@ func TestTag(t *testing.T) {
}

for _, tt := range tests {
assert.Equalf(t, []byte(tt.s), serialize(tt.v), "type %s: %v", reflect.TypeOf(tt.v), tt.v)
assert.Equalf(t, tt.s, string(serialize(tt.v)), "type %s: %v", reflect.TypeOf(tt.v), tt.v)
}
}

Expand Down

0 comments on commit fe44219

Please sign in to comment.