/
variant.go
766 lines (673 loc) · 17 KB
/
variant.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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
// 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 (
"reflect"
"time"
"github.com/355911097/opcua/errors"
)
var (
// MaxVariantArrayLength sets a limit on the number of elements in array
MaxVariantArrayLength = 0xffff
)
const (
// VariantArrayDimensions flags whether the array has more than one dimension
VariantArrayDimensions = 0x40
// VariantArrayValues flags whether the value is an array.
VariantArrayValues = 0x80
)
// Variant is a union of the built-in types.
//
// Specification: Part 6, 5.2.2.16
type Variant struct {
// mask contains the type and the array flags
// bits 0:5: built-in type id 1-25
// bit 6: array dimensions
// bit 7: array values
mask byte
// arrayLength is the number of elements in the array.
// This field is only present if the 'array values'
// flag is set.
//
// Multi-dimensional arrays are encoded as a one-dimensional array and this
// field specifies the total number of elements. The original array can be
// reconstructed from the dimensions that are encoded after the value
// field.
arrayLength int32
// arrayDimensionsLength is the number of dimensions.
// This field is only present if the 'array dimensions' flag
// is set.
arrayDimensionsLength int32
// arrayDimensions is the size for each dimension.
// This field is only present if the 'array dimensions' flag
// is set.
arrayDimensions []int32
value interface{}
}
func NewVariant(v interface{}) (*Variant, error) {
va := &Variant{}
if err := va.set(v); err != nil {
return nil, err
}
return va, nil
}
func MustVariant(v interface{}) *Variant {
va, err := NewVariant(v)
if err != nil {
panic(err)
}
return va
}
func (m *Variant) EncodingMask() byte {
return m.mask
}
// Type returns the type id of the value.
func (m *Variant) Type() TypeID {
return TypeID(m.mask & 0x3f)
}
func (m *Variant) setType(t TypeID) {
m.mask |= byte(t & 0x3f)
}
// Has returns whether given encoding mask bits are set.
func (m *Variant) Has(mask byte) bool {
return m.mask&mask == mask
}
// ArrayLength returns the total number of elements for one and multi-dimensional
// array values.
func (m *Variant) ArrayLength() int32 {
return m.arrayLength
}
// ArrayDimensions returns the dimensions of multi-dimensional arrays.
func (m *Variant) ArrayDimensions() []int32 {
return m.arrayDimensions
}
// Value returns the value.
func (m *Variant) Value() interface{} {
return m.value
}
// Decode implements the codec interface.
func (m *Variant) Decode(b []byte) (int, error) {
buf := NewBuffer(b)
m.mask = buf.ReadByte()
// a null value specifies that no other fields are encoded
if m.Type() == TypeIDNull {
return buf.Pos(), buf.Error()
}
// check the type
typ, ok := variantTypeIDToType[m.Type()]
if !ok {
return buf.Pos(), errors.Errorf("invalid type id: %d", m.Type())
}
// read single value and return
if !m.Has(VariantArrayValues) {
m.value = m.decodeValue(buf)
return buf.Pos(), buf.Error()
}
// get total array length (flattened for multi-dimensional arrays)
m.arrayLength = buf.ReadInt32()
// read flattened array elements
n := int(m.arrayLength)
if n < 0 || n > MaxVariantArrayLength {
return buf.Pos(), StatusBadEncodingLimitsExceeded
}
vals := reflect.MakeSlice(reflect.SliceOf(typ), n, n)
for i := 0; i < n; i++ {
vals.Index(i).Set(reflect.ValueOf(m.decodeValue(buf)))
}
// check for dimensions of multi-dimensional array
if m.Has(VariantArrayDimensions) {
m.arrayDimensionsLength = buf.ReadInt32()
if m.arrayDimensionsLength < 0 {
return buf.Pos(), StatusBadEncodingLimitsExceeded
}
m.arrayDimensions = make([]int32, m.arrayDimensionsLength)
for i := 0; i < int(m.arrayDimensionsLength); i++ {
m.arrayDimensions[i] = buf.ReadInt32()
if m.arrayDimensions[i] < 1 {
return buf.Pos(), StatusBadEncodingLimitsExceeded
}
}
}
// return early if there is an error since the rest of the code
// depends on the assumption that the array dimensions were read
// correctly.
if buf.Error() != nil {
return buf.Pos(), buf.Error()
}
// validate that the total number of elements
// matches the product of the array dimensions
if m.arrayDimensionsLength > 0 {
count := int32(1)
for i := range m.arrayDimensions {
count *= m.arrayDimensions[i]
}
if count != m.arrayLength {
return buf.Pos(), errUnbalancedSlice
}
}
// handle one-dimensional arrays
if m.arrayDimensionsLength < 2 {
m.value = vals.Interface()
return buf.Pos(), buf.Error()
}
// handle multi-dimensional arrays
// convert dimensions to []int to avoid lots of type casts
dims := make([]int, len(m.arrayDimensions))
for i := range m.arrayDimensions {
dims[i] = int(m.arrayDimensions[i])
}
m.value = split(0, 0, vals.Len(), dims, vals).Interface()
return buf.Pos(), buf.Error()
}
// split recursively creates a multi-dimensional array from a set of values
// and some given dimensions.
func split(level, i, j int, dims []int, vals reflect.Value) reflect.Value {
if level == len(dims)-1 {
a := vals.Slice(i, j)
// fmt.Printf("split: level:%d i:%d j:%d dims:%v a:%#v\n", level, i, j, dims, a.Interface())
return a
}
// split next level
var elems []reflect.Value
if vals.Len() > 0 {
step := (j - i) / dims[level]
for ; i < j; i += step {
elems = append(elems, split(level+1, i, i+step, dims, vals))
}
} else {
for k := 0; k < dims[level]; k++ {
elems = append(elems, split(level+1, 0, 0, dims, vals))
}
}
// now construct the typed slice, i.e. [](type of inner slice)
innerT := elems[0].Type()
a := reflect.MakeSlice(reflect.SliceOf(innerT), len(elems), len(elems))
for k := range elems {
a.Index(k).Set(elems[k])
}
// fmt.Printf("split: level:%d i:%d j:%d dims:%v a:%#v\n", level, i, j, dims, a.Interface())
return a
}
// decodeValue reads a single value of the base type from the buffer.
func (m *Variant) decodeValue(buf *Buffer) interface{} {
switch m.Type() {
case TypeIDBoolean:
return buf.ReadBool()
case TypeIDSByte:
return buf.ReadInt8()
case TypeIDByte:
return buf.ReadByte()
case TypeIDInt16:
return buf.ReadInt16()
case TypeIDUint16:
return buf.ReadUint16()
case TypeIDInt32:
return buf.ReadInt32()
case TypeIDUint32:
return buf.ReadUint32()
case TypeIDInt64:
return buf.ReadInt64()
case TypeIDUint64:
return buf.ReadUint64()
case TypeIDFloat:
return buf.ReadFloat32()
case TypeIDDouble:
return buf.ReadFloat64()
case TypeIDString:
return buf.ReadString()
case TypeIDDateTime:
return buf.ReadTime()
case TypeIDGUID:
v := new(GUID)
buf.ReadStruct(v)
return v
case TypeIDByteString:
return buf.ReadBytes()
case TypeIDXMLElement:
return XMLElement(buf.ReadString())
case TypeIDNodeID:
v := new(NodeID)
buf.ReadStruct(v)
return v
case TypeIDExpandedNodeID:
v := new(ExpandedNodeID)
buf.ReadStruct(v)
return v
case TypeIDStatusCode:
return StatusCode(buf.ReadUint32())
case TypeIDQualifiedName:
v := new(QualifiedName)
buf.ReadStruct(v)
return v
case TypeIDLocalizedText:
v := new(LocalizedText)
buf.ReadStruct(v)
return v
case TypeIDExtensionObject:
v := new(ExtensionObject)
buf.ReadStruct(v)
return v
case TypeIDDataValue:
v := new(DataValue)
buf.ReadStruct(v)
return v
case TypeIDVariant:
// todo(fs): limit recursion depth to 100
v := new(Variant)
buf.ReadStruct(v)
return v
case TypeIDDiagnosticInfo:
// todo(fs): limit recursion depth to 100
v := new(DiagnosticInfo)
buf.ReadStruct(v)
return v
default:
return nil
}
}
// Encode implements the codec interface.
func (m *Variant) Encode() ([]byte, error) {
buf := NewBuffer(nil)
buf.WriteByte(m.mask)
// a null value specifies that no other fields are encoded
if m.Type() == TypeIDNull {
return buf.Bytes(), buf.Error()
}
if m.Has(VariantArrayValues) {
buf.WriteInt32(m.arrayLength)
}
m.encode(buf, reflect.ValueOf(m.value))
if m.Has(VariantArrayDimensions) {
buf.WriteInt32(m.arrayDimensionsLength)
for i := 0; i < int(m.arrayDimensionsLength); i++ {
buf.WriteInt32(m.arrayDimensions[i])
}
}
return buf.Bytes(), buf.Error()
}
// encode recursively writes the values to the buffer.
func (m *Variant) encode(buf *Buffer, val reflect.Value) {
if val.Kind() != reflect.Slice || m.Type() == TypeIDByteString {
m.encodeValue(buf, val.Interface())
return
}
for i := 0; i < val.Len(); i++ {
m.encode(buf, val.Index(i))
}
}
// encodeValue writes a single value of the base type to the buffer.
func (m *Variant) encodeValue(buf *Buffer, v interface{}) {
switch x := v.(type) {
case bool:
buf.WriteBool(x)
case int8:
buf.WriteInt8(x)
case byte:
buf.WriteByte(x)
case int16:
buf.WriteInt16(x)
case uint16:
buf.WriteUint16(x)
case int32:
buf.WriteInt32(x)
case uint32:
buf.WriteUint32(x)
case int64:
buf.WriteInt64(x)
case uint64:
buf.WriteUint64(x)
case float32:
buf.WriteFloat32(x)
case float64:
buf.WriteFloat64(x)
case string:
buf.WriteString(x)
case time.Time:
buf.WriteTime(x)
case *GUID:
buf.WriteStruct(x)
case []byte:
buf.WriteByteString(x)
case XMLElement:
buf.WriteString(string(x))
case *NodeID:
buf.WriteStruct(x)
case *ExpandedNodeID:
buf.WriteStruct(x)
case StatusCode:
buf.WriteUint32(uint32(x))
case *QualifiedName:
buf.WriteStruct(x)
case *LocalizedText:
buf.WriteStruct(x)
case *ExtensionObject:
buf.WriteStruct(x)
case *DataValue:
buf.WriteStruct(x)
case *Variant:
buf.WriteStruct(x)
case *DiagnosticInfo:
buf.WriteStruct(x)
}
}
// errUnbalancedSlice indicates a multi-dimensional array has different
// number of elements on the same level.
var errUnbalancedSlice = errors.New("unbalanced multi-dimensional array")
// sliceDim determines the element type, dimensions and the total length
// of a one or multi-dimensional slice.
func sliceDim(v reflect.Value) (typ reflect.Type, dim []int32, count int32, err error) {
// null type
if v.Kind() == reflect.Invalid {
return nil, nil, 0, nil
}
// ByteString is its own type
if v.Type() == reflect.TypeOf([]byte{}) {
return v.Type(), nil, 1, nil
}
// element type
if v.Kind() != reflect.Slice {
return v.Type(), nil, 1, nil
}
// empty array
if v.Len() == 0 {
return v.Type().Elem(), append([]int32{0}, dim...), 0, nil
}
// check that inner slices all have the same length
if v.Index(0).Kind() == reflect.Slice {
for i := 0; i < v.Len(); i++ {
if v.Index(i).Len() != v.Index(0).Len() {
return nil, nil, 0, errUnbalancedSlice
}
}
}
// recurse to inner slice or element type
typ, dim, count, err = sliceDim(v.Index(0))
if err != nil {
return nil, nil, 0, err
}
return typ, append([]int32{int32(v.Len())}, dim...), count * int32(v.Len()), nil
}
// set sets the value and updates the flags according to the type.
func (m *Variant) set(v interface{}) error {
// set array length and dimensions if value is a slice
et, dim, count, err := sliceDim(reflect.ValueOf(v))
if err != nil {
return err
}
if len(dim) > 0 {
m.mask |= VariantArrayValues
m.arrayLength = count
}
if len(dim) > 1 {
m.mask |= VariantArrayDimensions
m.arrayDimensionsLength = int32(len(dim))
m.arrayDimensions = dim
}
typeid, ok := variantTypeToTypeID[et]
if !ok {
return errors.Errorf("cannot set variant to %T", v)
}
m.setType(typeid)
m.value = v
return nil
}
// todo(fs): this should probably be StringValue or we need to handle all types
// todo(fs): and recursion
func (m *Variant) String() string {
if m.ArrayLength() > 0 {
return ""
}
switch m.Type() {
case TypeIDString:
return m.value.(string)
case TypeIDXMLElement:
return string(m.XMLElement())
case TypeIDLocalizedText:
return m.value.(*LocalizedText).Text
case TypeIDQualifiedName:
return m.value.(*QualifiedName).Name
default:
return ""
}
}
// Bool returns the boolean value if the type is Boolean.
func (m *Variant) Bool() bool {
if m.ArrayLength() > 0 {
return false
}
switch m.Type() {
case TypeIDBoolean:
return m.value.(bool)
default:
return false
}
}
// Float returns the float value if the type is one of the float types.
func (m *Variant) Float() float64 {
if m.ArrayLength() > 0 {
return 0
}
switch m.Type() {
case TypeIDFloat:
return float64(m.value.(float32))
case TypeIDDouble:
return m.value.(float64)
default:
return 0
}
}
// Int returns the int value if the type is one of the int types.
func (m *Variant) Int() int64 {
if m.ArrayLength() > 0 {
return 0
}
switch m.Type() {
case TypeIDSByte:
return int64(m.value.(int8))
case TypeIDInt16:
return int64(m.value.(int16))
case TypeIDInt32:
return int64(m.value.(int32))
case TypeIDInt64:
return m.value.(int64)
default:
return 0
}
}
// Uint returns the uint value if the type is one of the uint types.
func (m *Variant) Uint() uint64 {
if m.ArrayLength() > 0 {
return 0
}
switch m.Type() {
case TypeIDByte:
return uint64(m.value.(byte))
case TypeIDUint16:
return uint64(m.value.(uint16))
case TypeIDUint32:
return uint64(m.value.(uint32))
case TypeIDUint64:
return m.value.(uint64)
default:
return 0
}
}
func (m *Variant) ByteString() []byte {
if m.ArrayLength() > 0 {
return nil
}
switch m.Type() {
case TypeIDByteString:
return m.value.([]byte)
default:
return nil
}
}
func (m *Variant) DataValue() *DataValue {
if m.ArrayLength() > 0 {
return nil
}
switch m.Type() {
case TypeIDDataValue:
return m.value.(*DataValue)
default:
return nil
}
}
func (m *Variant) DiagnosticInfo() *DiagnosticInfo {
if m.ArrayLength() > 0 {
return nil
}
switch m.Type() {
case TypeIDDiagnosticInfo:
return m.value.(*DiagnosticInfo)
default:
return nil
}
}
func (m *Variant) ExpandedNodeID() *ExpandedNodeID {
if m.ArrayLength() > 0 {
return nil
}
switch m.Type() {
case TypeIDExpandedNodeID:
return m.value.(*ExpandedNodeID)
default:
return nil
}
}
func (m *Variant) ExtensionObject() *ExtensionObject {
if m.ArrayLength() > 0 {
return nil
}
switch m.Type() {
case TypeIDExtensionObject:
return m.value.(*ExtensionObject)
default:
return nil
}
}
func (m *Variant) GUID() *GUID {
if m.ArrayLength() > 0 {
return nil
}
switch m.Type() {
case TypeIDGUID:
return m.value.(*GUID)
default:
return nil
}
}
func (m *Variant) LocalizedText() *LocalizedText {
if m.ArrayLength() > 0 {
return nil
}
switch m.Type() {
case TypeIDLocalizedText:
return m.value.(*LocalizedText)
default:
return nil
}
}
func (m *Variant) NodeID() *NodeID {
if m.ArrayLength() > 0 {
return nil
}
switch m.Type() {
case TypeIDNodeID:
return m.value.(*NodeID)
default:
return nil
}
}
func (m *Variant) QualifiedName() *QualifiedName {
if m.ArrayLength() > 0 {
return nil
}
switch m.Type() {
case TypeIDQualifiedName:
return m.value.(*QualifiedName)
default:
return nil
}
}
func (m *Variant) StatusCode() StatusCode {
if m.ArrayLength() > 0 {
return StatusBadTypeMismatch
}
switch m.Type() {
case TypeIDStatusCode:
return m.value.(StatusCode)
default:
return StatusBadTypeMismatch
}
}
// Time returns the time value if the type is DateTime.
func (m *Variant) Time() time.Time {
if m.ArrayLength() > 0 {
return time.Time{}
}
switch m.Type() {
case TypeIDDateTime:
return m.value.(time.Time)
default:
return time.Time{}
}
}
func (m *Variant) Variant() *Variant {
if m.ArrayLength() > 0 {
return nil
}
switch m.Type() {
case TypeIDVariant:
return m.value.(*Variant)
default:
return nil
}
}
func (m *Variant) XMLElement() XMLElement {
if m.ArrayLength() > 0 {
return ""
}
switch m.Type() {
case TypeIDXMLElement:
return m.value.(XMLElement)
default:
return ""
}
}
var variantTypeToTypeID = map[reflect.Type]TypeID{}
var variantTypeIDToType = map[TypeID]reflect.Type{
TypeIDNull: reflect.TypeOf(nil),
TypeIDBoolean: reflect.TypeOf(false),
TypeIDSByte: reflect.TypeOf(int8(0)),
TypeIDByte: reflect.TypeOf(uint8(0)),
TypeIDInt16: reflect.TypeOf(int16(0)),
TypeIDUint16: reflect.TypeOf(uint16(0)),
TypeIDInt32: reflect.TypeOf(int32(0)),
TypeIDUint32: reflect.TypeOf(uint32(0)),
TypeIDInt64: reflect.TypeOf(int64(0)),
TypeIDUint64: reflect.TypeOf(uint64(0)),
TypeIDFloat: reflect.TypeOf(float32(0)),
TypeIDDouble: reflect.TypeOf(float64(0)),
TypeIDString: reflect.TypeOf(string("")),
TypeIDDateTime: reflect.TypeOf(time.Time{}),
TypeIDGUID: reflect.TypeOf(new(GUID)),
TypeIDByteString: reflect.TypeOf([]byte{}),
TypeIDXMLElement: reflect.TypeOf(XMLElement("")),
TypeIDNodeID: reflect.TypeOf(new(NodeID)),
TypeIDExpandedNodeID: reflect.TypeOf(new(ExpandedNodeID)),
TypeIDStatusCode: reflect.TypeOf(StatusCode(0)),
TypeIDQualifiedName: reflect.TypeOf(new(QualifiedName)),
TypeIDLocalizedText: reflect.TypeOf(new(LocalizedText)),
TypeIDExtensionObject: reflect.TypeOf(new(ExtensionObject)),
TypeIDDataValue: reflect.TypeOf(new(DataValue)),
TypeIDVariant: reflect.TypeOf(new(Variant)),
TypeIDDiagnosticInfo: reflect.TypeOf(new(DiagnosticInfo)),
}
func init() {
for id, t := range variantTypeIDToType {
variantTypeToTypeID[t] = id
}
}