forked from gopcua/opcua
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encode.go
138 lines (120 loc) · 3.24 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
// 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/debug"
"github.com/bpizzi/opcua/errors"
)
// debugCodec enables printing of debug messages in the opcua codec.
var debugCodec = debug.FlagSet("codec")
// BinaryEncoder is the interface implemented by an object that can
// marshal itself into a binary OPC/UA representation.
type BinaryEncoder interface {
Encode() ([]byte, error)
}
var binaryEncoder = reflect.TypeOf((*BinaryEncoder)(nil)).Elem()
func isBinaryEncoder(val reflect.Value) bool {
return val.Type().Implements(binaryEncoder)
}
func Encode(v interface{}) ([]byte, error) {
val := reflect.ValueOf(v)
return encode(val, val.Type().String())
}
func encode(val reflect.Value, name string) ([]byte, error) {
if debugCodec {
fmt.Printf("encode: %s has type %s and is a %s\n", name, val.Type(), val.Type().Kind())
}
buf := NewBuffer(nil)
switch {
case isBinaryEncoder(val):
v := val.Interface().(BinaryEncoder)
return v.Encode()
case isTime(val):
buf.WriteTime(val.Interface().(time.Time))
default:
switch val.Kind() {
case reflect.Bool:
buf.WriteBool(val.Bool())
case reflect.Int8:
buf.WriteInt8(int8(val.Int()))
case reflect.Uint8:
buf.WriteUint8(uint8(val.Uint()))
case reflect.Int16:
buf.WriteInt16(int16(val.Int()))
case reflect.Uint16:
buf.WriteUint16(uint16(val.Uint()))
case reflect.Int32:
buf.WriteInt32(int32(val.Int()))
case reflect.Uint32:
buf.WriteUint32(uint32(val.Uint()))
case reflect.Int64:
buf.WriteInt64(int64(val.Int()))
case reflect.Uint64:
buf.WriteUint64(uint64(val.Uint()))
case reflect.Float32:
buf.WriteFloat32(float32(val.Float()))
case reflect.Float64:
buf.WriteFloat64(float64(val.Float()))
case reflect.String:
buf.WriteString(val.String())
case reflect.Ptr:
if val.IsNil() {
return nil, nil
}
return encode(val.Elem(), name)
case reflect.Struct:
return writeStruct(val, name)
case reflect.Slice:
return writeSlice(val, name)
default:
return nil, errors.Errorf("unsupported type: %s", val.Type())
}
}
return buf.Bytes(), buf.Error()
}
func writeStruct(val reflect.Value, name string) ([]byte, error) {
var buf []byte
valt := val.Type()
for i := 0; i < val.NumField(); i++ {
ft := valt.Field(i)
fname := name + "." + ft.Name
b, err := encode(val.Field(i), fname)
if err != nil {
return nil, err
}
buf = append(buf, b...)
}
return buf, nil
}
func writeSlice(val reflect.Value, name string) ([]byte, error) {
buf := NewBuffer(nil)
if val.IsNil() {
buf.WriteUint32(null)
return buf.Bytes(), buf.Error()
}
if val.Len() > math.MaxInt32 {
return nil, errors.Errorf("array too large")
}
buf.WriteUint32(uint32(val.Len()))
// fast path for []byte
if val.Type().Elem().Kind() == reflect.Uint8 {
// fmt.Println("[]byte fast path")
buf.Write(val.Bytes())
return buf.Bytes(), buf.Error()
}
// loop over elements
for i := 0; i < val.Len(); i++ {
ename := fmt.Sprintf("%s[%d]", name, i)
b, err := encode(val.Index(i), ename)
if err != nil {
return nil, err
}
buf.Write(b)
}
return buf.Bytes(), buf.Error()
}