-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmsgpack.go
201 lines (180 loc) · 4.63 KB
/
msgpack.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// Copyright 2017~2022 The Bottos Authors
// This file is part of the Bottos Chain library.
// Created by Rocket Core Team of Bottos.
//This program is free software: you can distribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.
//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//GNU General Public License for more details.
//You should have received a copy of the GNU General Public License
// along with bottos. If not, see <http://www.gnu.org/licenses/>.
/*
* file description: msgpack go
* @Author: Gong Zibin
* @Date: 2017-12-20
* @Last Modified by:
* @Last Modified time:
*/
package msgpack
import (
"bytes"
"fmt"
"io"
"reflect"
)
//Marshal is to serialize the message
func Marshal(v interface{}) ([]byte, error) {
writer := &bytes.Buffer{}
err := Encode(writer, v)
if err != nil {
return []byte{}, err
}
return writer.Bytes(), nil
}
//Unmarshal is to unserialize the message
func Unmarshal(data []byte, dst interface{}) error {
r := bytes.NewReader(data)
err := Decode(r, dst)
return err
}
//Encode is to encode message
func Encode(w io.Writer, structs interface{}) error {
v := reflect.ValueOf(structs)
if !v.IsValid() {
fmt.Printf("Not Valid %T\n", structs)
return fmt.Errorf("Not Valid %T\n", structs)
}
if v.Kind() == reflect.Ptr {
v = v.Elem()
if !v.IsValid() {
fmt.Printf("Nil Ptr: %T\n", structs)
return fmt.Errorf("Nil Ptr: %T\n", structs)
}
}
values := make([]interface{}, v.NumField())
for i := 0; i < v.NumField(); i++ {
values[i] = v.Field(i).Interface()
}
PackArraySize(w, uint16(len(values)))
for i := 0; i < len(values); i++ {
t := reflect.TypeOf(values[i])
val := reflect.ValueOf(values[i])
kind := t.Kind()
switch kind {
case reflect.String:
PackStr16(w, val.String())
case reflect.Uint8:
PackUint8(w, uint8(val.Uint()))
case reflect.Uint16:
PackUint16(w, uint16(val.Uint()))
case reflect.Uint32:
PackUint32(w, uint32(val.Uint()))
case reflect.Uint64:
PackUint64(w, uint64(val.Uint()))
case reflect.Slice: // []byte
if t.Elem().Kind() == reflect.Uint8 {
PackBin16(w, val.Bytes())
} else {
return fmt.Errorf("Unsupported Slice Type")
}
case reflect.Struct:
Encode(w, val.Interface())
case reflect.Ptr:
vvt := reflect.TypeOf(val.Elem().Interface())
if vvt.Kind() == reflect.Struct {
Encode(w, val.Elem().Interface())
} else {
return fmt.Errorf("Unsupported Type: %T", val)
}
default:
return fmt.Errorf("Unsupported Type: %v", kind)
}
}
return nil
}
//Decode is to encode message
func Decode(r io.Reader, dst interface{}) error {
v := reflect.ValueOf(dst)
if !v.IsValid() {
fmt.Printf("Not Valid %T\n", dst)
return fmt.Errorf("Not Valid %T\n", dst)
}
if v.Kind() != reflect.Ptr {
fmt.Printf("dst Not Settable %T\n", dst)
return fmt.Errorf("dst Not Settable %T)", dst)
}
if !v.Elem().IsValid() {
fmt.Printf("Nil Ptr: %T\n", dst)
return fmt.Errorf("Nil Ptr: %T\n", dst)
}
if v.Elem().NumField() > 0 {
UnpackArraySize(r)
}
for i := 0; i < v.Elem().NumField(); i++ {
feild := v.Elem().Field(i)
feildAddr := feild.Addr().Interface()
switch feild.Kind() {
case reflect.String:
val, err := UnpackStr16(r)
if err != nil {
return err
}
ptr :=feildAddr.(*string)
*ptr = val
case reflect.Uint8:
val, err := UnpackUint8(r)
if err != nil {
return err
}
ptr := feildAddr.(*uint8)
*ptr = val
case reflect.Uint16:
val, err := UnpackUint16(r)
if err != nil {
return err
}
ptr := feildAddr.(*uint16)
*ptr = val
case reflect.Uint32:
val, err := UnpackUint32(r)
if err != nil {
return err
}
ptr := feildAddr.(*uint32)
*ptr = val
case reflect.Uint64:
val, err := UnpackUint64(r)
if err != nil {
return err
}
ptr := feildAddr.(*uint64)
*ptr = val
case reflect.Slice:
if feild.Type().Elem().Kind() == reflect.Uint8 {
val, err := UnpackBin16(r)
if err != nil {
return err
}
ptr := feildAddr.(*[]byte)
*ptr = val
} else {
return fmt.Errorf("Unsupported Slice Type")
}
case reflect.Struct:
vv := feild.Interface()
if reflect.ValueOf(vv).Kind() != reflect.Ptr {
vv = feildAddr
}
Decode(r, vv)
case reflect.Ptr:
vv := feild.Interface()
Decode(r, vv)
default:
return fmt.Errorf("Unsupported Type")
}
}
return nil
}