-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecode.go
131 lines (115 loc) · 2.88 KB
/
decode.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package bb
import (
"bytes"
"encoding/binary"
"io"
"reflect"
)
// Unmarshall reads marshalled representation of struct v from a byte slice.
//
// # v must be pointer to struct
func Unmarshall(data []byte, v interface{}) error {
buf := bytes.NewBuffer(data)
valPtr := reflect.ValueOf(v)
if valPtr.Kind() != reflect.Ptr || valPtr.IsNil() {
return ErrInvalidType
}
val := valPtr.Elem()
if val.Kind() != reflect.Struct {
return ErrInvalidType
}
for i := 0; i < val.NumField(); i++ {
field := val.Field(i)
if !field.CanSet() {
// Skip unexported fields
continue
}
if err := decodeField(buf, field); err != nil {
return err
}
}
return nil
}
func decodeField(buf *bytes.Buffer, field reflect.Value) error {
switch field.Type().Kind() {
// --- String ---
case reflect.String:
str, err := decodeStr(buf)
if err != nil {
return err
}
field.SetString(str)
// --- Bool ---
case reflect.Bool:
b, err := decodeBool(buf)
if err != nil {
return err
}
field.SetBool(b)
// --- Float, Int, Uint ---
case reflect.Float32, reflect.Float64,
reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
if err := binary.Read(buf, binary.BigEndian, field.Addr().Interface()); err != nil {
return err
}
// --- Slice/Array ---
case reflect.Array, reflect.Slice:
if err := decodeArraySlice(buf, field); err != nil {
return err
}
// --- Struct ---
case reflect.Struct:
for i := 0; i < field.NumField(); i++ {
if err := decodeField(buf, field.Field(i)); err != nil {
return err
}
}
default:
return ErrInvalidValue
}
return nil
}
func decodeArraySlice(buf *bytes.Buffer, field reflect.Value) error {
length, err := decodeSize(buf)
if err != nil {
return err
}
// Make a new slice with the given length and the same type as field's slice or array
sliceType := reflect.SliceOf(field.Type().Elem())
newSlice := reflect.MakeSlice(sliceType, int(length), int(length))
for i := 0; i < int(length); i++ {
elem := reflect.New(field.Type().Elem()).Elem() // Create a new element of the slice type
if err := decodeField(buf, elem); err != nil {
return err
}
newSlice.Index(i).Set(elem) // Set the decoded element to the slice
}
// Set the newly created slice to the field
field.Set(newSlice)
return nil
}
func decodeStr(buf *bytes.Buffer) (string, error) {
length, err := decodeSize(buf)
if err != nil {
return "", err
}
strBytes := make([]byte, length)
if _, err := io.ReadFull(buf, strBytes); err != nil {
return "", err
}
return string(strBytes), nil
}
func decodeBool(buf *bytes.Buffer) (bool, error) {
var b [1]byte
_, err := buf.Read(b[:])
if err != nil {
return false, err
}
return b[0] != 0, nil
}
func decodeSize(buf *bytes.Buffer) (uint32, error) {
var i uint32
err := binary.Read(buf, binary.BigEndian, &i)
return i, err
}