forked from gopcua/opcua
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.go
82 lines (69 loc) · 1.72 KB
/
schema.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
// 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 main
import (
"encoding/xml"
"os"
)
type TypeDictionary struct {
XMLName xml.Name `xml:"TypeDictionary"`
Types []*StructType `xml:"StructuredType"`
Enums []*EnumType `xml:"EnumeratedType"`
}
type EnumType struct {
Name string `xml:",attr"`
Bits int `xml:"LengthInBits,attr"`
Doc string `xml:"Documentation"`
Values []*EnumValue `xml:"EnumeratedValue"`
}
type EnumValue struct {
Name string `xml:",attr"`
Value int `xml:",attr"`
}
type StructType struct {
Name string `xml:",attr"`
BaseType string `xml:"BaseType,attr"`
Doc string `xml:"Documentation"`
Fields []*StructField `xml:"Field"`
}
func (s *StructType) IsLengthField(f *StructField) bool {
for _, ff := range s.Fields {
if f.Name == ff.LengthField {
return true
}
}
return false
}
type StructField struct {
Name string `xml:",attr"`
Type string `xml:"TypeName,attr"`
LengthField string `xml:",attr"`
SwitchField string `xml:",attr"`
SwitchValue string `xml:",attr"`
IsEnum bool
}
func (f *StructField) IsSlice() bool {
return f.LengthField != ""
}
func ReadTypes(filename string) (*TypeDictionary, error) {
f, err := os.Open(filename)
if err != nil {
return nil, err
}
defer f.Close()
d := new(TypeDictionary)
if err := xml.NewDecoder(f).Decode(&d); err != nil {
return nil, err
}
enums := map[string]bool{}
for _, e := range d.Enums {
enums["tns:"+e.Name] = true
}
for _, t := range d.Types {
for _, f := range t.Fields {
f.IsEnum = enums[f.Type]
}
}
return d, nil
}