Merge PR #4253: Change User Supplied Param Change Value to json.RawMessage

This commit is contained in:
Alexander Bezobchuk
2019-05-02 14:21:35 -04:00
committed by GitHub
parent 60a6a8dcef
commit 67f1e12eec
8 changed files with 93 additions and 26 deletions
+50 -15
View File
@@ -1,6 +1,7 @@
package utils
import (
"encoding/json"
"io/ioutil"
"github.com/cosmos/cosmos-sdk/codec"
@@ -9,24 +10,58 @@ import (
"github.com/cosmos/cosmos-sdk/x/params"
)
// ParamChangeProposalJSON defines a ParameterChangeProposal with a deposit used
// to parse parameter change proposals from a JSON file.
type ParamChangeProposalJSON struct {
Title string `json:"title"`
Description string `json:"description"`
Changes []params.ParamChange `json:"changes"`
Deposit sdk.Coins `json:"deposit"`
type (
// ParamChangesJSON defines a slice of ParamChangeJSON objects which can be
// converted to a slice of ParamChange objects.
ParamChangesJSON []ParamChangeJSON
// ParamChangeJSON defines a parameter change used in JSON input. This
// allows values to be specified in raw JSON instead of being string encoded.
ParamChangeJSON struct {
Subspace string `json:"subspace"`
Key string `json:"key"`
Subkey string `json:"subkey,omitempty"`
Value json.RawMessage `json:"value"`
}
// ParamChangeProposalJSON defines a ParameterChangeProposal with a deposit used
// to parse parameter change proposals from a JSON file.
ParamChangeProposalJSON struct {
Title string `json:"title"`
Description string `json:"description"`
Changes ParamChangesJSON `json:"changes"`
Deposit sdk.Coins `json:"deposit"`
}
// ParamChangeProposalReq defines a parameter change proposal request body.
ParamChangeProposalReq struct {
BaseReq rest.BaseReq `json:"base_req"`
Title string `json:"title"`
Description string `json:"description"`
Changes ParamChangesJSON `json:"changes"`
Proposer sdk.AccAddress `json:"proposer"`
Deposit sdk.Coins `json:"deposit"`
}
)
func NewParamChangeJSON(subspace, key, subkey string, value json.RawMessage) ParamChangeJSON {
return ParamChangeJSON{subspace, key, subkey, value}
}
// ParamChangeProposalReq defines a parameter change proposal request body.
type ParamChangeProposalReq struct {
BaseReq rest.BaseReq `json:"base_req"`
// ToParamChange converts a ParamChangeJSON object to ParamChange.
func (pcj ParamChangeJSON) ToParamChange() params.ParamChange {
return params.NewParamChange(pcj.Subspace, pcj.Key, pcj.Subkey, string(pcj.Value))
}
Title string `json:"title"`
Description string `json:"description"`
Changes []params.ParamChange `json:"changes"`
Proposer sdk.AccAddress `json:"proposer"`
Deposit sdk.Coins `json:"deposit"`
// ToParamChanges converts a slice of ParamChangeJSON objects to a slice of
// ParamChange.
func (pcj ParamChangesJSON) ToParamChanges() []params.ParamChange {
res := make([]params.ParamChange, len(pcj))
for i, pc := range pcj {
res[i] = pc.ToParamChange()
}
return res
}
// ParseParamChangeProposalJSON reads and parses a ParamChangeProposalJSON from
+35
View File
@@ -0,0 +1,35 @@
package utils
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/require"
)
func TestNewParamChangeJSON(t *testing.T) {
pcj := NewParamChangeJSON("subspace", "key", "subkey", json.RawMessage(`{}`))
require.Equal(t, "subspace", pcj.Subspace)
require.Equal(t, "key", pcj.Key)
require.Equal(t, "subkey", pcj.Subkey)
require.Equal(t, json.RawMessage(`{}`), pcj.Value)
}
func TestToParamChanges(t *testing.T) {
pcj1 := NewParamChangeJSON("subspace", "key1", "", json.RawMessage(`{}`))
pcj2 := NewParamChangeJSON("subspace", "key2", "", json.RawMessage(`{}`))
pcjs := ParamChangesJSON{pcj1, pcj2}
paramChanges := pcjs.ToParamChanges()
require.Len(t, paramChanges, 2)
require.Equal(t, paramChanges[0].Subspace, pcj1.Subspace)
require.Equal(t, paramChanges[0].Key, pcj1.Key)
require.Equal(t, paramChanges[0].Subkey, pcj1.Subkey)
require.Equal(t, paramChanges[0].Value, string(pcj1.Value))
require.Equal(t, paramChanges[1].Subspace, pcj2.Subspace)
require.Equal(t, paramChanges[1].Key, pcj2.Key)
require.Equal(t, paramChanges[1].Subkey, pcj2.Subkey)
require.Equal(t, paramChanges[1].Value, string(pcj2.Value))
}