Make JSONMarshaler methods require proto.Message (#7054)

* Make JSONMarshaler require proto.Message

* Use &msg with MarshalJSON

* Use *LegacyAmino in queriers instead of JSONMarshaler

* Revert ABCIMessageLogs String() and coins tests

* Use LegacyAmino in client/debug and fix subspace tests

* Use LegacyAmino in all legacy queriers and adapt simulation

* Make AminoCodec implement Marshaler and some godoc fixes

* Test fixes

* Remove unrelevant comment

* Use TxConfig.TxJSONEncoder

* Use encoding/json in genutil cli migrate/validate genesis cmds

* Address simulation related comments

* Use JSONMarshaler in cli tests

* Use proto.Message as respType in cli tests

* Use tmjson for tm GenesisDoc

* Update types/module/simulation.go

Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com>

* Update types/module/module_test.go

Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com>

* Add godoc comments

* Remove unused InsertKeyJSON

* Fix tests

Co-authored-by: Aaron Craelius <aaronc@users.noreply.github.com>
Co-authored-by: Federico Kunze <31522760+fedekunze@users.noreply.github.com>
This commit is contained in:
Marie
2020-08-26 09:39:38 +00:00
committed by GitHub
co-authored by Federico Kunze Aaron Craelius
parent b5bc864862
commit 7f59723d88
126 changed files with 580 additions and 492 deletions
-1
View File
@@ -233,7 +233,6 @@ func startInProcess(ctx *Context, legacyAminoCdc *codec.LegacyAmino, appCreator
clientCtx := client.Context{}.
WithHomeDir(home).
WithChainID(genDoc.ChainID).
WithJSONMarshaler(legacyAminoCdc).
// amino is needed here for backwards compatibility of REST routes
WithLegacyAmino(legacyAminoCdc).
WithClient(local.New(tmNode))
-20
View File
@@ -1,7 +1,6 @@
package server
import (
"encoding/json"
"errors"
"fmt"
"io"
@@ -21,7 +20,6 @@ import (
dbm "github.com/tendermint/tm-db"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/server/config"
"github.com/cosmos/cosmos-sdk/server/types"
sdk "github.com/cosmos/cosmos-sdk/types"
@@ -191,24 +189,6 @@ func AddCommands(rootCmd *cobra.Command, defaultNodeHome string, appCreator type
)
}
// InsertKeyJSON inserts a new JSON field/key with a given value to an existing
// JSON message. An error is returned if any serialization operation fails.
//
// NOTE: The ordering of the keys returned as the resulting JSON message is
// non-deterministic, so the client should not rely on key ordering.
func InsertKeyJSON(cdc codec.JSONMarshaler, baseJSON []byte, key string, value json.RawMessage) ([]byte, error) {
var jsonMap map[string]json.RawMessage
if err := cdc.UnmarshalJSON(baseJSON, &jsonMap); err != nil {
return nil, err
}
jsonMap[key] = value
bz, err := codec.MarshalJSONIndent(cdc, jsonMap)
return json.RawMessage(bz), err
}
// https://stackoverflow.com/questions/23558425/how-do-i-get-the-local-ip-address-in-go
// TODO there must be a better way to get external IP
func ExternalIP() (string, error) {
-41
View File
@@ -1,41 +0,0 @@
package server
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/require"
"github.com/cosmos/cosmos-sdk/codec"
)
func TestInsertKeyJSON(t *testing.T) {
cdc := codec.New()
foo := map[string]string{"foo": "foofoo"}
bar := map[string]string{"barInner": "barbar"}
// create raw messages
bz, err := cdc.MarshalJSON(foo)
require.NoError(t, err)
fooRaw := json.RawMessage(bz)
bz, err = cdc.MarshalJSON(bar)
require.NoError(t, err)
barRaw := json.RawMessage(bz)
// make the append
appBz, err := InsertKeyJSON(cdc, fooRaw, "barOuter", barRaw)
require.NoError(t, err)
// test the append
var appended map[string]json.RawMessage
err = cdc.UnmarshalJSON(appBz, &appended)
require.NoError(t, err)
var resBar map[string]string
err = cdc.UnmarshalJSON(appended["barOuter"], &resBar)
require.NoError(t, err)
require.Equal(t, bar, resBar, "appended: %v", appended)
}