Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #74 from stripe/brandur-data-replacement
Replace data in response with data from request
- Loading branch information
Showing
8 changed files
with
219 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package datareplacer | ||
|
||
import ( | ||
"reflect" | ||
) | ||
|
||
func ReplaceData(requestData map[string]interface{}, responseData map[string]interface{}) map[string]interface{} { | ||
for k, requestValue := range requestData { | ||
responseValue, ok := responseData[k] | ||
|
||
// Recursively call in to replace data, but only if the key is | ||
// in both maps. | ||
// | ||
// A fairly obvious improvement here is if a key is in the | ||
// request but not present in the response, then check the | ||
// canonical schema to see if it's there. It might be an | ||
// optional field that doesn't appear in the fixture, and if it | ||
// was given to us with the request, we probably want to | ||
// include it. | ||
if ok { | ||
requestKeyMap, requestKeyOK := requestValue.(map[string]interface{}) | ||
responseKeyMap, responseKeyOK := responseValue.(map[string]interface{}) | ||
|
||
if requestKeyOK && responseKeyOK { | ||
responseData[k] = ReplaceData(requestKeyMap, responseKeyMap) | ||
} else { | ||
// In the non-map case, just set the respons key's value to | ||
// what was in the request, but only if both values are the | ||
// same type (this is to prevent problems where a field is set | ||
// as an ID, but the response field is the hydrated object of | ||
// that). | ||
// | ||
// While this will largely be "good enough", there's some | ||
// obvious cases that aren't going to be handled correctly like | ||
// index-based array updates (e.g., | ||
// `additional_owners[1][name]=...`). I'll have to iron out | ||
// that rough edges later on. | ||
if isSameType(requestValue, responseValue) { | ||
responseData[k] = requestValue | ||
} | ||
} | ||
} | ||
} | ||
|
||
return responseData | ||
} | ||
|
||
func isSameType(v1, v2 interface{}) bool { | ||
return reflect.ValueOf(v1).Type() == reflect.ValueOf(v2).Type() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package datareplacer | ||
|
||
import ( | ||
"testing" | ||
|
||
assert "github.com/stretchr/testify/require" | ||
) | ||
|
||
// | ||
// Tests | ||
// | ||
|
||
func TestReplaceData_Basic(t *testing.T) { | ||
responseData := map[string]interface{}{ | ||
"foo": "response-value", | ||
} | ||
|
||
ReplaceData(map[string]interface{}{ | ||
"foo": "request-value", | ||
}, responseData) | ||
|
||
assert.Equal(t, map[string]interface{}{ | ||
"foo": "request-value", | ||
}, responseData) | ||
} | ||
|
||
// Arrays are currently just replaced wholesale. | ||
func TestReplaceData_Array(t *testing.T) { | ||
responseData := map[string]interface{}{ | ||
"arr": []string{ | ||
"response-value", | ||
}, | ||
} | ||
|
||
ReplaceData(map[string]interface{}{ | ||
"arr": []string{ | ||
"request-value", | ||
}, | ||
}, responseData) | ||
|
||
assert.Equal(t, map[string]interface{}{ | ||
"arr": []string{ | ||
"request-value", | ||
}, | ||
}, responseData) | ||
} | ||
|
||
func TestReplaceData_Recursive(t *testing.T) { | ||
responseData := map[string]interface{}{ | ||
"map": map[string]interface{}{ | ||
"nested": "response-value", | ||
}, | ||
} | ||
|
||
ReplaceData(map[string]interface{}{ | ||
"map": map[string]interface{}{ | ||
"nested": "request-value", | ||
}, | ||
}, responseData) | ||
|
||
assert.Equal(t, map[string]interface{}{ | ||
"map": map[string]interface{}{ | ||
"nested": "request-value", | ||
}, | ||
}, responseData) | ||
} | ||
|
||
func TestReplaceData_DontReplaceOnDifferentFields(t *testing.T) { | ||
responseData := map[string]interface{}{ | ||
"other": "other-value", | ||
} | ||
|
||
ReplaceData(map[string]interface{}{ | ||
"foo": "request-value", | ||
}, responseData) | ||
|
||
assert.Equal(t, map[string]interface{}{ | ||
"other": "other-value", | ||
}, responseData) | ||
} | ||
|
||
func TestReplaceData_DontReplaceOnDifferentTypes(t *testing.T) { | ||
responseData := map[string]interface{}{ | ||
"foo": "response-value", | ||
} | ||
|
||
ReplaceData(map[string]interface{}{ | ||
"foo": 7, | ||
}, responseData) | ||
|
||
assert.Equal(t, map[string]interface{}{ | ||
"foo": "response-value", | ||
}, responseData) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.