forked from anz-bank/sysl
/
types.go
177 lines (139 loc) · 2.97 KB
/
types.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
package importer
import (
"sort"
"strings"
"github.com/sirupsen/logrus"
)
type Func func(args OutputData, text string, logger *logrus.Logger) (out string, err error)
type Type interface {
Name() string
}
type StandardType struct {
name string
Properties FieldList
Attributes []string
}
func (s *StandardType) Name() string { return s.name }
type Union struct {
name string
Options FieldList
}
func (u *Union) Name() string { return u.name }
type SyslBuiltIn struct {
name string
}
func (s *SyslBuiltIn) Name() string { return s.name }
var StringAlias = &SyslBuiltIn{name: StringTypeName}
// !alias type without the EXTERNAL_ prefix
type Alias struct {
name string
Target Type
}
func (s *Alias) Name() string { return s.name }
type ExternalAlias struct {
name string
Target Type
}
const (
StringTypeName = "string"
ObjectTypeName = "object"
ArrayTypeName = "array"
)
func NewStringAlias(name string) Type {
return &ExternalAlias{
name: name,
Target: StringAlias,
}
}
func (s *ExternalAlias) Name() string { return s.name }
type ImportedBuiltInAlias struct {
name string // input language type name
Target Type
}
func (s *ImportedBuiltInAlias) Name() string { return s.name }
type Array struct {
name string
Items Type
}
func (s *Array) Name() string { return s.name }
type Enum struct {
name string
}
func (s *Enum) Name() string { return s.name }
type maxType int
const (
MinOnly maxType = iota
MaxSpecified
OpenEnded
)
type sizeSpec struct {
Min int
Max int
MaxType maxType
}
type Field struct {
Name string
Type Type
Optional bool
Attributes []string
SizeSpec *sizeSpec
}
type TypeList struct {
types []Type
}
func (t TypeList) Items() []Type {
return t.types
}
func (t TypeList) Sort() {
sort.SliceStable(t.types, func(i, j int) bool {
a := t.types[i].Name()
b := t.types[j].Name()
return strings.Compare(a, b) < 0
})
}
type FieldList []Field
// nolint:gochecknoglobals
var builtIns = []string{"int32", "int64", "int", "float", "string", "date", "bool", "decimal", "datetime", "xml"}
func IsBuiltIn(name string) bool {
for _, kw := range builtIns {
if name == kw {
return true
}
}
return false
}
func (t TypeList) Find(name string) (Type, bool) {
if builtin, ok := checkBuiltInTypes(name); ok {
return builtin, ok
}
for _, n := range t.Items() {
if n.Name() == name {
if importAlias, ok := n.(*ImportedBuiltInAlias); ok {
return importAlias.Target, true
}
return n, true
}
}
return &StandardType{}, false
}
func (t *TypeList) Add(item ...Type) {
t.types = append(t.types, item...)
}
func (t *TypeList) AddAndRet(item Type) Type {
t.types = append(t.types, item)
return item
}
func checkBuiltInTypes(name string) (Type, bool) {
if contains(name, builtIns) {
return &SyslBuiltIn{name: name}, true
}
return &StandardType{}, false
}
func contains(needle string, haystack []string) bool {
for _, x := range haystack {
if x == needle {
return true
}
}
return false
}