Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Rework name clash resolver #45

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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 @@ -24,6 +24,7 @@ type Element struct {
SimpleType *SimpleType `xml:"simpleType"`
schema *Schema `xml:"-"`
typ Type `xml:"-"`
Parent *Element `xml:"-"`
}

func (e *Element) Attributes() []Attribute {
Expand Down Expand Up @@ -127,6 +128,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 @@ -64,6 +64,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 @@ -268,7 +314,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