Skip to content

Commit

Permalink
Register imported modules
Browse files Browse the repository at this point in the history
Whenever an element from foreign module is imported we make a note about the
module import needed.
  • Loading branch information
isimluk committed May 25, 2020
1 parent 2ede605 commit 8e31384
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions pkg/xsd/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ import (

// Schema is the root XSD element
type Schema struct {
XMLName xml.Name `xml:"http://www.w3.org/2001/XMLSchema schema"`
Xmlns Xmlns `xml:"-"`
TargetNamespace string `xml:"targetNamespace,attr"`
Imports []Import `xml:"import"`
Elements []Element `xml:"element"`
Attributes []Attribute `xml:"attribute"`
ComplexTypes []ComplexType `xml:"complexType"`
XMLName xml.Name `xml:"http://www.w3.org/2001/XMLSchema schema"`
Xmlns Xmlns `xml:"-"`
TargetNamespace string `xml:"targetNamespace,attr"`
Imports []Import `xml:"import"`
Elements []Element `xml:"element"`
Attributes []Attribute `xml:"attribute"`
ComplexTypes []ComplexType `xml:"complexType"`
importedModules map[string]bool `xml:"-"`
}

func Parse(xsdPath string) (*Schema, error) {
Expand All @@ -25,7 +26,7 @@ func Parse(xsdPath string) (*Schema, error) {
}
defer f.Close()

var schema Schema
schema := Schema{importedModules: map[string]bool{}}
d := xml.NewDecoder(f)

if err := d.Decode(&schema); err != nil {
Expand Down Expand Up @@ -71,6 +72,10 @@ func (sch *Schema) findReferencedElement(ref reference) *Element {
if innerSchema == nil {
panic("Internal error: referenced element '" + ref + "' cannot be found.")
}
if innerSchema != sch {
sch.registerImportedModule(innerSchema)

}
return innerSchema.GetElement(ref.Name())
}

Expand Down Expand Up @@ -136,6 +141,10 @@ func (sch *Schema) GoImportsNeeded() []string {
return []string{"encoding/xml"}
}

func (sch *Schema) registerImportedModule(module *Schema) {
sch.importedModules[module.GoPackageName()] = true
}

type Import struct {
XMLName xml.Name `xml:"http://www.w3.org/2001/XMLSchema import"`
Namespace string `xml:"namespace,attr"`
Expand Down

0 comments on commit 8e31384

Please # to comment.