Skip to content

Commit

Permalink
Merge pull request #6 from henryolik/dev
Browse files Browse the repository at this point in the history
Rename types with identical name so they all generate properly
  • Loading branch information
henryolik authored Oct 19, 2023
2 parents 87ba3bc + f56e8ac commit 7299538
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
28 changes: 28 additions & 0 deletions xsd/xsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,34 @@ func XMLName(t Type) xml.Name {
panic(fmt.Sprintf("xsd: unexpected xsd.Type %[1]T %[1]v passed to XMLName", t))
}

func XMLNamePtr(t Type) *xml.Name {
switch t := t.(type) {
case *SimpleType:
return &t.Name
case *ComplexType:
return &t.Name
case Builtin:
return &xml.Name{Space: t.Name().Space, Local: t.Name().Local}
case linkedType:
return &xml.Name{Space: t.Space, Local: t.Local}
}
panic(fmt.Sprintf("xsd: unexpected xsd.Type %[1]T %[1]v passed to XMLNamePtr", t))
}

func Elements(t Type) *[]Element {
switch t := t.(type) {
case *SimpleType:
return &[]Element{}
case *ComplexType:
return &t.Elements
case Builtin:
return &[]Element{}
case linkedType:
return &[]Element{}
}
panic(fmt.Sprintf("xsd: unexpected xsd.Type %[1]T %[1]v passed to Elements", t))
}

// Base returns the base type that a Type is derived from.
// If the value is of type Builtin, Base will return nil.
func Base(t Type) Type {
Expand Down
6 changes: 6 additions & 0 deletions xsdgen/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func (cfg *Config) GenCode(data ...[]byte) (*Code, error) {
// GenAST creates an *ast.File containing type declarations and
// associated methods based on a set of XML schema.
func (cfg *Config) GenAST(files ...string) (*ast.File, error) {
cfg.filesRead = make(map[string]bool)
data, err := cfg.readFiles(files...)
code, err := cfg.GenCode(data...)
if err != nil {
Expand All @@ -66,6 +67,10 @@ func (cfg *Config) GenAST(files ...string) (*ast.File, error) {
func (cfg *Config) readFiles(files ...string) ([][]byte, error) {
data := make([][]byte, 0, len(files))
for _, filename := range files {
if _, ok := cfg.filesRead[filename]; ok {
// skip reading the file again
continue
}
path, err := filepath.Abs(filename)
if err != nil {
return nil, err
Expand All @@ -75,6 +80,7 @@ func (cfg *Config) readFiles(files ...string) ([][]byte, error) {
return nil, err
}
cfg.debugf("read %s(%s)", path, filename)
cfg.filesRead[filename] = true
if cfg.followImports {
dir := filepath.Dir(path)
importedRefs, err := xsd.Imports(b)
Expand Down
3 changes: 3 additions & 0 deletions xsdgen/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ type Config struct {
// if populated, only types that are true in this map
// will be selected.
allowTypes map[xml.Name]bool

// keep track of files that are read already to avoid reading it again
filesRead map[string]bool
}

type typeTransform func(xsd.Schema, xsd.Type) xsd.Type
Expand Down
23 changes: 23 additions & 0 deletions xsdgen/xsdgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,30 @@ func (cfg *Config) gen(primaries, deps []xsd.Schema) (*Code, error) {
}
}

rename := make(map[xml.Name]string)
types := make(map[string]string)

// Check for any duplicate names between namespaces
for i, primary := range primaries {
for k, _ := range primary.Types {
if !strings.HasPrefix(k.Local, "_") && types[k.Local] != "" && types[k.Local] != k.Space {
rename[k] = k.Local + fmt.Sprint(i)
} else {
types[k.Local] = k.Space
}
}
}

for _, primary := range primaries {
// Rename duplicate types so they generate properly
for _, v := range primary.Types {
for i := range *xsd.Elements(v) {
if val, exists := rename[*xsd.XMLNamePtr((*xsd.Elements(v))[i].Type)]; exists {
xsd.XMLNamePtr((*xsd.Elements(v))[i].Type).Local = val
}
}
}

cfg.debugf("flattening type hierarchy for schema %q", primary.TargetNS)
types := cfg.flatten(primary.Types)
types = cfg.expandComplexTypes(types)
Expand Down

0 comments on commit 7299538

Please sign in to comment.