libs5-go/metadata/misc.go

56 lines
1.0 KiB
Go
Raw Normal View History

2024-01-04 13:20:37 +00:00
package metadata
import (
"encoding/base64"
"github.com/vmihailenco/msgpack/v5"
)
type Base64UrlBinary []byte
func (b *Base64UrlBinary) UnmarshalJSON(data []byte) error {
strData := string(data)
if len(strData) >= 2 && strData[0] == '"' && strData[len(strData)-1] == '"' {
strData = strData[1 : len(strData)-1]
}
if strData == "null" {
return nil
}
decodedData, err := base64.RawURLEncoding.DecodeString(strData)
if err != nil {
return err
}
2024-01-04 13:57:58 +00:00
*b = Base64UrlBinary(decodedData)
2024-01-04 13:20:37 +00:00
return nil
}
func (b Base64UrlBinary) MarshalJSON() ([]byte, error) {
return []byte(base64.RawURLEncoding.EncodeToString(b)), nil
}
func decodeIntMap(dec *msgpack.Decoder) (map[int]interface{}, error) {
mapLen, err := dec.DecodeMapLen()
if err != nil {
return nil, err
}
data := make(map[int]interface{}, mapLen)
for i := 0; i < mapLen; i++ {
key, err := dec.DecodeInt()
if err != nil {
return nil, err
}
value, err := dec.DecodeInterface()
if err != nil {
return nil, err
}
data[key] = value
}
return data, nil
}