From 4457dff4153d6dc3c8897e3bc2cfb1ab5393dc57 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Thu, 4 Jan 2024 04:19:24 -0500 Subject: [PATCH] feat: add json marshal support to encoding --- encoding/cid.go | 14 ++++++++++++++ encoding/multibase.go | 12 ++++++++++++ encoding/multihash.go | 23 +++++++++++++++++++++++ 3 files changed, 49 insertions(+) diff --git a/encoding/cid.go b/encoding/cid.go index 34cd93e..ba2d56e 100644 --- a/encoding/cid.go +++ b/encoding/cid.go @@ -3,6 +3,7 @@ package encoding import ( "bytes" "encoding/hex" + "encoding/json" "errors" "fmt" "git.lumeweb.com/LumeWeb/libs5-go/internal/bases" @@ -22,6 +23,8 @@ type CID struct { Size uint32 } +var _ json.Marshaler = (*CID)(nil) + func NewCID(Type types.CIDType, Hash Multihash, Size uint32) *CID { c := &CID{ Type: Type, @@ -195,6 +198,17 @@ func (cid *CID) HashCode() int { int(fullBytes[3])<<24 } +func (cid CID) MarshalJSON() ([]byte, error) { + // Delegate to the MarshalJSON method of the encoder + return json.Marshal(cid.Multibase) +} + +func (cid *CID) UnmarshalJSON(data []byte) error { + if err := json.Unmarshal(data, &cid.Multibase); err != nil { + return err + } + return nil +} func CIDFromRegistryPublicKey(pubkey interface{}) (*CID, error) { return CIDFromHash(pubkey, 0, types.CIDTypeResolver) } diff --git a/encoding/multibase.go b/encoding/multibase.go index f0a11cd..6ba4300 100644 --- a/encoding/multibase.go +++ b/encoding/multibase.go @@ -1,6 +1,7 @@ package encoding import ( + "encoding/json" "errors" "git.lumeweb.com/LumeWeb/libs5-go/internal/bases" "github.com/multiformats/go-multibase" @@ -20,6 +21,8 @@ type multibaseImpl struct { encoder Encoder } +var _ json.Marshaler = (*multibaseImpl)(nil) + type Multibase interface { ToHex() (string, error) ToBase32() (string, error) @@ -70,3 +73,12 @@ func (m *multibaseImpl) ToBase58() (string, error) { func (m *multibaseImpl) ToString() (string, error) { return m.ToBase58() } +func (b multibaseImpl) MarshalJSON() ([]byte, error) { + url, err := b.ToBase64Url() + if err != nil { + return nil, err + } + + return []byte(url), nil + +} diff --git a/encoding/multihash.go b/encoding/multihash.go index 2ea4354..81cf1fb 100644 --- a/encoding/multihash.go +++ b/encoding/multihash.go @@ -2,6 +2,7 @@ package encoding import ( "bytes" + "encoding/json" "errors" "fmt" "git.lumeweb.com/LumeWeb/libs5-go/internal/bases" @@ -21,6 +22,9 @@ type Multihash struct { FullBytes []byte } +var _ json.Marshaler = (*Multihash)(nil) +var _ json.Unmarshaler = (*Multihash)(nil) + func NewMultihash(fullBytes []byte) *Multihash { return &Multihash{FullBytes: fullBytes} } @@ -71,6 +75,25 @@ func (m *Multihash) HashCode() MultihashCode { return utils.HashCode(m.FullBytes[:4]) } +func (b *Multihash) UnmarshalJSON(data []byte) error { + decodedData, err := MultibaseDecodeString(string(data)) + if err != nil { + return err + } + + b.FullBytes = decodedData + return nil +} +func (b Multihash) MarshalJSON() ([]byte, error) { + url, err := b.ToBase64Url() + if err != nil { + return nil, err + } + + return []byte(url), nil + +} + func getEncoding(hash string) (multibase.Encoding, error) { r, _ := utf8.DecodeRuneInString(hash) enc := multibase.Encoding(r)