forked from apache/dubbo-go-hessian2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.go
453 lines (396 loc) · 12.1 KB
/
list.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package hessian
import (
"io"
"reflect"
"strconv"
"strings"
"sync"
"time"
)
import (
perrors "github.com/pkg/errors"
)
import (
"github.com/apache/dubbo-go-hessian2/java_exception"
)
var (
listTypeNameMapper = &sync.Map{}
listTypeMapper = map[string]reflect.Type{
"string": reflect.TypeOf(""),
"java.lang.String": reflect.TypeOf(""),
"char": reflect.TypeOf(""),
"short": reflect.TypeOf(int32(0)),
"int": reflect.TypeOf(int32(0)),
"long": reflect.TypeOf(int64(0)),
"float": reflect.TypeOf(float64(0)),
"double": reflect.TypeOf(float64(0)),
"boolean": reflect.TypeOf(true),
"java.util.Date": reflect.TypeOf(time.Time{}),
"date": reflect.TypeOf(time.Time{}),
"object": reflect.TypeOf([]Object{}).Elem(),
"java.lang.Object": reflect.TypeOf([]Object{}).Elem(),
// exception field StackTraceElement
"java.lang.StackTraceElement": reflect.TypeOf([]*java_exception.StackTraceElement{}).Elem(),
}
)
func init() {
listTypeNameMapper.Store("string", "[string")
listTypeNameMapper.Store("int8", "[short")
listTypeNameMapper.Store("int16", "[short")
listTypeNameMapper.Store("uint16", "[short")
listTypeNameMapper.Store("int32", "[int")
listTypeNameMapper.Store("uint32", "[int")
listTypeNameMapper.Store("int", "[long")
listTypeNameMapper.Store("uint", "[long")
listTypeNameMapper.Store("int64", "[long")
listTypeNameMapper.Store("uint64", "[long")
listTypeNameMapper.Store("float32", "[float")
listTypeNameMapper.Store("float64", "[double")
listTypeNameMapper.Store("bool", "[boolean")
listTypeNameMapper.Store("time.Time", "[date")
listTypeNameMapper.Store("github.com/apache/dubbo-go-hessian2/java_exception/java_exception.Throwabler", "[java.lang.Throwable")
listTypeNameMapper.Store("github.com/apache/dubbo-go-hessian2/hessian.Object", "[object")
}
func registerListNameMapping(gotype, javaType string) {
listTypeNameMapper.Store(gotype, "["+javaType)
}
func getListTypeName(goType string) string {
buf := strings.Builder{}
count := strings.Count(goType, "[]")
if count > 0 {
for i := 1; i < count; i++ {
buf.WriteString("[")
}
goType = strings.Replace(goType, "[]", "", -1)
}
v, ok := listTypeNameMapper.Load(goType)
if !ok {
v, ok = listTypeNameMapper.Load(strings.TrimPrefix(goType, "*"))
}
if ok {
buf.WriteString(v.(string))
return buf.String()
}
return ""
}
func getListType(javaListName string) reflect.Type {
javaName := javaListName
if strings.Index(javaName, "[") == 0 {
javaName = javaName[1:]
}
if strings.Index(javaName, "[") == 0 {
lt := getListType(javaName)
if lt == nil {
return nil
}
return reflect.SliceOf(lt)
}
var sliceTy reflect.Type
ltm := listTypeMapper[javaName]
if ltm != nil {
sliceTy = reflect.SliceOf(ltm)
}
if sliceTy == nil {
tpStructInfo, _ := getStructInfo(javaName)
if tpStructInfo == nil || tpStructInfo.typ == nil {
return nil
}
tp := tpStructInfo.typ
if tp.Kind() != reflect.Ptr {
tp = reflect.New(tp).Type()
}
sliceTy = reflect.SliceOf(tp)
}
return sliceTy
}
// Object is equal to Object of java When encoding
type Object interface{}
/////////////////////////////////////////
// List
/////////////////////////////////////////
// encList write list
func (e *Encoder) encList(v interface{}) error {
if !strings.Contains(reflect.TypeOf(v).String(), "interface {}") {
return e.writeTypedList(v)
}
return e.writeUntypedList(v)
}
// writeTypedList write typed list
// Include 3 formats:
// ::= x55 type value* 'Z' # variable-length list
// ::= 'V' type int value* # fixed-length list
// ::= [x70-77] type value* # fixed-length typed list
func (e *Encoder) writeTypedList(v interface{}) error {
var err error
value := reflect.ValueOf(v)
// https://github.com/apache/dubbo-go-hessian2/issues/317
// if list is null, just return 'N'
if value.IsNil() {
e.buffer = encByte(e.buffer, BC_NULL) // 'N'
return nil
}
// check ref
if n, ok := e.checkRefMap(value); ok {
e.buffer = encRef(e.buffer, n)
return nil
}
// try to get list type name of the value type first, for hessian support pointer type.
// if not found, try to get list type name of the unpacked type.
typeName := getListTypeName(value.Type().String())
if typeName == "" {
value = UnpackPtrValue(value)
goType := UnpackPtrType(value.Type())
toType := combineGoTypeName(goType)
typeName = getListTypeName(toType)
if typeName == "" {
return perrors.New("no this type name: " + toType)
}
}
e.buffer = encByte(e.buffer, BC_LIST_FIXED) // 'V'
e.buffer = encString(e.buffer, typeName)
e.buffer = encInt32(e.buffer, int32(value.Len()))
for i := 0; i < value.Len(); i++ {
if err = e.Encode(value.Index(i).Interface()); err != nil {
return err
}
}
return nil
}
// writeUntypedList write untyped list
// Include 3 formats:
// ::= x57 value* 'Z' # variable-length untyped list
// ::= x58 int value* # fixed-length untyped list
// ::= [x78-7f] value* # fixed-length untyped list
func (e *Encoder) writeUntypedList(v interface{}) error {
var err error
value := reflect.ValueOf(v)
// check ref
if n, ok := e.checkRefMap(value); ok {
e.buffer = encRef(e.buffer, n)
return nil
}
value = UnpackPtrValue(value)
e.buffer = encByte(e.buffer, BC_LIST_FIXED_UNTYPED) // x58
e.buffer = encInt32(e.buffer, int32(value.Len()))
for i := 0; i < value.Len(); i++ {
if err = e.Encode(value.Index(i).Interface()); err != nil {
return err
}
}
return nil
}
/////////////////////////////////////////
// List
/////////////////////////////////////////
// # list/vector
// ::= x55 type value* 'Z' # variable-length list
// ::= 'V' type int value* # fixed-length list
// ::= x57 value* 'Z' # variable-length untyped list
// ::= x58 int value* # fixed-length untyped list
// ::= [x70-77] type value* # fixed-length typed list
// ::= [x78-7f] value* # fixed-length untyped list
func (d *Decoder) readBufByte() (byte, error) {
var (
err error
buf [1]byte
)
_, err = io.ReadFull(d.reader, buf[:1])
if err != nil {
return 0, perrors.WithStack(err)
}
return buf[0], nil
}
func listFixedTypedLenTag(tag byte) bool {
return tag >= _listFixedTypedLenTagMin && tag <= _listFixedTypedLenTagMax
}
// list include 3 formats:
// ::= x55 type value* 'Z' # variable-length list
// ::= 'V' type int value* # fixed-length list
// ::= [x70-77] type value* # fixed-length typed list
func typedListTag(tag byte) bool {
return tag == BC_LIST_FIXED || tag == BC_LIST_VARIABLE || listFixedTypedLenTag(tag)
}
func listFixedUntypedLenTag(tag byte) bool {
return tag >= _listFixedUntypedLenTagMin && tag <= _listFixedUntypedLenTagMax
}
// include 3 formats:
// ::= x57 value* 'Z' # variable-length untyped list
// ::= x58 int value* # fixed-length untyped list
// ::= [x78-7f] value* # fixed-length untyped list
func untypedListTag(tag byte) bool {
return tag == BC_LIST_FIXED_UNTYPED || tag == BC_LIST_VARIABLE_UNTYPED || listFixedUntypedLenTag(tag)
}
// decList read list
func (d *Decoder) decList(flag int32) (interface{}, error) {
var (
err error
tag byte
)
if flag != TAG_READ {
tag = byte(flag)
} else {
tag, err = d.ReadByte()
if err != nil {
return nil, perrors.WithStack(err)
}
}
switch {
case tag == BC_NULL:
return nil, nil
case tag == BC_REF:
return d.decRef(int32(tag))
case typedListTag(tag):
return d.readTypedList(tag)
case untypedListTag(tag):
return d.readUntypedList(tag)
case binaryTag(tag):
return d.decBinary(int32(tag))
default:
return nil, perrors.Errorf("error list tag: 0x%x", tag)
}
}
// readTypedList read typed list
// list include 3 formats:
// ::= x55 type value* 'Z' # variable-length list
// ::= 'V' type int value* # fixed-length list
// ::= [x70-77] type value* # fixed-length typed list
func (d *Decoder) readTypedList(tag byte) (interface{}, error) {
listTyp, err := d.decString(TAG_READ)
if err != nil {
return nil, perrors.Errorf("error to read list type[%s]: %v", listTyp, err)
}
isVariableArr := tag == BC_LIST_VARIABLE
var length int
if listFixedTypedLenTag(tag) {
length = int(tag - _listFixedTypedLenTagMin)
} else if tag == BC_LIST_FIXED {
ii, err := d.decInt32(TAG_READ)
if err != nil {
return nil, perrors.WithStack(err)
}
length = int(ii)
} else if isVariableArr {
length = 0
} else {
return nil, perrors.Errorf("error typed list tag: 0x%x", tag)
}
if isCollectionSerialize(listTyp) {
return d.decodeCollection(length, listTyp)
}
return d.readTypedListValue(length, listTyp, isVariableArr)
}
func (d *Decoder) readTypedListValue(length int, listTyp string, isVariableArr bool) (interface{}, error) {
// return when no element
if length < 0 {
return nil, nil
}
var (
aryValue reflect.Value
arrType reflect.Type
)
t, err := strconv.Atoi(listTyp)
if err == nil {
// find the ref list type
arrType = d.typeRefs.Get(t)
if arrType == nil {
return nil, perrors.Errorf("can't find ref list type at index %d", t)
}
aryValue = reflect.MakeSlice(arrType, length, length)
} else {
// try to find the registered list type
arrType = getListType(listTyp)
if arrType != nil {
aryValue = reflect.MakeSlice(arrType, length, length)
d.typeRefs.appendTypeRefs(listTyp, arrType)
} else {
// using default generic list type if not found registered
aryValue = reflect.ValueOf(make([]interface{}, length, length))
d.typeRefs.appendTypeRefs(listTyp, aryValue.Type())
}
}
holder := d.appendRefs(aryValue)
for j := 0; j < length || isVariableArr; j++ {
it, err := d.DecodeValue()
if err != nil {
if perrors.Is(err, io.EOF) && isVariableArr {
break
}
return nil, perrors.WithStack(err)
}
if isVariableArr {
if it != nil {
aryValue = reflect.Append(aryValue, EnsureRawValue(it))
} else {
aryValue = reflect.Append(aryValue, reflect.Zero(aryValue.Type().Elem()))
}
holder.change(aryValue)
} else {
if it != nil {
//aryValue.Index(j).Set(EnsureRawValue(it))
SetValue(aryValue.Index(j), EnsureRawValue(it))
}
}
}
return holder, nil
}
// readUntypedList read untyped list
// Include 3 formats:
// ::= x57 value* 'Z' # variable-length untyped list
// ::= x58 int value* # fixed-length untyped list
// ::= [x78-7f] value* # fixed-length untyped list
func (d *Decoder) readUntypedList(tag byte) (interface{}, error) {
isVariableArr := tag == BC_LIST_VARIABLE_UNTYPED
var length int
if listFixedUntypedLenTag(tag) {
length = int(tag - _listFixedUntypedLenTagMin)
} else if tag == BC_LIST_FIXED_UNTYPED {
ii, err := d.decInt32(TAG_READ)
if err != nil {
return nil, perrors.WithStack(err)
}
length = int(ii)
} else if isVariableArr {
length = 0
} else {
return nil, perrors.Errorf("error untyped list tag: %x", tag)
}
ary := make([]interface{}, length)
aryValue := reflect.ValueOf(ary)
holder := d.appendRefs(aryValue)
for j := 0; j < length || isVariableArr; j++ {
it, err := d.DecodeValue()
if err != nil {
if perrors.Is(err, io.EOF) && isVariableArr {
break
}
return nil, perrors.WithStack(err)
}
if isVariableArr {
if it != nil {
aryValue = reflect.Append(aryValue, EnsureRawValue(it))
} else {
aryValue = reflect.Append(aryValue, reflect.Zero(aryValue.Type().Elem()))
}
holder.change(aryValue)
} else {
ary[j] = EnsureRawAny(it)
}
}
return holder, nil
}