-
Notifications
You must be signed in to change notification settings - Fork 2
/
decode.go
235 lines (199 loc) · 5.21 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
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package fixedwidth
import (
"bytes"
"errors"
"fmt"
"reflect"
"strconv"
"unicode/utf8"
)
// Unmarshaler is the place fixed-width decoding happen
type Unmarshaler struct {
tag
}
// NewUnmarshaler create new Unmarshaler
func NewUnmarshaler() Unmarshaler {
return Unmarshaler{}
}
// Unmarshal decodes fixed-width encoding data to model,
// model is required to be a pointer.
func (m Unmarshaler) Unmarshal(data []byte, model interface{}) error {
modelType := reflect.TypeOf(model)
if modelType.Kind() != reflect.Ptr {
return errors.New("the model must be a pointer")
}
_, err := m.unmarshal(data, reflect.ValueOf(model).Elem(), reflect.TypeOf(model).Elem())
return err
}
func (m Unmarshaler) unmarshal(data []byte, modelValue reflect.Value, modelType reflect.Type) (int, error) {
if isBasicType(modelType.Kind()) {
return m.unmarshalBasicType(data, modelValue)
}
switch modelType.Kind() {
case reflect.Struct:
return m.unmarshalStruct(data, modelValue)
case reflect.Ptr:
return m.unmarshalPointer(data, modelValue, modelType)
case reflect.Slice:
return m.unmarshalSlice(data, modelValue)
case reflect.Interface:
return m.unmarshalInterface(data, modelValue)
}
return 0, nil
}
func (m Unmarshaler) unmarshalStruct(data []byte, structValue reflect.Value) (int, error) {
structType := structValue.Type()
if structType.Kind() != reflect.Struct {
return 0, errors.New("input value is not a struct")
}
index := 0
dataLen := len(data)
for i := 0; i < structValue.NumField(); i++ {
if index >= dataLen {
break
}
structField := structType.Field(i)
fieldValue := structValue.Field(i)
limit, ok := m.getLimitFixedTag(structField)
if !ok {
return 0, fmt.Errorf("invalid fixed tag of field %s", structField.Name)
}
if limit == 0 && !isStructOrStructPointer(structField.Type) {
continue
}
var uLen int
var dataChunk []byte
if limit == 0 && isStructOrStructPointer(structField.Type) {
dataChunk = data[index:]
} else {
upperBound := getUpperBound(index, limit, data)
dataChunk = data[index:upperBound]
}
uLen, err := m.unmarshal(dataChunk, fieldValue, structField.Type)
if err != nil {
return 0, err
}
index += uLen
}
return index, nil
}
func (m Unmarshaler) unmarshalBasicType(data []byte, modelValue reflect.Value) (int, error) {
l := len(data)
data = removePadding(data)
if len(data) == 0 {
return l, nil
}
modelType := modelValue.Type()
switch modelType.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
i, err := strconv.ParseInt(string(data), 10, 0)
if err != nil {
return 0, err
}
modelValue.SetInt(i)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
i, err := strconv.ParseUint(string(data), 10, 0)
if err != nil {
return 0, err
}
modelValue.SetUint(i)
case reflect.Float32, reflect.Float64:
f, err := strconv.ParseFloat(string(data), 64)
if err != nil {
return 0, err
}
modelValue.SetFloat(f)
case reflect.String:
modelValue.SetString(string(data))
}
return l, nil
}
func (m Unmarshaler) unmarshalPointer(data []byte, modelValue reflect.Value, modelType reflect.Type) (int, error) {
if modelType.Kind() != reflect.Ptr {
return 0, errors.New("invalid type")
}
newType := modelType.Elem()
newValue := reflect.New(newType)
l, err := m.unmarshal(data, newValue.Elem(), newType)
if err != nil {
return 0, err
}
modelValue.Set(newValue)
return l, nil
}
func (m Unmarshaler) unmarshalSlice(data []byte, modelValue reflect.Value) (int, error) {
modelType := modelValue.Type()
lines := bytes.Split(data, []byte("\n"))
for _, line := range lines {
newElem := reflect.New(modelType.Elem()).Elem()
_, err := m.unmarshal(line, newElem, modelType.Elem())
if err != nil {
return 0, err
}
modelValue.Set(reflect.Append(modelValue, newElem))
}
return len(data), nil
}
func (m Unmarshaler) unmarshalInterface(data []byte, modelValue reflect.Value) (int, error) {
var tempString string
newType := reflect.TypeOf(tempString)
newValue := reflect.New(newType)
l, err := m.unmarshal(data, newValue.Elem(), newType)
if err != nil {
return 0, err
}
modelValue.Set(newValue.Elem())
return l, nil
}
func removePadding(data []byte) []byte {
if len(data) == 0 {
return data
}
i := len(data) - 1
for ; i >= 0; i-- {
if data[i] == spaceByte {
continue
}
break
}
return data[:i+1]
}
func isBasicType(p reflect.Kind) bool {
basicTypes := []reflect.Kind{
reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
reflect.Float32, reflect.Float64,
reflect.String,
}
for _, t := range basicTypes {
if t == p {
return true
}
}
return false
}
func isStructOrStructPointer(t reflect.Type) bool {
if t.Kind() == reflect.Struct {
return true
}
if t.Kind() == reflect.Ptr && t.Elem().Kind() == reflect.Struct {
return true
}
return false
}
func getUpperBound(lowerBound, limit int, data []byte) int {
diff := 0
lenData := len(data)
data = data[lowerBound:]
for limit > 0 {
_, s := utf8.DecodeRune(data)
diff += s
limit--
data = data[s:]
}
upperBound := lowerBound + diff
if upperBound > lenData {
upperBound = lenData
}
return upperBound
}