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

Fix #129 #130

Open
wants to merge 2 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
16 changes: 15 additions & 1 deletion pkg/xsd/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,22 @@ func (sch *Schema) encodingXmlImportNeeded() bool {
return len(sch.Elements) != 0 || len(sch.ComplexTypes) != 0
}

func deduplicateElementsLossfree(elements []Element) []Element {
seen := make(map[string]int, len(elements))
for j, element := range elements {
dupeCount, dupe := seen[element.GoName()]
if !dupe {
seen[element.GoName()] = 1
continue
}
elements[j].nameOverride = fmt.Sprintf("%s-%d", element.GoName(), dupeCount)
seen[element.GoName()]++
}
return elements
}

func (sch *Schema) ExportableElements() []Element {
return append(sch.Elements, sch.inlinedElements...)
return deduplicateElementsLossfree(append(sch.Elements, sch.inlinedElements...))
}

func (sch *Schema) ExportableComplexTypes() []ComplexType {
Expand Down
31 changes: 31 additions & 0 deletions tests/xsd-examples/valid/issue129.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" attributeFormDefault="qualified" version="1.0">
<xsd:annotation/>
<xsd:complexType name="AAA">
<xsd:annotation/>
<xsd:sequence>
<xsd:element name="List" minOccurs="0">
<xsd:annotation/>
<xsd:complexType>
<xsd:sequence>
<xsd:element name="CCC" type="xsd:string" minOccurs="1" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="BBB">
<xsd:annotation/>
<xsd:sequence>
<xsd:element name="List" minOccurs="0">
<xsd:annotation/>
<xsd:complexType>
<xsd:sequence>
<xsd:element name="DDD" type="xsd:int" minOccurs="1" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
37 changes: 37 additions & 0 deletions tests/xsd-examples/valid/issue129.xsd.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Code generated by https://github.com/gocomply/xsd2go; DO NOT EDIT.
// Models for
package issue129

import (
"encoding/xml"
)

// Element
type List struct {
XMLName xml.Name `xml:"List"`

Ccc []string `xml:",any"`
}

// Element
type List1 struct {
XMLName xml.Name `xml:"List"`

Ddd []int `xml:",any"`
}

// XSD ComplexType declarations

type Aaa struct {
XMLName xml.Name

List *List `xml:",any"`
}

type Bbb struct {
XMLName xml.Name

List *List `xml:",any"`
}

// XSD SimpleType declarations