-
Notifications
You must be signed in to change notification settings - Fork 206
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(vlocalchain): use
jsonp.Marshaller
with OrigName: false
for …
…response marshalling (#9447) closes: #9445 ## Description Uses `jsonp.Marshaller` with `OrigName: false` for response marshalling for `vlocalchain` queries and transactions to ensure responses sent to the JS side of the bridge are camelCase instead of snake_case. ### Security Considerations ### Scaling Considerations ### Documentation Considerations ### Testing Considerations Starts with new tests that have multiword response keys (e.g. `completion_time`/`completionTime`, `validator_address`/`validatorAddress`) to demonstrate current behavior. ### Upgrade Considerations `vlocalchain` has yet to be released, but this is important to land in the initial iteration to avoid future breaking changes.
- Loading branch information
Showing
5 changed files
with
366 additions
and
26 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package vm | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"github.com/gogo/protobuf/jsonpb" | ||
"github.com/gogo/protobuf/proto" | ||
) | ||
|
||
// We need jsonpb for its access to the global registry. | ||
var marshaller = jsonpb.Marshaler{EmitDefaults: true} | ||
|
||
func ProtoJSONMarshal(val interface{}) ([]byte, error) { | ||
if pm, ok := val.(proto.Message); ok { | ||
var s string | ||
s, err := marshaller.MarshalToString(pm) | ||
return []byte(s), err | ||
} | ||
|
||
// Marshal a non-proto value to JSON. | ||
return json.Marshal(val) | ||
} | ||
|
||
// ProtoJSONMarshalSlice marshals a slice of proto messages and non-proto values to | ||
// a single JSON byte slice. | ||
func ProtoJSONMarshalSlice(vals []interface{}) ([]byte, error) { | ||
var err error | ||
jsonSlice := make([]json.RawMessage, len(vals)) | ||
for i, val := range vals { | ||
jsonSlice[i], err = ProtoJSONMarshal(val) | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
|
||
// Marshal the JSON array to a single JSON byte slice. | ||
return json.Marshal(jsonSlice) | ||
} |
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,103 @@ | ||
package vm_test | ||
|
||
import ( | ||
"bytes" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/Agoric/agoric-sdk/golang/cosmos/vm" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
|
||
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" | ||
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" | ||
) | ||
|
||
func TestProtoJSONMarshal(t *testing.T) { | ||
accAddr, err := sdk.AccAddressFromHexUnsafe("0123456789") | ||
if err != nil { | ||
panic(err) | ||
} | ||
valAddr, err := sdk.ValAddressFromHex("9876543210") | ||
if err != nil { | ||
panic(err) | ||
} | ||
coin := sdk.NewInt64Coin("uatom", 1234567) | ||
|
||
testCases := []struct { | ||
name string | ||
create func() interface{} | ||
expected string | ||
}{ | ||
{ | ||
"nil", | ||
func() interface{} { | ||
return nil | ||
}, | ||
`null`, | ||
}, | ||
{ | ||
"primitive number", | ||
func() interface{} { | ||
return 12345 | ||
}, | ||
`12345`, | ||
}, | ||
{ | ||
"MsgDelegate", | ||
func() interface{} { | ||
return stakingtypes.NewMsgDelegate(accAddr, valAddr, coin) | ||
}, | ||
`{"delegatorAddress":"cosmos1qy352eufjjmc9c","validatorAddress":"cosmosvaloper1npm9gvss52mlmk","amount":{"denom":"uatom","amount":"1234567"}}`, | ||
}, | ||
{ | ||
"QueryDenomOwnersResponse", | ||
func() interface{} { | ||
return &banktypes.QueryDenomOwnersResponse{ | ||
DenomOwners: []*banktypes.DenomOwner{ | ||
{ | ||
Address: accAddr.String(), | ||
Balance: coin, | ||
}, | ||
{ | ||
Address: valAddr.String(), | ||
Balance: coin.Add(coin), | ||
}, | ||
}, | ||
} | ||
}, | ||
`{"denomOwners":[{"address":"cosmos1qy352eufjjmc9c","balance":{"denom":"uatom","amount":"1234567"}},{"address":"cosmosvaloper1npm9gvss52mlmk","balance":{"denom":"uatom","amount":"2469134"}}],"pagination":null}`, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
val := tc.create() | ||
bz, err := vm.ProtoJSONMarshal(val) | ||
if err != nil { | ||
t.Errorf("ProtoJSONMarshal of %q failed %v", val, err) | ||
} | ||
if !bytes.Equal(bz, []byte(tc.expected)) { | ||
t.Errorf("ProtoJSONMarshal of %q returned %q, expected %q", val, string(bz), tc.expected) | ||
} | ||
}) | ||
} | ||
|
||
t.Run("all in a slice", func(t *testing.T) { | ||
vals := make([]interface{}, len(testCases)) | ||
expectedJson := make([]string, len(testCases)) | ||
for i, tc := range testCases { | ||
vals[i] = tc.create() | ||
expectedJson[i] = tc.expected | ||
} | ||
bz, err := vm.ProtoJSONMarshalSlice(vals) | ||
if err != nil { | ||
t.Errorf("ProtoJSONMarshalSlice of %q failed %v", vals, err) | ||
} | ||
|
||
expected := "[" + strings.Join(expectedJson, ",") + "]" | ||
if !bytes.Equal(bz, []byte(expected)) { | ||
t.Errorf("ProtoJSONMarshalSlice of %q returned %q, expected %q", vals, string(bz), expected) | ||
} | ||
}) | ||
} |
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
Oops, something went wrong.