Skip to content

Commit

Permalink
Merge pull request #77 from make-software/bugfix/CMW-809/map_cltype_p…
Browse files Browse the repository at this point in the history
…rint

Fix Map CLType json encode issue (CMW-809)
  • Loading branch information
koltsov-iv authored Feb 1, 2024
2 parents b1f5443 + 5ad785f commit b0ce6d8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
16 changes: 16 additions & 0 deletions tests/types/cl_value/map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ package cl_value
import (
"bytes"
"encoding/hex"
"encoding/json"
"math/big"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/make-software/casper-go-sdk/casper"
"github.com/make-software/casper-go-sdk/types"
"github.com/make-software/casper-go-sdk/types/clvalue"
"github.com/make-software/casper-go-sdk/types/clvalue/cltype"
"github.com/make-software/casper-go-sdk/types/key"
Expand Down Expand Up @@ -73,3 +76,16 @@ func Test_NewMapFromBuffer_IncompleteFormat_ShouldRaiseError(t *testing.T) {
_, err = clvalue.NewMapFromBuffer(bytes.NewBuffer(inBytes), cltype.NewMap(cltype.String, cltype.String))
assert.Error(t, err)
}

func Test_ArgsWriter_MapFromRawJson(t *testing.T) {
source := `{"cl_type":{"Map":{"value":"U32","key":"String"}},"bytes":"01000000030000004f4e4502000000"}`
clValue, err := types.ArgsFromRawJson(json.RawMessage(source))
require.NoError(t, err)
args := &casper.Args{}
args.AddArgument("test", clValue)
oneArg, err := args.Find("test")
require.NoError(t, err)
res, err := oneArg.MarshalJSON()
require.NoError(t, err)
assert.JSONEq(t, source, string(res))
}
9 changes: 8 additions & 1 deletion types/clvalue/cltype/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,14 @@ func (m *Map) Name() TypeName {
}

func (m *Map) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]CLType{m.Key.String(): m.Val})
type mapTypeInner struct {
Key CLType `json:"key"`
Value CLType `json:"value"`
}
type mapJsonView struct {
Map mapTypeInner `json:"Map"`
}
return json.Marshal(mapJsonView{Map: mapTypeInner{Key: m.Key, Value: m.Val}})
}

func NewMap(keyType CLType, valType CLType) *Map {
Expand Down

0 comments on commit b0ce6d8

Please sign in to comment.