forked from GoComply/xsd2go
/
attributegroup.go
73 lines (58 loc) · 1.75 KB
/
attributegroup.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
package xsd
import (
"encoding/xml"
"github.com/iancoleman/strcase"
)
type AttributeGroup struct {
XMLName xml.Name `xml:"http://www.w3.org/2001/XMLSchema attributeGroup"`
Name string `xml:"name,attr"`
Ref reference `xml:"ref,attr"`
AttributesDirect []Attribute `xml:"attribute"`
typ Type `xml:"-"`
schema *Schema `xml:"-"`
}
func (att *AttributeGroup) Attributes() []Attribute {
attrs := att.AttributesDirect
if att.typ != nil {
attrs = append(attrs, att.typ.Attributes()...)
}
return attrs
}
func (att *AttributeGroup) compile(sch *Schema, parentElement *Element) {
att.schema = sch
if att.Ref != "" {
att.typ = sch.findReferencedType(att.Ref)
if att.typ == nil {
panic("Cannot build xsd:attributeGroup: unknown type: " + string(att.Ref))
}
att.typ.compile(sch, parentElement)
}
// Handle improbable name clash. Consider XSD defining two attributes on the element:
// "id" and "Id", this would create name clash given the camelization we do.
goNames := map[string]uint{}
for idx := range att.Attributes() {
attribute := &att.Attributes()[idx]
attribute.compile(sch)
count := goNames[attribute.GoName()]
count += 1
goNames[attribute.GoName()] = count
attribute.DuplicateCount = count
// Second GoName may be different depending on the DuplicateCount
goNames[attribute.GoName()] = count
}
}
func (att *AttributeGroup) GoName() string {
return strcase.ToCamel(att.Name)
}
func (att *AttributeGroup) GoTypeName() string {
return att.GoName()
}
func (att *AttributeGroup) Schema() *Schema {
return att.schema
}
func (att *AttributeGroup) Elements() []Element {
return []Element{}
}
func (att *AttributeGroup) ContainsText() bool {
return true
}