Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pkg/xsd/element.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type Element struct {
SimpleType *SimpleType `xml:"simpleType"`
schema *Schema
typ Type
Parent *Element `xml:"-"`
}

func (e *Element) Attributes() []Attribute {
Expand Down Expand Up @@ -142,6 +143,7 @@ func (e *Element) isArray() bool {

func (e *Element) compile(s *Schema, parentElement *Element) {
e.schema = s
e.Parent = parentElement
if e.ComplexType != nil {
e.typ = e.ComplexType
if e.SimpleType != nil {
Expand Down
47 changes: 46 additions & 1 deletion pkg/xsd/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,52 @@ func (sch *Schema) compile() {
st := &sch.SimpleTypes[idx]
st.compile(sch, nil)
}
sch.resolveNameCollisions()
}

func (sch *Schema) resolveNameCollisions() {
elementsByName := sch.elementsByGoName()
for _, l := range elementsByName {
if len(l) > 1 {
// Found elements that have duplicate name
// TODO: resolve the name collisions more intelligently here.
// TODO: Needs to compare parent name for all the collisions and potentially use grand parent
fmt.Printf("\n - TODO: resolving name collision: %s", l[0].GoName())
for idx := range l {
el := l[idx]
el.prefixNameWithParent(el.Parent)
}
}
}
// TODO call resolveNameCollisions again on resolved schema, to avoid new collisions created by resolver
}

func (sch *Schema) elementsByGoName() map[string][]*Element {
result := map[string][]*Element{}
for idx := range sch.Elements {
el := &sch.Elements[idx]
l, ok := result[el.GoName()]
if !ok {
result[el.GoName()] = []*Element{
el,
}
} else {
result[el.GoName()] = append(l, el)
}
}

for idx := range sch.inlinedElements {
el := &sch.inlinedElements[idx]
l, ok := result[el.GoName()]
if !ok {
result[el.GoName()] = []*Element{
el,
}
} else {
result[el.GoName()] = append(l, el)
}
}
return result
}

func (sch *Schema) findReferencedAttribute(ref reference) *Attribute {
Expand Down Expand Up @@ -305,7 +351,6 @@ func (sch *Schema) registerInlinedElement(el *Element, parentElement *Element) {
if el.Name == "" {
panic("Not implemented: found inlined xsd:element without @name attribute")
}
el.prefixNameWithParent(parentElement)
sch.inlinedElements = append(sch.inlinedElements, *el)
}
}
Expand Down
Loading