forked from casper-ecosystem/casper-golang-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clmap.go
52 lines (42 loc) · 951 Bytes
/
clmap.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package types
import (
"encoding/hex"
"io"
"github.com/Ammer-Tech/casper-golang-sdk/serialization"
)
type CLMap struct {
KeyType CLType
ValueType CLType
// key of the map is encoded CLValue of the KeyType type
Raw map[string]CLValue
}
func (clmap CLMap) Marshal(w io.Writer) (int, error) {
size := len(clmap.Raw)
bytes := make([]byte, 0)
sizeBytes, err := serialization.Marshal(int32(size))
if err != nil {
return 0, err
}
bytes = append(bytes, sizeBytes...)
for k, v := range clmap.Raw {
var keysBytes []byte
if clmap.KeyType == CLTypeString {
keysBytes, err = serialization.Marshal(k)
if err != nil {
return 0, err
}
} else {
keysBytes, err = hex.DecodeString(k)
if err != nil {
return 0, err
}
}
valueBytes, err := serialization.Marshal(v)
if err != nil {
return 0, err
}
bytes = append(bytes, keysBytes...)
bytes = append(bytes, valueBytes...)
}
return w.Write(bytes)
}