Skip to content

Commit

Permalink
Fixing index management for params of type arrays of hashes in fixtur…
Browse files Browse the repository at this point in the history
…es (stripe#617)
  • Loading branch information
gracegoo-stripe authored Mar 23, 2021
1 parent fb121ab commit 4948b1c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
22 changes: 15 additions & 7 deletions pkg/fixtures/fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ func (fxt *Fixture) parseInterface(params interface{}) []string {
data = append(data, fxt.parseMap(m, "", -1)...)
case reflect.Array:
a := params.([]interface{})
data = append(data, fxt.parseArray(a, "", -1)...)
data = append(data, fxt.parseArray(a, "")...)
default:
}

Expand All @@ -221,15 +221,18 @@ func (fxt *Fixture) parseMap(params map[string]interface{}, parent string, index
for key, value := range params {
switch {
case parent != "" && index >= 0:
// ex: lines[0][id] = "id_0000", lines[1][id] = "id_1234", etc.
keyname = fmt.Sprintf("%s[%d][%s]", parent, index, key)
case parent != "":
// ex: metadata[name] = "blah", metadata[timestamp] = 1231341525, etc.
keyname = fmt.Sprintf("%s[%s]", parent, key)
default:
keyname = key
}

switch v := reflect.ValueOf(value); v.Kind() {
case reflect.String:
// A string can be a regular value or one we need to look up first, ex: ${product.id}
data = append(data, fmt.Sprintf("%s=%s", keyname, fxt.parseQuery(v.String())))
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
data = append(data, fmt.Sprintf("%s=%v", keyname, v.Int()))
Expand Down Expand Up @@ -264,7 +267,7 @@ func (fxt *Fixture) parseMap(params map[string]interface{}, parent string, index
case reflect.Array, reflect.Slice:
a := value.([]interface{})

result := fxt.parseArray(a, keyname, index)
result := fxt.parseArray(a, keyname)
data = append(data, result...)
default:
continue
Expand All @@ -274,22 +277,27 @@ func (fxt *Fixture) parseMap(params map[string]interface{}, parent string, index
return data
}

func (fxt *Fixture) parseArray(params []interface{}, parent string, index int) []string {
// This function interates through each element in the array and handles the parsing accordingly depending on the type of array.
func (fxt *Fixture) parseArray(params []interface{}, parent string) []string {
data := make([]string, len(params))

// The index is only used for arrays of maps
index := -1
for _, value := range params {
switch v := reflect.ValueOf(value); v.Kind() {
case reflect.String:
// A string can be a regular value or one we need to look up first, ex: ${product.id}
data = append(data, fmt.Sprintf("%s[]=%s", parent, fxt.parseQuery(v.String())))
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
data = append(data, fmt.Sprintf("%s[]=%v", parent, v.Int()))
case reflect.Map:
m := value.(map[string]interface{})
// When we parse arrays of maps, we want to track an index for the request
data = append(data, fxt.parseMap(m, parent, index+1)...)
// When we parse arrays of maps, we want to track the index of the element for the request
// ex: lines[0][id] = "id_0000", lines[1][id] = "id_1234", etc.
index++
data = append(data, fxt.parseMap(m, parent, index)...)
case reflect.Array, reflect.Slice:
a := value.([]interface{})
data = append(data, fxt.parseArray(a, parent, index)...)
data = append(data, fxt.parseArray(a, parent)...)
default:
continue
}
Expand Down
19 changes: 17 additions & 2 deletions pkg/fixtures/fixtures_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,21 +102,36 @@ func TestParseInterface(t *testing.T) {
address["line1"] = "1 Planet Express St"
address["city"] = "New New York"

// array of hashes
taxIDData := make([]interface{}, 2)
taxIDZero := make(map[string]interface{})
taxIDZero["type"] = "type_0"
taxIDZero["value"] = "value_0"
taxIDOne := make(map[string]interface{})
taxIDOne["type"] = "type_1"
taxIDOne["value"] = "value_1"
taxIDData[0] = taxIDZero
taxIDData[1] = taxIDOne

data := make(map[string]interface{})
data["name"] = "Bender Bending Rodriguez"
data["email"] = "[email protected]"
data["address"] = address

data["tax_id_data"] = taxIDData
fxt := Fixture{}

output := (fxt.parseInterface(data))
sort.Strings(output)

require.Equal(t, len(output), 4)
require.Equal(t, len(output), 8)
require.Equal(t, output[0], "address[city]=New New York")
require.Equal(t, output[1], "address[line1]=1 Planet Express St")
require.Equal(t, output[2], "[email protected]")
require.Equal(t, output[3], "name=Bender Bending Rodriguez")
require.Equal(t, output[4], "tax_id_data[0][type]=type_0")
require.Equal(t, output[5], "tax_id_data[0][value]=value_0")
require.Equal(t, output[6], "tax_id_data[1][type]=type_1")
require.Equal(t, output[7], "tax_id_data[1][value]=value_1")
}

func TestParseWithQueryIgnoreDefault(t *testing.T) {
Expand Down

0 comments on commit 4948b1c

Please sign in to comment.