From c80c99b13e535d8e17f307d73f263648e56b35b5 Mon Sep 17 00:00:00 2001 From: Denis Kirillov Date: Fri, 12 Jan 2024 10:49:12 +0300 Subject: [PATCH] [#41] chain: Fix ID serialization Signed-off-by: Denis Kirillov --- pkg/chain/chain.go | 13 +++++++++++++ pkg/chain/chain_test.go | 13 +++++++++++++ 2 files changed, 26 insertions(+) diff --git a/pkg/chain/chain.go b/pkg/chain/chain.go index 24c0afc..fec4370 100644 --- a/pkg/chain/chain.go +++ b/pkg/chain/chain.go @@ -31,6 +31,19 @@ type Chain struct { MatchType MatchType } +func (id ID) MarshalJSON() ([]byte, error) { + return json.Marshal([]byte(id)) +} + +func (id *ID) UnmarshalJSON(data []byte) error { + var idRaw []byte + if err := json.Unmarshal(data, &idRaw); err != nil { + return err + } + *id = ID(idRaw) + return nil +} + func (c *Chain) Bytes() []byte { data, err := json.Marshal(c) if err != nil { diff --git a/pkg/chain/chain_test.go b/pkg/chain/chain_test.go index 8e0c706..f050574 100644 --- a/pkg/chain/chain_test.go +++ b/pkg/chain/chain_test.go @@ -9,6 +9,19 @@ import ( "github.com/stretchr/testify/require" ) +func TestChainIDSerialization(t *testing.T) { + chainIDBytes := []byte{93, 236, 80, 138, 168, 3, 144, 92, 173, 141, 16, 42, 249, 90, 97, 109, 211, 169, 54, 163} + + chain1 := &Chain{ID: ID(chainIDBytes)} + data := chain1.Bytes() + + var chain2 Chain + err := chain2.DecodeBytes(data) + require.NoError(t, err) + + require.Equal(t, chain1.ID, chain2.ID) +} + func TestEncodeDecode(t *testing.T) { expected := Chain{ MatchType: MatchTypeFirstMatch,