Skip to content

Commit

Permalink
Add pretty print option
Browse files Browse the repository at this point in the history
Signed-off-by: Peter Broadhurst <[email protected]>
  • Loading branch information
peterbroadhurst committed Oct 20, 2024
1 parent ee4f1b4 commit 3c690d6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
19 changes: 14 additions & 5 deletions pkg/abi/outputserialization.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ import (
// Serializer contains a set of options for how to serialize an parsed
// ABI value tree, into JSON.
type Serializer struct {
ts FormattingMode
is IntSerializer
fs FloatSerializer
bs ByteSerializer
dn DefaultNameGenerator
ts FormattingMode
is IntSerializer
fs FloatSerializer
bs ByteSerializer
dn DefaultNameGenerator
pretty bool
}

// NewSerializer creates a new ABI value tree serializer, with the default
Expand Down Expand Up @@ -106,6 +107,11 @@ func (s *Serializer) SetDefaultNameGenerator(dn DefaultNameGenerator) *Serialize
return s
}

func (s *Serializer) SetPretty(pretty bool) *Serializer {
s.pretty = pretty
return s
}

func Base10StringIntSerializer(i *big.Int) interface{} {
return i.String()
}
Expand Down Expand Up @@ -177,6 +183,9 @@ func (s *Serializer) SerializeJSONCtx(ctx context.Context, cv *ComponentValue) (
if err != nil {
return nil, err
}
if s.pretty {
return json.MarshalIndent(&v, "", " ")
}
return json.Marshal(&v)
}

Expand Down
2 changes: 2 additions & 0 deletions pkg/abi/outputserialization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func TestJSONSerializationFormatsTuple(t *testing.T) {
SetFormattingMode(FormatAsFlatArrays).
SetIntSerializer(HexIntSerializer0xPrefix).
SetByteSerializer(HexByteSerializer0xPrefix).
SetPretty(true).
SerializeJSON(v)
assert.NoError(t, err)
assert.JSONEq(t, `[
Expand All @@ -71,6 +72,7 @@ func TestJSONSerializationFormatsTuple(t *testing.T) {
"0xfeedbeef"
]
]`, string(j2))
assert.Contains(t, string(j2), "\n")

j3, err := NewSerializer().
SetFormattingMode(FormatAsSelfDescribingArrays).
Expand Down

0 comments on commit 3c690d6

Please sign in to comment.