diff --git a/pkg/xsd/element.go b/pkg/xsd/element.go index ce5c6b8..f6b70f1 100644 --- a/pkg/xsd/element.go +++ b/pkg/xsd/element.go @@ -25,6 +25,7 @@ type Element struct { SimpleType *SimpleType `xml:"simpleType"` schema *Schema typ Type + Parent *Element `xml:"-"` } func (e *Element) Attributes() []Attribute { @@ -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 { diff --git a/pkg/xsd/schema.go b/pkg/xsd/schema.go index 1b742b4..b051a9e 100644 --- a/pkg/xsd/schema.go +++ b/pkg/xsd/schema.go @@ -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 { @@ -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) } }