forked from gopcua/opcua
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decode.go
165 lines (143 loc) · 3.95 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
// Copyright 2018-2020 opcua authors. All rights reserved.
// Use of this source code is governed by a MIT-style license that can be
// found in the LICENSE file.
package ua
import (
"fmt"
"math"
"reflect"
"time"
"github.com/bpizzi/opcua/errors"
)
var (
binaryDecoder = reflect.TypeOf((*BinaryDecoder)(nil)).Elem()
timeType = reflect.TypeOf(time.Time{})
)
func isBinaryDecoder(val reflect.Value) bool {
return val.Type().Implements(binaryDecoder)
}
func isTime(val reflect.Value) bool {
return val.Type() == timeType
}
type BinaryDecoder interface {
Decode([]byte) (int, error)
}
func Decode(b []byte, v interface{}) (int, error) {
val := reflect.ValueOf(v)
return decode(b, val, val.Type().String())
}
func decode(b []byte, val reflect.Value, name string) (n int, err error) {
if debugCodec {
fmt.Printf("decode: %s has type %v and is a %s, %d bytes\n", name, val.Type(), val.Type().Kind(), len(b))
defer func() {
fmt.Printf("decode: decoded %d bytes into %s\n", n, name)
}()
}
buf := NewBuffer(b)
switch {
case isBinaryDecoder(val):
v := val.Interface().(BinaryDecoder)
return v.Decode(b)
case isTime(val):
val.Set(reflect.ValueOf(buf.ReadTime()))
default:
// fmt.Printf("decode: %s is a %s\n", name, val.Kind())
switch val.Kind() {
case reflect.Bool:
val.SetBool(buf.ReadBool())
case reflect.Int8:
val.SetInt(int64(buf.ReadInt8()))
case reflect.Uint8:
val.SetUint(uint64(buf.ReadByte()))
case reflect.Int16:
val.SetInt(int64(buf.ReadInt16()))
case reflect.Uint16:
val.SetUint(uint64(buf.ReadUint16()))
case reflect.Int32:
val.SetInt(int64(buf.ReadInt32()))
case reflect.Uint32:
val.SetUint(uint64(buf.ReadUint32()))
case reflect.Int64:
val.SetInt(buf.ReadInt64())
case reflect.Uint64:
val.SetUint(buf.ReadUint64())
case reflect.Float32:
val.SetFloat(float64(buf.ReadFloat32()))
case reflect.Float64:
val.SetFloat(buf.ReadFloat64())
case reflect.String:
val.SetString(buf.ReadString())
case reflect.Slice:
return decodeSlice(b, val, name)
case reflect.Ptr:
return decode(b, val.Elem(), name)
case reflect.Struct:
return decodeStruct(b, val, name)
default:
return 0, errors.Errorf("unsupported type %s", val.Type())
}
}
return buf.Pos(), buf.Error()
}
func decodeStruct(b []byte, val reflect.Value, name string) (int, error) {
pos := 0
valt := val.Type()
for i := 0; i < val.NumField(); i++ {
ft := valt.Field(i)
fname := name + "." + ft.Name
// if the field is a pointer we need to create
// the value before we can marshal data into it.
f := val.Field(i)
if f.Type().Kind() == reflect.Ptr {
f.Set(reflect.New(f.Type().Elem()))
// fmt.Printf("decode: %s has type %v and has new value %#v\n", fname, f.Type(), f.Interface())
}
n, err := decode(b[pos:], f, fname)
if err != nil {
return pos, err
}
pos += n
}
return pos, nil
}
func decodeSlice(b []byte, val reflect.Value, name string) (int, error) {
buf := NewBuffer(b)
n := buf.ReadUint32()
if buf.Error() != nil {
return buf.Pos(), buf.Error()
}
if n == null {
return buf.Pos(), nil
}
if n > math.MaxInt32 {
return buf.Pos(), errors.Errorf("array too large: %d", n)
}
// elemType is the type of the slice elements
// e.g. *Foo for []*Foo
elemType := val.Type().Elem()
// fmt.Println("elemType: ", elemType.String())
// fast path for []byte
if elemType.Kind() == reflect.Uint8 {
// fmt.Println("decode: []byte fast path")
val.SetBytes(buf.ReadN(int(n)))
return buf.Pos(), buf.Error()
}
pos := buf.Pos()
// a is a slice of []*Foo
a := reflect.MakeSlice(val.Type(), int(n), int(n))
for i := 0; i < int(n); i++ {
// if the slice elements are pointers we need to create
// them before we can marshal data into them.
if elemType.Kind() == reflect.Ptr {
a.Index(i).Set(reflect.New(elemType.Elem()))
}
ename := fmt.Sprintf("%s[%d]", name, i)
m, err := decode(b[pos:], a.Index(i), ename)
if err != nil {
return pos, err
}
pos += m
}
val.Set(a)
return pos, nil
}