Created
April 8, 2022 21:15
-
-
Save tmclnk/2cb5dd55f1880b429b2db2a7dc271693 to your computer and use it in GitHub Desktop.
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
// AgilityPayload canonical payload shape for agility "REST" methods. | |
// Use this if you don't want to make structs for your backend call, | |
// but still want to access deeply nested fields which you wouldn't | |
// be able to dereference when using map[string]interface{}. | |
type AgilityPayload map[string]map[string]map[string][]map[string]interface{} | |
// ToMap to let you dump a map of names to meaningless objects | |
// into an even less useful map of names to meaningless objects. | |
// _this_ is what you'd pass to the RequestBody. | |
func (c *AgilityPayload) ToMap() map[string]interface{} { | |
result := make(map[string]interface{}) | |
for k, v := range *c { | |
result[k] = v | |
} | |
return result | |
} | |
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
// ToAgilityPayload is like our "toMap" functions, except it's returning | |
// AgilityPayload instead of map[string]interface{}. Still a map | |
// of names to meaningless objects though, it's just that we can | |
// inspect those meaningless objects because we know the structure. | |
func (c *CreditCardPaymentRequest) ToAgilityPayload() AgilityPayload { | |
m := AgilityPayload{ | |
"EcomCashRcptCCPaymentJSON": { | |
"dsEcomCashRcptPayment": { | |
"dtEcomPayment": { | |
{ | |
"customerID": c.Data.Attributes.CustomerID, | |
"shipToSequence": c.Data.Attributes.ShiptoSequence, | |
"processorTransactionID": c.Data.Attributes.CreditCardSurchage, | |
"paymentAmount": c.Data.Attributes.PaymentAmount, | |
}, | |
}, | |
}, | |
}, | |
} | |
return m | |
} | |
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
func TestAgilityPayload_ToMap(t *testing.T) { | |
// AgilityPayload obviates the need for putting | |
// map[string]interface{} or []map[string]interface{} here | |
payload := AgilityPayload{ | |
"EcomCashRcptCCPaymentJSON": { | |
"dsEcomCashRcptPayment": { | |
"dtEcomSelectedInvoiceList": { | |
{"aropenGUID": "56B5DCC7-75E8-4A11-85C2-3B2D8799918A"}, | |
}, | |
"dtEcomSelectedCreditInvoiceList": { | |
{"aropenGUID": "1C68A5FE-6241-4FAC-9311-C5F2A4D19A5C"}, | |
}, | |
}, | |
}, | |
} | |
assert.Equal(t, "56B5DCC7-75E8-4A11-85C2-3B2D8799918A", payload["EcomCashRcptCCPaymentJSON"]["dsEcomCashRcptPayment"]["dtEcomSelectedInvoiceList"][0]["aropenGUID"]) | |
assert.Equal(t, "1C68A5FE-6241-4FAC-9311-C5F2A4D19A5C", payload["EcomCashRcptCCPaymentJSON"]["dsEcomCashRcptPayment"]["dtEcomSelectedCreditInvoiceList"][0]["aropenGUID"]) | |
expected := map[string]interface{}{ | |
"EcomCashRcptCCPaymentJSON": map[string]interface{}{ | |
"dsEcomCashRcptPayment": map[string]interface{}{ | |
"dtEcomSelectedInvoiceList": []map[string]interface{}{ | |
{"aropenGUID": "56B5DCC7-75E8-4A11-85C2-3B2D8799918A"}, | |
}, | |
"dtEcomSelectedCreditInvoiceList": []map[string]interface{}{ | |
{"aropenGUID": "1C68A5FE-6241-4FAC-9311-C5F2A4D19A5C"}, | |
}, | |
}, | |
}, | |
} | |
expectedBytes, err := json.Marshal(expected) | |
assert.NoError(t, err) | |
payloadBytes, err := json.Marshal(payload) | |
assert.NoError(t, err) | |
assert.JSONEq(t, string(expectedBytes), string(payloadBytes)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment