refactor: change marshaling

This commit is contained in:
Derrick Hammer 2024-01-04 08:19:26 -05:00
parent 05fb104990
commit 722dd7d014
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
3 changed files with 55 additions and 26 deletions

View File

@ -7,6 +7,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"git.lumeweb.com/LumeWeb/libs5-go/internal/bases" "git.lumeweb.com/LumeWeb/libs5-go/internal/bases"
"git.lumeweb.com/LumeWeb/libs5-go/serialize"
"git.lumeweb.com/LumeWeb/libs5-go/types" "git.lumeweb.com/LumeWeb/libs5-go/types"
"git.lumeweb.com/LumeWeb/libs5-go/utils" "git.lumeweb.com/LumeWeb/libs5-go/utils"
"github.com/vmihailenco/msgpack/v5" "github.com/vmihailenco/msgpack/v5"
@ -25,6 +26,7 @@ type CID struct {
} }
var _ json.Marshaler = (*CID)(nil) var _ json.Marshaler = (*CID)(nil)
var _ json.Unmarshaler = (*CID)(nil)
var _ msgpack.CustomEncoder = (*CID)(nil) var _ msgpack.CustomEncoder = (*CID)(nil)
var _ msgpack.CustomDecoder = (*CID)(nil) var _ msgpack.CustomDecoder = (*CID)(nil)
@ -201,17 +203,32 @@ func (cid *CID) HashCode() int {
int(fullBytes[3])<<24 int(fullBytes[3])<<24
} }
func (cid CID) MarshalJSON() ([]byte, error) { func (b CID) MarshalJSON() ([]byte, error) {
// Delegate to the MarshalJSON method of the encoder url, err := b.ToBase64Url()
return json.Marshal(cid.Multibase) if err != nil {
return nil, err
}
return json.Marshal(url)
} }
func (cid *CID) UnmarshalJSON(data []byte) error { func (cid *CID) UnmarshalJSON(data []byte) error {
if err := json.Unmarshal(data, &cid.Multibase); err != nil { decData, err := serialize.UnmarshalBase64UrlJSON(data)
if err != nil {
return err return err
} }
decodedCid, err := CIDFromBytes(decData)
if err != nil {
return err
}
*cid = *decodedCid
return nil return nil
} }
func (cid CID) EncodeMsgpack(enc *msgpack.Encoder) error { func (cid CID) EncodeMsgpack(enc *msgpack.Encoder) error {
return enc.EncodeBytes(cid.ToBytes()) return enc.EncodeBytes(cid.ToBytes())
} }

View File

@ -1,7 +1,9 @@
package encoding package encoding
import ( import (
"encoding/json"
"errors" "errors"
"git.lumeweb.com/LumeWeb/libs5-go/serialize"
"git.lumeweb.com/LumeWeb/libs5-go/types" "git.lumeweb.com/LumeWeb/libs5-go/types"
"git.lumeweb.com/LumeWeb/libs5-go/utils" "git.lumeweb.com/LumeWeb/libs5-go/utils"
"github.com/vmihailenco/msgpack/v5" "github.com/vmihailenco/msgpack/v5"
@ -19,6 +21,8 @@ type EncryptedCID struct {
var _ msgpack.CustomEncoder = (*EncryptedCID)(nil) var _ msgpack.CustomEncoder = (*EncryptedCID)(nil)
var _ msgpack.CustomDecoder = (*EncryptedCID)(nil) var _ msgpack.CustomDecoder = (*EncryptedCID)(nil)
var _ json.Marshaler = (*EncryptedCID)(nil)
var _ json.Unmarshaler = (*EncryptedCID)(nil)
func NewEncryptedCID(encryptedBlobHash Multihash, originalCID CID, encryptionKey []byte, padding uint32, chunkSizeAsPowerOf2 int, encryptionAlgorithm byte) *EncryptedCID { func NewEncryptedCID(encryptedBlobHash Multihash, originalCID CID, encryptionKey []byte, padding uint32, chunkSizeAsPowerOf2 int, encryptionAlgorithm byte) *EncryptedCID {
e := &EncryptedCID{ e := &EncryptedCID{
@ -79,10 +83,36 @@ func (c *EncryptedCID) ToBytes() []byte {
data = append(data, c.OriginalCID.ToBytes()...) data = append(data, c.OriginalCID.ToBytes()...)
return data return data
} }
func (cid EncryptedCID) EncodeMsgpack(enc *msgpack.Encoder) error { func (c EncryptedCID) EncodeMsgpack(enc *msgpack.Encoder) error {
return enc.EncodeBytes(cid.ToBytes()) return enc.EncodeBytes(c.ToBytes())
} }
func (cid *EncryptedCID) DecodeMsgpack(dec *msgpack.Decoder) error { func (c *EncryptedCID) DecodeMsgpack(dec *msgpack.Decoder) error {
return decodeMsgpackCID(cid, dec) return decodeMsgpackCID(c, dec)
}
func (c EncryptedCID) MarshalJSON() ([]byte, error) {
str, err := c.ToString()
if err != nil {
return nil, err
}
// Delegate to the MarshalJSON method of the encoder
return json.Marshal(str)
}
func (c *EncryptedCID) UnmarshalJSON(data []byte) error {
decData, err := serialize.UnmarshalBase64UrlJSON(data)
if err != nil {
return err
}
decodedCid, err := EncryptedCIDFromBytes(decData)
if err != nil {
return err
}
*c = *decodedCid
return nil
} }

View File

@ -1,11 +1,9 @@
package encoding package encoding
import ( import (
"encoding/json"
"errors" "errors"
"git.lumeweb.com/LumeWeb/libs5-go/internal/bases" "git.lumeweb.com/LumeWeb/libs5-go/internal/bases"
"github.com/multiformats/go-multibase" "github.com/multiformats/go-multibase"
"github.com/vmihailenco/msgpack/v5"
) )
var ( var (
@ -22,8 +20,6 @@ type multibaseImpl struct {
encoder Encoder encoder Encoder
} }
var _ json.Marshaler = (*multibaseImpl)(nil)
type Multibase interface { type Multibase interface {
ToHex() (string, error) ToHex() (string, error)
ToBase32() (string, error) ToBase32() (string, error)
@ -33,7 +29,6 @@ type Multibase interface {
} }
var _ Multibase = (*multibaseImpl)(nil) var _ Multibase = (*multibaseImpl)(nil)
var _ msgpack.CustomEncoder = (*multibaseImpl)(nil)
func NewMultibase(encoder Encoder) Multibase { func NewMultibase(encoder Encoder) Multibase {
return &multibaseImpl{encoder: encoder} return &multibaseImpl{encoder: encoder}
@ -75,16 +70,3 @@ func (m *multibaseImpl) ToBase58() (string, error) {
func (m *multibaseImpl) ToString() (string, error) { func (m *multibaseImpl) ToString() (string, error) {
return m.ToBase58() return m.ToBase58()
} }
func (b multibaseImpl) MarshalJSON() ([]byte, error) {
url, err := b.ToBase64Url()
if err != nil {
return nil, err
}
return []byte(url), nil
}
func (b multibaseImpl) EncodeMsgpack(enc *msgpack.Encoder) error {
return enc.EncodeBytes(b.encoder.ToBytes())
}