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

Support slices in event.GetObjValue and add tests #506

Merged
merged 2 commits into from
Jan 8, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
24 changes: 23 additions & 1 deletion event.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package stripe
import (
"encoding/json"
"fmt"
"strconv"
)

// Event is the resource representing a Stripe event.
Expand Down Expand Up @@ -82,7 +83,28 @@ func getValue(m map[string]interface{}, keys []string) string {
node := m[keys[0]]

for i := 1; i < len(keys); i++ {
node = node.(map[string]interface{})[keys[i]]
key := keys[i]

sliceNode, ok := node.([]interface{})
if ok {
intKey, err := strconv.Atoi(key)
if err != nil {
panic(fmt.Sprintf(
"Cannot access nested slice element with non-integer key: %s",
key))
}
node = sliceNode[intKey]
continue
}

mapNode, ok := node.(map[string]interface{})
if ok {
node = mapNode[key]
continue
}

panic(fmt.Sprintf(
"Cannot descend into non-map non-slice object with key: %s", key))
}

if node == nil {
Expand Down
55 changes: 55 additions & 0 deletions event_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package stripe

import (
"testing"

assert "github.com/stretchr/testify/require"
)

func TestGetObjValue(t *testing.T) {
event := &Event{
Data: &EventData{
Obj: map[string]interface{}{
"top_level_key": "top_level",
"integer_key": 123,
"map": map[string]interface{}{
"nested_key": "nested",
},
"slice": []interface{}{
"index-0",
"index-1",
"index-2",
},
"slice_of_maps": []interface{}{
map[string]interface{}{
"slice_nested_key": "slice_nested",
},
},
},
},
}

assert.Equal(t, "top_level", event.GetObjValue("top_level_key"))

// Check that it coerces non-string values into strings (this behavior is
// somewhat questionable, but I'm going with how it already works)
assert.Equal(t, "123", event.GetObjValue("integer_key"))

assert.Equal(t, "nested", event.GetObjValue("map", "nested_key"))
assert.Equal(t, "index-1", event.GetObjValue("slice", "1"))
assert.Equal(t, "slice_nested",
event.GetObjValue("slice_of_maps", "0", "slice_nested_key"))

// By design a `nil` just returns an empty string
assert.Equal(t, "", event.GetObjValue("bad_key"))

// Panic conditions. Usually the function tries to just return a value is
// fairly forgiving, but it does panic under certain obviously impossible
// cases.
assert.Panicsf(t, func() {
event.GetObjValue("slice", "string_key")
}, "Cannot access nested slice element with non-integer key: %s", "bad_key")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm likely missing something since the tests are passing, but shouldn't the assert expect "string_key", not "bad_key"?

assert.Panicsf(t, func() {
event.GetObjValue("top_level_key", "bad_key")
}, "Cannot descend into non-map non-slice object with key: %s", "bad_key")
}