/
proto.go
336 lines (279 loc) · 8.28 KB
/
proto.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
// Copyright 2020 - 2021 The xgen Authors. All rights reserved. Use of this
// source code is governed by a BSD-style license that can be found in the
// LICENSE file.
//
// Package xgen written in pure Go providing a set of functions that allow you
// to parse XSD (XML schema files). This library needs Go version 1.10 or
// later.
package xgen
import (
"strings"
)
// SimpleTypeInside definitions provide for constraining character information item
// [children] of element and attribute information items.
// https://www.w3.org/TR/xmlschema-1/#Simple_Type_Definitions
type SimpleType struct {
Doc string
Name string
Base string
// то что записано в схеме
OrigBase string
Anonymous bool
List bool
Union bool
MemberTypes map[string]string
Restriction Restriction
OrigName string
}
// Element declarations provide for: Local validation of element information
// item values using a type definition; Specifying default or fixed values for
// an element information items; Establishing uniquenesses and reference
// constraint relationships among the values of related elements and
// attributes; Controlling the substitutability of elements through the
// mechanism of element substitution groups.
// https://www.w3.org/TR/xmlschema-1/#cElement_Declarations
type Element struct {
Doc string
Name string
Wildcard bool
Type string
Abstract bool
Plural bool
PluralOptional bool
Optional bool
Nillable bool
Default string
}
// Attribute declarations provide for: Local validation of attribute
// information item values using a simple type definition; Specifying default
// or fixed values for attribute information items.
// https://www.w3.org/TR/xmlschema-1/structures.html#element-attribute
type Attribute struct {
Name string
Doc string
Type string
Plural bool
Default *string
Optional bool
// {value constraint}
//Optional. A pair consisting of a value and one of default, fixed.
Fixed string // todo: Implement for validation
SimpleTypeInside *SimpleType
}
// ComplexType definitions are identified by their {name} and {target
// namespace}. Except for anonymous complex type definitions (those with no
// {name}), since type definitions (i.e. both simple and complex type
// definitions taken together) must be uniquely identified within an ·XML
// Schema·, no complex type definition can have the same name as another
// simple or complex type definition. Complex type {name}s and {target
// namespace}s are provided for reference from instances, and for use in the
// XML representation of schema components (specifically in <element>). See
// References to schema components across namespaces for the use of component
// identifiers when importing one schema into another.
// https://www.w3.org/TR/xmlschema-1/structures.html#element-complexType
type ComplexType struct {
Doc string
Name string
Base string
Anonymous bool
Elements []*Element
Attributes []*Attribute
Groups []*Group
AttributeGroup []*AttributeGroup
Mixed bool
EmbeddedStructName string
AnyElements bool
AnyAttributes bool
}
// Group (model group) definitions are provided primarily for reference from
// the XML Representation of Complex Type Definitions. Thus, model group
// definitions provide a replacement for some uses of XML's parameter entity
// facility.
// https://www.w3.org/TR/xmlschema-1/structures.html#cModel_Group_Definitions
type Group struct {
Doc string
Name string
Elements []*Element
Groups []*Group
Plural bool
Ref string
}
// AttributeGroup definitions do not participate in ·validation· as such, but
// the {attribute uses} and {attribute wildcard} of one or more complex type
// definitions may be constructed in whole or part by reference to an
// attribute group. Thus, attribute group definitions provide a replacement
// for some uses of XML's parameter entity facility. Attribute group
// definitions are provided primarily for reference from the XML
// representation of schema components (see <complexType> and
// <attributeGroup>).
// https://www.w3.org/TR/xmlschema-1/structures.html#Attribute_Group_Definition
type AttributeGroup struct {
Doc string
Name string
Ref string
Attributes []*Attribute
AttributeGroup []*AttributeGroup
insideAttrGroup bool
}
// Restriction are used to define acceptable values for XML elements or
// attributes. Restriction on XML elements are called facets.
// https://www.w3.org/TR/xmlschema-1/structures.html#element-restriction
type Restriction struct {
Doc string
Precision int
Enum []string
Min, Max float64
MinLength, MaxLength int
Pattern []string
}
type Choice struct {
MinOccurs string
MaxOccurs string
}
var BaseTypeTmpl = strings.ReplaceAll(`package %s
import (
"encoding/json"
"encoding/xml"
"errors"
"fmt"
"github.com/google/uuid"
"strconv"
"strings"
)
// NodeID используется для маппинга элементов из бд и структуры
type NodeID struct {
NodeID *uuid.UUID \\xml:"-" json:"node_id,omitempty"\\
}
func (t *NodeID) Scan(el *Element) error {
if el.ID != uuid.Nil {
t.NodeID = &el.ID
}
return nil
}
func (t *NodeID) Read(el *Element) error {
if t.NodeID != nil {
el.ID = *t.NodeID
}
return nil
}
type UnsignedInt string
func (u *UnsignedInt) Validate() error {
_, err := strconv.ParseUint(string(*u), 10, 32)
return err
}
/* Additional fields */
var nameSpaceAliases = make(map[string]string)
type AdditionalFieldsType []AdditionalField
type AdditionalField struct {
xml.Attr
}
func (a *AdditionalField) UnmarshalXMLAttr(attr xml.Attr) error {
a.Attr = attr
if attr.Name.Local != "" && strings.Contains(attr.Value, "//") {
nameSpaceAliases[attr.Value] = attr.Name.Local
}
return nil
}
func (a *AdditionalField) MarshalXMLAttr(_ xml.Name) (xml.Attr, error) {
space := a.Name.Space
if strings.Contains(space, "//") {
space = nameSpaceAliases[space]
}
if space != "" {
space += ":"
}
return xml.Attr{
Name: xml.Name{
//Space: a.FieldName.Space,
Local: space + a.Name.Local,
},
Value: a.Value,
}, nil
}
type AdditionalFields struct {
AdditionalFields AdditionalFieldsType \\xml:",any,attr"\\
CustomElements []AnyHolder \\xml:",any"\\
}
type AnyHolder struct {
MModel
}
type MModel struct {
XMLName xml.Name
Attr []xml.Attr \\xml:",any,attr"\\
XML string \\xml:",innerxml"\\
}
func (a *AnyHolder) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var model MModel
err := d.DecodeElement(&model, &start)
a.MModel = model
return err
}
func (a *AnyHolder) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
space := a.MModel.XMLName.Space
if strings.Contains(space, "//") {
space = nameSpaceAliases[space]
}
if space != "" {
space += ":"
}
am := MModel{
XMLName: xml.Name{
Space: "",
Local: space + a.XMLName.Local,
},
Attr: a.Attr,
XML: a.XML,
}
return e.Encode(am)
}
var (
_ xml.Unmarshaler = (*AnyHolder)(nil)
_ xml.Marshaler = (*AnyHolder)(nil)
)
type Validator interface {
Validate() error
}
func CheckValidate(v interface{}) error {
if fn, ok := v.(Validator); ok {
return fn.Validate()
}
return nil
}
type UnionContent struct {
Content string \\xml:",chardata" json:",omitempty"\\
}
func (u UnionContent) MarshalJSON() ([]byte, error) {
return []byte("\"" + u.Content + "\""), nil
}
func (u *UnionContent) UnmarshalJSON(v []byte) error {
if len(v) > 0 {
u.Content = strings.Trim(string(v), "\"")
}
return nil
}
func (u UnionContent) String() string {
return u.Content
}
var (
_ json.Unmarshaler = (*UnionContent)(nil)
_ json.Marshaler = (*UnionContent)(nil)
_ fmt.Stringer = (*UnionContent)(nil)
)
// "ComplexType"
// UnitSlice - хак чтобы побороть []uint32
type UnitSlice struct {
NodeID
Content string \\xml:",chardata" json:",omitempty"\\
}
func (t *UnitSlice) CreateElement(xmlTag string, level, pos uint, parentEl *Element) (*Element, error) {
if xmlTag == "" {
return nil, errors.New("UnitSlice.CreateElement empty tag ")
}
el := NewElement(xmlTag, level, pos, parentEl)
if t.NodeID.NodeID != nil {
el.ID = *t.NodeID.NodeID
}
el.Content = t.Content
return el, nil
}
`, `\\`, "`")