From c24e1c408a86f904747f4b36c8ba58c78b90b839 Mon Sep 17 00:00:00 2001 From: mike seiler Date: Sat, 3 Dec 2022 22:04:05 -0700 Subject: [PATCH 1/2] fields must be capital letter fields --- pinner/pinmgr.go | 10 +++++----- pinner/pinmgr_test.go | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/pinner/pinmgr.go b/pinner/pinmgr.go index 2848d9b8..9bc6a2e0 100644 --- a/pinner/pinmgr.go +++ b/pinner/pinmgr.go @@ -361,8 +361,8 @@ type AddrInfoString struct { Addrs []string } type PinningOperationSerialize struct { - po PinningOperation - peers []AddrInfoString + Po PinningOperation + Peers []AddrInfoString } func encode_msgpack(po *PinningOperation) ([]byte, error) { @@ -377,7 +377,7 @@ func encode_msgpack(po *PinningOperation) ([]byte, error) { newpeer := AddrInfoString{ID: peers[i].ID, Addrs: newaddrs} serialPeers = append(serialPeers, newpeer) } - savedObject := PinningOperationSerialize{po: *po, peers: serialPeers} + savedObject := PinningOperationSerialize{Po: *po, Peers: serialPeers} return msgpack.Marshal(&savedObject) } func decode_msgpack(po_bytes []byte) (*PinningOperation, error) { @@ -386,9 +386,9 @@ func decode_msgpack(po_bytes []byte) (*PinningOperation, error) { if err != nil { log.Fatal(err) } - po := next.po + po := next.Po newPeers := []*peer.AddrInfo{} - peers := next.peers + peers := next.Peers for i := 0; i < len(peers); i++ { newaddrs := []ma.Multiaddr{} for j := 0; j < len(peers[i].Addrs); j++ { diff --git a/pinner/pinmgr_test.go b/pinner/pinmgr_test.go index 92e29696..5b18c9ee 100644 --- a/pinner/pinmgr_test.go +++ b/pinner/pinmgr_test.go @@ -114,7 +114,7 @@ func TestEncodeDecode(t *testing.T) { fmt.Println(err) } peer := []*peer.AddrInfo{{ID: peer.ID("12D3KooWCsxFFH242NZ4bjRMJEVc61La6Ha4yGVNXeEEwpf8KWCX"), Addrs: []ma.Multiaddr{addr}}} - po := &PinningOperation{Peers: peer} + po := &PinningOperation{Peers: peer, Name: "pinning operation name"} bytes, err := encode_msgpack(po) if err != nil { fmt.Println(err) From 7997472d0fee1bb824b201321b93e9d5fafd7673 Mon Sep 17 00:00:00 2001 From: mike seiler Date: Sat, 3 Dec 2022 22:15:36 -0700 Subject: [PATCH 2/2] test that before and after encode / decode is same value. also restore Peer after serialize --- pinner/pinmgr.go | 4 +++- pinner/pinmgr_test.go | 6 +++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/pinner/pinmgr.go b/pinner/pinmgr.go index 9bc6a2e0..b193b0fb 100644 --- a/pinner/pinmgr.go +++ b/pinner/pinmgr.go @@ -378,7 +378,9 @@ func encode_msgpack(po *PinningOperation) ([]byte, error) { serialPeers = append(serialPeers, newpeer) } savedObject := PinningOperationSerialize{Po: *po, Peers: serialPeers} - return msgpack.Marshal(&savedObject) + bytes, err := msgpack.Marshal(&savedObject) + po.Peers = peers + return bytes, err } func decode_msgpack(po_bytes []byte) (*PinningOperation, error) { var next *PinningOperationSerialize diff --git a/pinner/pinmgr_test.go b/pinner/pinmgr_test.go index 5b18c9ee..e0149cca 100644 --- a/pinner/pinmgr_test.go +++ b/pinner/pinmgr_test.go @@ -119,10 +119,14 @@ func TestEncodeDecode(t *testing.T) { if err != nil { fmt.Println(err) } - po, err = decode_msgpack(bytes) + newpo, err := decode_msgpack(bytes) if err != nil { fmt.Println(err) } + assert.Equal(t, newpo.Name, po.Name, "name doesnt match") + assert.Equal(t, newpo.Peers[0].Addrs[0].String(), po.Peers[0].Addrs[0].String(), "addr doesnt match") + assert.Equal(t, newpo.Peers[0].ID, po.Peers[0].ID, "ID doesnt match") + }) } func newPinData(name string, userid int, contid int) PinningOperation {