Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow traversing by embedded fields #2

Merged
merged 1 commit into from
Mar 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,15 @@ func GetFieldByTag(ival interface{}, tagName string, fieldNames []string) (inter
for i := 0; i < valType.NumField(); i++ {
tagValue := valType.Field(i).Tag.Get(tagName)

// If it's an embedded field, traverse it.
if tagValue == "" && valType.Field(i).Anonymous {
value := val.Field(i).Interface()
val, err := GetFieldByTag(value, tagName, fieldNames)
if err == nil {
return val, nil
}
}

parts := strings.Split(tagValue, ",")
if parts[0] == fieldName {
value := val.Field(i).Interface()
Expand Down
15 changes: 15 additions & 0 deletions parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,8 +433,16 @@ func (s *PredicateSuite) TestEquals() {
s.False(pr.(BoolPredicate)())
}

type TestEmbedStruct struct {
EmbedParam struct {
EmbedKey1 map[string][]string `json:"embed_key1,omitempty"`
EmbedKey2 map[string]string `json:"embed_key2,omitempty"`
} `json:"embed_param,omitempty"`
}

// TestStruct is a test structure with json tags.
type TestStruct struct {
TestEmbedStruct
Param struct {
Key1 map[string][]string `json:"key1,omitempty"`
Key2 map[string]string `json:"key2,omitempty"`
Expand All @@ -444,6 +452,7 @@ type TestStruct struct {
func (s *PredicateSuite) TestGetTagField() {
val := TestStruct{}
val.Param.Key1 = map[string][]string{"key": {"val"}}
val.EmbedParam.EmbedKey1 = map[string][]string{"embed-key": {"embed-val"}}

type testCase struct {
tag string
Expand All @@ -461,6 +470,12 @@ func (s *PredicateSuite) TestGetTagField() {
{tag: "json", val: &val, fields: []string{"param", "key3"}, err: trace.NotFound("not found")},
// nil pointer
{tag: "json", val: nil, fields: []string{"param", "key1"}, err: trace.BadParameter("bad param")},

// embedded field test equivalent
{tag: "json", val: val, fields: []string{"embed_param", "embed_key1"}, expect: val.EmbedParam.EmbedKey1},
{tag: "json", val: &val, fields: []string{"embed_param", "embed_key1"}, expect: val.EmbedParam.EmbedKey1},
{tag: "json", val: &val, fields: []string{"embed_param", "embed_key3"}, err: trace.NotFound("not found")},
{tag: "json", val: nil, fields: []string{"embed_param", "embed_key1"}, err: trace.BadParameter("bad param")},
}

for i, tc := range testCases {
Expand Down