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

simplify some types of union #141

Open
wants to merge 1 commit 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions xsdgen/testdata/redundant-union.go.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Code generated by xsdgen.test. DO NOT EDIT.

package ws

import "encoding/xml"

// May be one of A, B, C
type EnumType string

const (
EnumType_A EnumType = "A"
EnumType_B EnumType = "B"
EnumType_C EnumType = "C"
)

type Type1 struct {
Elem []EnumType `xml:"Elem,omitempty"`
}
25 changes: 25 additions & 0 deletions xsdgen/testdata/redundant-union.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<!-- edited with XMLSpy v2008 (http://www.altova.com) by A (A) -->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://example.org/" targetNamespace="http://example.org/">
<xs:element name="Type1">
<xs:complexType>
<xs:sequence minOccurs="1" maxOccurs="1">
<xs:element name="Elem" type="redundantUnionType" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>

<xs:simpleType name="enumType">
<xs:restriction base="xs:string">
<xs:enumeration value="A"/>
<xs:enumeration value="B"/>
<xs:enumeration value="C"/>
</xs:restriction>
</xs:simpleType>

<xs:simpleType name="redundantUnionType">
<xs:union memberTypes="enumType xs:string"/>
</xs:simpleType>

</xs:schema>
55 changes: 54 additions & 1 deletion xsdgen/xsdgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,54 @@ func dedup(types []xsd.Type) (unique []xsd.Type) {
return unique
}

// for now, bustUnion only resolves simple cases like a union of a builtin and an enum of the same builtin, e.g. in
// this case redundantUnionType will be replaced with enumType
//
// <xs:simpleType name="enumType">
// <xs:restriction base="xs:string">
// <xs:enumeration value="A"/>
// <xs:enumeration value="B"/>
// </xs:restriction>
// </xs:simpleType>
//
// <xs:simpleType name="redundantUnionType">
// <xs:union memberTypes="enumType xs:string"/>
// </xs:simpleType>
func (cfg *Config) bustUnion(t *xsd.SimpleType) xsd.Type {
var underlying xsd.Type
var actual xsd.Type
for _, member := range t.Union {
switch ut := member.(type) {
case xsd.Builtin:
if underlying != nil && underlying != ut {
// can't simplify
return t
}
underlying = ut
case *xsd.SimpleType:
base := xsd.Base(ut)
if underlying != nil && underlying != ut {
// can't simplify
return t
}
if actual != nil && actual != ut {
// TODO: can we simplify this case?
return t
}
underlying = base
actual = ut
default:
// TODO: handle more union member types
return t
}
}

if actual != nil {
return actual
}
return underlying
}

func (cfg *Config) flatten1(t xsd.Type, push func(xsd.Type), depth int) xsd.Type {
const maxDepth = 1000
if depth > maxDepth {
Expand All @@ -414,7 +462,12 @@ func (cfg *Config) flatten1(t xsd.Type, push func(xsd.Type), depth int) xsd.Type
base, builtin xsd.Type
ok bool
)
// TODO: handle list/union types
// TODO: handle list types

if len(t.Union) > 0 {
return cfg.bustUnion(t)
}

for base = xsd.Base(t); base != nil; base = xsd.Base(base) {
if builtin, ok = base.(xsd.Builtin); ok {
break
Expand Down