-
Notifications
You must be signed in to change notification settings - Fork 2
/
encode.go
159 lines (143 loc) · 3.3 KB
/
encode.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package codec
import (
"bytes"
"runtime"
"reflect"
"encoding/binary"
"fmt"
)
func Encode(v interface {}) ([]byte, error) {
en := new(encoder)
en.buf = new(bytes.Buffer)
if err := en.marshal(v); err != nil {
return nil, err
}
return en.buf.Bytes(), nil
}
func reflectValue(v interface {}) reflect.Value {
var val reflect.Value
if reflect.TypeOf(v).Kind() == reflect.Ptr {
val = reflect.ValueOf(v).Elem()
} else {
val = reflect.ValueOf(v)
}
return val
}
type encoder struct {
buf *bytes.Buffer
}
func (this *encoder) marshal(v interface {}) (err error) {
defer func() {
if r := recover(); r != nil {
if _, ok := r.(runtime.Error); ok {
panic(r)
}
if s, ok := r.(string); ok {
panic(s)
}
err = r.(error)
}
}()
this.reflect(v)
return nil
}
func (this *encoder) reflect(v interface {}) {
rv := reflectValue(v)
rvCount := rv.NumField()
for i := 0; i < rvCount; i++ {
this.encode(rv.Field(i))
}
}
func (this *encoder) encode(rv reflect.Value) {
switch rv.Kind() {
case reflect.Array:
this.encodeArray(rv)
case reflect.Struct:
this.encodeStruct(rv)
case reflect.Slice:
this.encodeSlice(rv)
case reflect.String:
this.encodeString(rv)
default:
this.encodeValue(rv)
}
}
func (this *encoder) encodeArray(rv reflect.Value) {
rvLen := rv.Len()
if rvLen <= 0 {
return
}
firstElem := rv.Index(0)
switch firstElem.Kind() {
case reflect.Struct:
for i:= 0; i < rvLen; i++ {
this.encodeStruct(rv.Index(i))
}
case reflect.Uint8:
valArr := make([]byte, rvLen)
for i := 0; i < rvLen; i++ {
valArr[i] = byte(rv.Index(i).Uint())
}
this.buf.Write(valArr)
case reflect.String:
this.encodeString(rv)
}
}
func (this *encoder) encodeStruct(rv reflect.Value) {
rvCount := rv.NumField()
for i := 0; i < rvCount; i++ {
this.encode(rv.Field(i))
}
}
func (this *encoder) encodeSlice(rv reflect.Value) {
rvLen := uint32(rv.Len())
binary.Write(this.buf, binary.LittleEndian, rvLen)
this.encodeArray(rv)
}
func (this *encoder) encodeString(rv reflect.Value) {
strBytes := []byte(rv.String())
binary.Write(this.buf, binary.LittleEndian, uint32(len(strBytes)))
this.buf.Write(strBytes)
}
func (this *encoder) encodeValue(rv reflect.Value) {
switch rv.Kind() {
case reflect.Bool:
var val uint8
if rv.Bool() {
val = uint8(1)
}
binary.Write(this.buf, binary.LittleEndian, val)
case reflect.Int8:
val := int8(rv.Int())
binary.Write(this.buf, binary.LittleEndian, val)
case reflect.Uint8:
val := uint8(rv.Uint())
binary.Write(this.buf, binary.LittleEndian, val)
case reflect.Int16:
val := int16(rv.Int())
binary.Write(this.buf, binary.LittleEndian, val)
case reflect.Uint16:
val := uint16(rv.Uint())
binary.Write(this.buf, binary.LittleEndian, val)
case reflect.Int32:
val := int32(rv.Int())
binary.Write(this.buf, binary.LittleEndian, val)
case reflect.Uint32:
val := uint32(rv.Uint())
binary.Write(this.buf, binary.LittleEndian, val)
case reflect.Float32:
val := float32(rv.Float())
binary.Write(this.buf, binary.LittleEndian, val)
case reflect.Int64:
val := rv.Int()
binary.Write(this.buf, binary.LittleEndian, val)
case reflect.Uint64:
val := rv.Uint()
binary.Write(this.buf, binary.LittleEndian, val)
default:
this.error(fmt.Errorf("Can't encode %v type.", rv.Kind()))
}
}
func (this *encoder) error(err error) {
panic(err)
}