refactor: move UnmarshalBase64UrlJSON to encoding to prevent an import loop

This commit is contained in:
Derrick Hammer 2024-01-04 09:51:35 -05:00
parent 039fbc1867
commit e6034b9aae
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
3 changed files with 10 additions and 14 deletions

View File

@ -7,7 +7,6 @@ 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"
@ -213,7 +212,7 @@ func (b CID) MarshalJSON() ([]byte, error) {
} }
func (cid *CID) UnmarshalJSON(data []byte) error { func (cid *CID) UnmarshalJSON(data []byte) error {
decData, err := serialize.UnmarshalBase64UrlJSON(data) decData, err := UnmarshalBase64UrlJSON(data)
if err != nil { if err != nil {
return err return err

View File

@ -1,9 +1,6 @@
package serialize package encoding
import ( import "encoding/base64"
"encoding/base64"
"github.com/multiformats/go-multibase"
)
func UnmarshalBase64UrlJSON(data []byte) ([]byte, error) { func UnmarshalBase64UrlJSON(data []byte) ([]byte, error) {
strData := string(data) strData := string(data)
@ -15,15 +12,16 @@ func UnmarshalBase64UrlJSON(data []byte) ([]byte, error) {
return nil, nil return nil, nil
} }
if strData[0] == 'u' { decodedData, err := MultibaseDecodeString(strData)
_, decoded, err := multibase.Decode(strData)
if err != nil { if err != nil {
if err != ErrMultibaseEncodingNotSupported {
return nil, err return nil, err
} }
return decoded, nil } else {
return decodedData, nil
} }
decodedData, err := base64.RawURLEncoding.DecodeString(strData) decodedData, err = base64.RawURLEncoding.DecodeString(strData)
if err != nil { if err != nil {
return nil, err return nil, err
} }

View File

@ -3,7 +3,6 @@ package encoding
import ( import (
"encoding/json" "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"
@ -101,7 +100,7 @@ func (c EncryptedCID) MarshalJSON() ([]byte, error) {
} }
func (c *EncryptedCID) UnmarshalJSON(data []byte) error { func (c *EncryptedCID) UnmarshalJSON(data []byte) error {
decData, err := serialize.UnmarshalBase64UrlJSON(data) decData, err := UnmarshalBase64UrlJSON(data)
if err != nil { if err != nil {
return err return err