Skip to content

Instantly share code, notes, and snippets.

@tmclnk
Created April 8, 2022 21:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tmclnk/2cb5dd55f1880b429b2db2a7dc271693 to your computer and use it in GitHub Desktop.
Save tmclnk/2cb5dd55f1880b429b2db2a7dc271693 to your computer and use it in GitHub Desktop.
// 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
}
// 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
}
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