Skip to content

Commit

Permalink
Bugfix: Skip reading same file multiple times
Browse files Browse the repository at this point in the history
Also merge types from schemas read from multiple files
  • Loading branch information
vraut-pdgc committed Dec 6, 2021
1 parent fdbe082 commit 5b138fb
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
9 changes: 8 additions & 1 deletion xsd/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,14 @@ func Parse(docs ...[]byte) ([]Schema, error) {
if err := s.parse(root); err != nil {
return nil, err
}
parsed[tns] = s
// schema already exists, so merge types with new ones
if ps, ok := parsed[tns]; ok {
for name, newType := range s.Types {
ps.Types[name] = newType
}
} else {
parsed[tns] = s
}
}

for _, s := range parsed {
Expand Down
9 changes: 8 additions & 1 deletion xsdgen/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,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 @@ -62,14 +63,20 @@ func (cfg *Config) GenAST(files ...string) (*ast.File, error) {
return code.GenAST()
}

func (cfg *Config) readFiles(files ...string) ([][]byte,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
}

b, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
cfg.debugf("read %s", filename)
cfg.filesRead[filename] = true
if cfg.followImports {
importedRefs, err := xsd.Imports(b)
if err != nil {
Expand Down
11 changes: 7 additions & 4 deletions xsdgen/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ import (
// A Config holds user-defined overrides and filters that are used when
// generating Go source code from an xsd document.
type Config struct {
logger Logger
loglevel int
namespaces []string
pkgname string
logger Logger
loglevel int
namespaces []string
pkgname string
// load xsd imports recursively into memory before parsing
followImports bool
preprocessType typeTransform
Expand All @@ -42,6 +42,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

0 comments on commit 5b138fb

Please sign in to comment.