Skip to content

Commit

Permalink
Restoring GetValue
Browse files Browse the repository at this point in the history
Restores GetValue to it's original interface to avoid breaking changes.
  • Loading branch information
MbolotSuse committed Feb 23, 2024
1 parent 1592712 commit 53fba45
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
17 changes: 15 additions & 2 deletions pkg/data/values.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,25 @@ func GetValueN(data map[string]interface{}, keys ...string) interface{} {
return val
}

// GetValue retrieves a value from the provided collection, which must be a map[string]interface or a []interface.
// GetValue works similar to GetValueFromAny, but can only process maps. Kept this way to avoid breaking changes with
// the previous interface, GetValueFromAny should be used in most cases since that can handle slices as well.
func GetValue(data map[string]interface{}, keys ...string) (interface{}, bool) {
for i, key := range keys {
if i == len(keys)-1 {
val, ok := data[key]
return val, ok
}
data, _ = data[key].(map[string]interface{})
}
return nil, false
}

// GetValueFromAny retrieves a value from the provided collection, which must be a map[string]interface or a []interface.
// Keys are always strings.
// For a map, a key denotes the key in the map whose value we want to retrieve.
// For the slice, it denotes the index (starting at 0) of the value we want to retrieve.
// Returns the retrieved value (if any) and a bool indicating if the value was found.
func GetValue(data interface{}, keys ...string) (interface{}, bool) {
func GetValueFromAny(data interface{}, keys ...string) (interface{}, bool) {
for i, key := range keys {
if i == len(keys)-1 {
if dataMap, ok := data.(map[string]interface{}); ok {
Expand Down
4 changes: 2 additions & 2 deletions pkg/data/values_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/stretchr/testify/assert"
)

func TestGetValue(t *testing.T) {
func TestGetValueFromAny(t *testing.T) {
t.Parallel()
tests := []struct {
name string
Expand Down Expand Up @@ -216,7 +216,7 @@ func TestGetValue(t *testing.T) {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
gotValue, gotSuccess := GetValue(test.data, test.keys...)
gotValue, gotSuccess := GetValueFromAny(test.data, test.keys...)
assert.Equal(t, test.wantValue, gotValue)
assert.Equal(t, test.wantSuccess, gotSuccess)
})
Expand Down

0 comments on commit 53fba45

Please sign in to comment.