From e932368ff36009d0695b03c62a90b16487a2217c Mon Sep 17 00:00:00 2001 From: Niklas Nett Date: Fri, 23 Aug 2024 15:09:08 +0200 Subject: [PATCH] Fix #129 --- pkg/xsd/schema.go | 16 +++++++++++++++- tests/xsd-examples/valid/issue129.xsd.out | 2 +- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/pkg/xsd/schema.go b/pkg/xsd/schema.go index 4799aac..30afef1 100644 --- a/pkg/xsd/schema.go +++ b/pkg/xsd/schema.go @@ -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 { diff --git a/tests/xsd-examples/valid/issue129.xsd.out b/tests/xsd-examples/valid/issue129.xsd.out index f70abbb..3469f2d 100644 --- a/tests/xsd-examples/valid/issue129.xsd.out +++ b/tests/xsd-examples/valid/issue129.xsd.out @@ -14,7 +14,7 @@ type List struct { } // Element -type List struct { +type List1 struct { XMLName xml.Name `xml:"List"` Ddd []int `xml:",any"`