Skip to content

Commit

Permalink
change toMapStr as global method & bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
khushijain21 committed Oct 30, 2024
1 parent 9d6c29d commit ed1b2ff
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
10 changes: 7 additions & 3 deletions mapstr/mapstr.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,10 @@ func (m M) FindFold(key string) (matchedKey string, value interface{}, err error
}
}

if !found {
return "", nil, ErrKeyNotFound
}

return matchedKey, value, nil
}

Expand Down Expand Up @@ -462,7 +466,7 @@ func mergeFieldsGetDestMap(target, from M, underRoot bool) (M, error) {
} else {
// Use existing 'fields' value.
var err error
destMap, err = toMapStr(f)
destMap, err = ToMapStr(f)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -515,7 +519,7 @@ func AddTagsWithKey(ms M, key string, tags []string) error {
// toMapStr performs a type assertion on v and returns a MapStr. v can be either
// a MapStr or a map[string]interface{}. If it's any other type or nil then
// an error is returned.
func toMapStr(v interface{}) (M, error) {
func ToMapStr(v interface{}) (M, error) {
m, ok := tryToMapStr(v)
if !ok {
return nil, fmt.Errorf("expected map but type is %T", v)
Expand Down Expand Up @@ -571,7 +575,7 @@ func mapFind(
}
}

v, err := toMapStr(d)
v, err := ToMapStr(d)
if err != nil {
return "", nil, nil, false, err
}
Expand Down
5 changes: 5 additions & 0 deletions mapstr/mapstr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1192,6 +1192,11 @@ func TestFindFold(t *testing.T) {
key: "level1_field1.not_exists.some_key",
expErr: "key not found",
},
{
name: "returns non-found error",
key: "level1_field4",
expErr: "key not found",
},
}

for _, tc := range cases {
Expand Down

0 comments on commit ed1b2ff

Please sign in to comment.