diff --git a/broker/common/common.go b/broker/common/common.go index 4205b573..3c9e9955 100644 --- a/broker/common/common.go +++ b/broker/common/common.go @@ -29,6 +29,9 @@ func StructToMap(obj interface{}) (map[string]interface{}, error) { jsonTag, ok := typ.Field(i).Tag.Lookup("json") if ok { before, _, found := strings.Cut(jsonTag, ",") + if before == "-" { + continue + } if found { fieldName = before } else { diff --git a/broker/common/common_test.go b/broker/common/common_test.go index d6c66a95..28c81162 100644 --- a/broker/common/common_test.go +++ b/broker/common/common_test.go @@ -1,6 +1,7 @@ package common import ( + "encoding/xml" "reflect" "testing" @@ -13,6 +14,11 @@ type User struct { Active bool } +type userWithIgnoredField struct { + Name string `json:"name"` + XMLName xml.Name `json:"-"` +} + var bob = "Bob" var alice = "Alice" @@ -43,6 +49,14 @@ func TestStructToMap(t *testing.T) { }, wantErr: false, }, + { + name: "Skip json dash fields", + input: userWithIgnoredField{Name: "alice"}, + want: map[string]interface{}{ + "name": "alice", + }, + wantErr: false, + }, { name: "Error on non-struct (int)", input: 42,