Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

teach xsd.Schema how to merge type maps #132

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion xsd/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,14 @@ func Parse(docs ...[]byte) ([]Schema, error) {
if err := s.parse(root); err != nil {
return nil, err
}
parsed[tns] = s
if existingSchema, found := parsed[tns]; found {
err = existingSchema.MergeTypes(&s)
if err != nil {
return nil, fmt.Errorf("error merging types into existing schema: %v", err)
}
} else {
parsed[tns] = s
}
}

for _, s := range parsed {
Expand Down
14 changes: 14 additions & 0 deletions xsd/xsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"fmt"
"io"
"regexp"
"strings"
"time"

"aqwari.net/xml/xmltree"
Expand Down Expand Up @@ -110,6 +111,19 @@ type Schema struct {
Doc string
}

// MergeTypes adds the types from the incoming schema into
// the receiving schema, returns error if the schemas have
// different target namespaces.
func (s *Schema) MergeTypes(incoming *Schema) error {
if !strings.EqualFold(s.TargetNS, incoming.TargetNS) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was under the impression that XML namespaces are case sensitive. What is the reason for using EqualFold over == here?

return fmt.Errorf("cannot merge types from %s into %s", incoming.TargetNS, s.TargetNS)
}
for n,t := range incoming.Types {
s.Types[n] = t
}
return nil
}

// FindType looks for a type by its canonical name. In addition to the types
// declared in a Schema, FindType will also search through the types that
// a Schema's top-level types are derived from. FindType will return nil if
Expand Down