2022-04-22 13:22:40 +00:00
|
|
|
package pilorama
|
|
|
|
|
|
|
|
import "github.com/nspcc-dev/neo-go/pkg/io"
|
|
|
|
|
|
|
|
func (x *Meta) FromBytes(data []byte) error {
|
|
|
|
if len(data) == 0 {
|
|
|
|
x.Items = nil
|
|
|
|
x.Time = 0
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
r := io.NewBinReaderFromBuf(data)
|
2022-05-28 09:39:37 +00:00
|
|
|
x.DecodeBinary(r)
|
|
|
|
return r.Err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x Meta) Bytes() []byte {
|
|
|
|
w := io.NewBufBinWriter()
|
|
|
|
x.EncodeBinary(w.BinWriter)
|
|
|
|
return w.Bytes()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (x Meta) GetAttr(name string) []byte {
|
2024-07-10 06:30:01 +00:00
|
|
|
return findAttr(x.Items, name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func findAttr(ms []KeyValue, name string) []byte {
|
|
|
|
for _, kv := range ms {
|
2022-05-28 09:39:37 +00:00
|
|
|
if kv.Key == name {
|
|
|
|
return kv.Value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DecodeBinary implements the io.Serializable interface.
|
|
|
|
func (x *Meta) DecodeBinary(r *io.BinReader) {
|
2022-04-22 13:22:40 +00:00
|
|
|
ts := r.ReadVarUint()
|
|
|
|
size := r.ReadVarUint()
|
|
|
|
m := make([]KeyValue, size)
|
|
|
|
for i := range m {
|
|
|
|
m[i].Key = r.ReadString()
|
|
|
|
m[i].Value = r.ReadVarBytes()
|
|
|
|
}
|
|
|
|
if r.Err != nil {
|
2022-05-28 09:39:37 +00:00
|
|
|
return
|
2022-04-22 13:22:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
x.Time = ts
|
|
|
|
x.Items = m
|
|
|
|
}
|
|
|
|
|
2022-05-28 09:39:37 +00:00
|
|
|
// EncodeBinary implements the io.Serializable interface.
|
|
|
|
func (x Meta) EncodeBinary(w *io.BinWriter) {
|
2022-04-22 13:22:40 +00:00
|
|
|
w.WriteVarUint(x.Time)
|
|
|
|
w.WriteVarUint(uint64(len(x.Items)))
|
|
|
|
for _, e := range x.Items {
|
|
|
|
w.WriteString(e.Key)
|
|
|
|
w.WriteVarBytes(e.Value)
|
|
|
|
}
|
2022-05-28 09:39:37 +00:00
|
|
|
}
|
2022-04-22 13:22:40 +00:00
|
|
|
|
2022-05-28 09:39:37 +00:00
|
|
|
// Size returns size of x in bytes.
|
|
|
|
func (x Meta) Size() int {
|
|
|
|
size := getVarIntSize(x.Time)
|
|
|
|
size += getVarIntSize(uint64(len(x.Items)))
|
|
|
|
for i := range x.Items {
|
|
|
|
ln := len(x.Items[i].Key)
|
|
|
|
size += getVarIntSize(uint64(ln)) + ln
|
|
|
|
|
|
|
|
ln = len(x.Items[i].Value)
|
|
|
|
size += getVarIntSize(uint64(ln)) + ln
|
|
|
|
}
|
|
|
|
return size
|
2022-04-22 13:22:40 +00:00
|
|
|
}
|
|
|
|
|
2022-05-28 09:39:37 +00:00
|
|
|
// getVarIntSize returns the size in number of bytes of a variable integer.
|
|
|
|
// (reference: GetVarSize(int value), https://github.com/neo-project/neo/blob/master/neo/IO/Helper.cs)
|
|
|
|
func getVarIntSize(value uint64) int {
|
|
|
|
var size int
|
|
|
|
|
|
|
|
if value < 0xFD {
|
|
|
|
size = 1 // unit8
|
|
|
|
} else if value <= 0xFFFF {
|
|
|
|
size = 3 // byte + uint16
|
|
|
|
} else {
|
|
|
|
size = 5 // byte + uint32
|
2022-04-22 13:22:40 +00:00
|
|
|
}
|
2022-05-28 09:39:37 +00:00
|
|
|
return size
|
2022-04-22 13:22:40 +00:00
|
|
|
}
|