libs5-go/metadata/directory_reference.go

91 lines
2.3 KiB
Go
Raw Normal View History

2024-01-04 13:20:37 +00:00
package metadata
import (
"github.com/emirpasic/gods/maps/linkedhashmap"
"github.com/vmihailenco/msgpack/v5"
)
2024-01-04 13:20:37 +00:00
var _ SerializableMetadata = (*DirectoryReference)(nil)
type DirectoryReference struct {
Created uint64 `json:"created"`
Name string `json:"name"`
EncryptedWriteKey Base64UrlBinary `json:"encryptedWriteKey,string"`
PublicKey Base64UrlBinary `json:"publicKey,string"`
EncryptionKey *Base64UrlBinary `json:"encryptionKey,string"`
2024-01-04 13:20:37 +00:00
Ext map[string]interface{} `json:"ext"`
URI string `json:"uri,omitempty"`
Key string `json:"key,omitempty"`
2024-01-04 13:20:37 +00:00
Size int64 `json:"size"`
}
func NewDirectoryReference(created uint64, name string, encryptedWriteKey, publicKey, encryptionKey Base64UrlBinary, ext map[string]interface{}) *DirectoryReference {
2024-01-04 13:20:37 +00:00
return &DirectoryReference{
Created: created,
Name: name,
EncryptedWriteKey: encryptedWriteKey,
PublicKey: publicKey,
EncryptionKey: &encryptionKey,
2024-01-04 13:20:37 +00:00
Ext: ext,
URI: "",
Key: "",
Size: 0,
}
}
func (dr *DirectoryReference) EncodeMsgpack(enc *msgpack.Encoder) error {
dmap := &directoryReferenceSerializationMap{*linkedhashmap.New()}
dmap.Put(1, dr.Name)
dmap.Put(2, dr.Created)
dmap.Put(3, dr.PublicKey)
dmap.Put(4, dr.EncryptedWriteKey)
2024-01-04 13:20:37 +00:00
if dr.EncryptionKey != nil {
dmap.Put(5, dr.EncryptionKey)
2024-01-04 13:20:37 +00:00
}
if dr.Ext != nil {
dmap.Put(6, dr.Ext)
2024-01-04 13:20:37 +00:00
}
return enc.Encode(dmap)
2024-01-04 13:20:37 +00:00
}
func (dr *DirectoryReference) DecodeMsgpack(dec *msgpack.Decoder) error {
var (
err error
l int
)
if l, err = dec.DecodeMapLen(); err != nil {
return err
}
for i := 0; i < l; i++ {
key, err := dec.DecodeInt8()
if err != nil {
return err
}
value, err := dec.DecodeInterface()
if err != nil {
return err
}
switch key {
case int8(1):
dr.Name = value.(string)
case int8(2):
dr.Created = value.(uint64)
case int8(3):
dr.PublicKey = value.([]byte)
case int8(4):
dr.EncryptedWriteKey = value.([]byte)
case int8(5):
dr.EncryptionKey = value.(*Base64UrlBinary)
2024-01-04 13:20:37 +00:00
case int8(6):
dr.Ext = value.(map[string]interface{})
}
}
return nil
}