Skip to content

Commit

Permalink
Merge pull request #14 from kaleido-io/output
Browse files Browse the repository at this point in the history
Allow serializer to override default name generator
  • Loading branch information
nguyer committed Jul 15, 2022
2 parents 4390cf5 + 14ec4c0 commit 0aa53d8
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
17 changes: 15 additions & 2 deletions pkg/abi/outputserialization.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type Serializer struct {
is IntSerializer
fs FloatSerializer
bs ByteSerializer
dn DefaultNameGenerator
}

// NewSerializer creates a new ABI value tree serializer, with the default
Expand All @@ -48,6 +49,7 @@ func NewSerializer() *Serializer {
is: Base10StringIntSerializer,
fs: Base10StringFloatSerializer,
bs: HexByteSerializer,
dn: NumericDefaultNameGenerator,
}
}

Expand All @@ -70,6 +72,8 @@ var (
minSafeJSONNumberFloat = big.NewFloat(-9007199254740991)
)

type DefaultNameGenerator func(idx int) string

type IntSerializer func(i *big.Int) interface{}

type FloatSerializer func(f *big.Float) interface{}
Expand All @@ -96,6 +100,11 @@ func (s *Serializer) SetByteSerializer(bs ByteSerializer) *Serializer {
return s
}

func (s *Serializer) SetDefaultNameGenerator(dn DefaultNameGenerator) *Serializer {
s.dn = dn
return s
}

func Base10StringIntSerializer(i *big.Int) interface{} {
return i.String()
}
Expand Down Expand Up @@ -133,6 +142,10 @@ func HexByteSerializer0xPrefix(b []byte) interface{} {
return "0x" + hex.EncodeToString(b)
}

func NumericDefaultNameGenerator(idx int) string {
return strconv.FormatInt(int64(idx), 10)
}

func (s *Serializer) SerializeInterface(cv *ComponentValue) (interface{}, error) {
return s.SerializeInterfaceCtx(context.Background(), cv)
}
Expand Down Expand Up @@ -210,7 +223,7 @@ func (s *Serializer) serializeTuple(ctx context.Context, breadcrumbs string, cv
if child.Component != nil {
name := child.Component.KeyName()
if name == "" {
name = strconv.FormatInt(int64(i), 10)
name = s.dn(i)
}
v, err := s.walkOutput(ctx, fmt.Sprintf("%s[%s]", breadcrumbs, name), child)
if err != nil {
Expand Down Expand Up @@ -239,7 +252,7 @@ func (s *Serializer) serializeTuple(ctx context.Context, breadcrumbs string, cv
vm["type"] = child.Component.String()
}
if vm["name"] == "" {
vm["name"] = strconv.FormatInt(int64(i), 10)
vm["name"] = s.dn(i)
}
v, err := s.walkOutput(ctx, fmt.Sprintf("%s[%s]", breadcrumbs, vm["name"]), child)
if err != nil {
Expand Down
12 changes: 10 additions & 2 deletions pkg/abi/outputserialization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"encoding/base64"
"math/big"
"strconv"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -305,16 +306,23 @@ func TestJSONSerializationFormatsAnonymousTuple(t *testing.T) {

j3, err := NewSerializer().
SetFormattingMode(FormatAsSelfDescribingArrays).
SetDefaultNameGenerator(func(idx int) string {
name := "input"
if idx > 0 {
name += strconv.Itoa(idx)
}
return name
}).
SerializeJSON(v)
assert.NoError(t, err)
assert.JSONEq(t, `[
{
"name": "0",
"name": "input",
"type": "address",
"value": "6c26465984ac94713e83300d1f002296772ebb64"
},
{
"name": "1",
"name": "input1",
"type": "uint256",
"value": "1"
}
Expand Down

0 comments on commit 0aa53d8

Please sign in to comment.