Skip to content

Commit 77bd8be

Browse files
vraut-pdgcm29h
authored andcommitted
Bugfix: Skip reading same file multiple times
Also merge types from schemas read from multiple files
1 parent d35c3a1 commit 77bd8be

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

xsd/parse.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,14 @@ func Parse(docs ...[]byte) ([]Schema, error) {
153153
if err := s.parse(root); err != nil {
154154
return nil, err
155155
}
156-
parsed[tns] = s
156+
// schema already exists, so merge types with new ones
157+
if ps, ok := parsed[tns]; ok {
158+
for name, newType := range s.Types {
159+
ps.Types[name] = newType
160+
}
161+
} else {
162+
parsed[tns] = s
163+
}
157164
}
158165

159166
for _, s := range parsed {

xsdgen/cli.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ func (cfg *Config) GenCode(data ...[]byte) (*Code, error) {
5555
// GenAST creates an *ast.File containing type declarations and
5656
// associated methods based on a set of XML schema.
5757
func (cfg *Config) GenAST(files ...string) (*ast.File, error) {
58+
cfg.filesRead = make(map[string]bool)
5859
data, err := cfg.readFiles(files...)
5960
code, err := cfg.GenCode(data...)
6061
if err != nil {
@@ -70,11 +71,17 @@ func (cfg *Config) readFiles(files ...string) ([][]byte, error) {
7071
if err != nil {
7172
return nil, err
7273
}
74+
if _, ok := cfg.filesRead[path]; ok {
75+
// skip reading the file again
76+
continue
77+
}
78+
cfg.filesRead[path] = true
79+
7380
b, err := ioutil.ReadFile(path)
7481
if err != nil {
7582
return nil, err
7683
}
77-
cfg.debugf("read %s(%s)", path, filename)
84+
cfg.debugf("read %s", path)
7885
if cfg.followImports {
7986
dir := filepath.Dir(path)
8087
importedRefs, err := xsd.Imports(b)

xsdgen/config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ type Config struct {
4444
// if populated, only types that are true in this map
4545
// will be selected.
4646
allowTypes map[xml.Name]bool
47+
48+
// keep track of files that are read already to avoid reading it again
49+
filesRead map[string]bool
4750
}
4851

4952
type typeTransform func(xsd.Schema, xsd.Type) xsd.Type

0 commit comments

Comments
 (0)