Comments (4)
A possible compromise would be to add constants of all possible values for enumerations (without validation during unmarshalling). That will help with type safety when using these values later on.
from go-xml.
My concern with this is creating an explosion in the amount of generated code and even the number of types. One feature of the xsd package is that it tries to use the builtin Go types if introducing a new type adds no value (for some definition of "value"). I didn't intend for this package to be used for validation, as that is far more work. But let's give this a try.
For some of the restrictions, there are many ways to do this.
Enumerations
Consider the following type (taken from w3schools):
<xs:element name="car">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="Audi"/>
<xs:enumeration value="Golf"/>
<xs:enumeration value="BMW"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
Here's what xsdgen
does today:
// Must be one of Audi, Golf, BMW
type Car string
Here's one possible solution:
// Must be one of Audi, Golf, BMW
type Car string
var validCar = map[Car]struct{} {
"Audi": struct{},
"Golf": struct{},
"BMW": struct{},
}
func (c Car) MarshalText() ([]byte, error) {
if _, ok := validCar[c]; !ok {
return nil, fmt.Errorf("%q is not a valid %T", c, c)
}
return []byte(c), nil
}
func (c *Car) UnmarshalText(text []byte) error {
if _, ok := validCar[Car(text)]; !ok {
return fmt.Errof("%q is not a valid %T", text, c)
}
*c = Car(text)
return nil
}
- Pro: validation is simple and "generic" for all comparable types
- Pro: it's obvious that Car is some kind of string
- Con: have to write []byte -> int, []byte -> float, []byte -> byte, and any other base type conversions (we already do this for handling list simpleTypes).
Another option mapping to an int:
type Car int
const (
Audi Car = iota
Golf
BMW
)
var validCars = map[string]Car{
"Audi": Audi,
"Golf": Golf,
"BMW": BMW,
}
func (c *Car) UnmarshalText(text []byte) error {
if x, ok := validCars[string(text)]; ok {
*c = x
return nil
}
return fmt.Errorf("%q is an invalid %T", text, c)
}
func (c Car) MarshalText() ([]byte, error) {
return []byte(c.String()), error
}
- Pro: Arguably easier to work with int-based enum, rather than string.
- Pro: Set of valid Car's exposed at the package level
- Pro: Can leverage stringer to make the mapping for
MarshalText
- Con: Have to make sure enum vals are valid Go identifiers. Will interfere with stringer.
- Con: High likelihood of name collisions may have to use hungarian notation
Patterns
The regular expressions defined by XSD seem pretty close to RE2, used by the regexp
package. I would be satisfied with opportunistically generating Marshal/Unmarshal methods iff the regexp
package can compile the pattern.
Assertion
Nope. Not worth it. I don't want to link in an xpath implementation just to validate an xml type.
Other restrictions:
Min/max inclusive/exclusive could be useful, and should be easy enough to implement.
The whitespace restriction is necessary, I think, as it changes how a value should be parsed.
explicitTimezone? pass :D
from go-xml.
I actually went ahead and did already implement some of these.
But I'm still struggling with a few other things related to the MPEG-DASH XSD I mentioned in #16.
In particular, some types may contain both an element and an attribute called "title". xsdgen is currently not capable of generating separate fields for this case.
from go-xml.
I opened #23 for that
from go-xml.
Related Issues (20)
- xsdgen: gen type "measResults": Unknown built-in type "anySimpleType" HOT 6
- Timezone is not considered in MarshalText method HOT 4
- Schema with restriction and base="xsd:dateTime" HOT 1
- Unable to unmarshal embedded xhtml HOT 3
- could not find base type anySimpleType in namespace http://www.w3.org/2001/XMLSchema HOT 3
- Cannot use non-namespaced (but included) types in schema HOT 4
- complexType DetailedHashSum: could not find type "BinaryDataType" in namespace http://service.ddex.net/xml/avs/avs for element DataType HOT 3
- complexType localElement: could not find type in namespace for attribute substitutionGroup HOT 11
- utf-8 name in xsd HOT 3
- Issue with `type="xsd:date" maxOccurs="unbounded"` HOT 1
- Failed to parse chardata. HOT 2
- Element ref not generating and linking types correctly. HOT 3
- question about walking the xsd HOT 3
- Does not generate code HOT 1
- Tests generated for `gentests/books/books_test.go` do not pass
- replace rule not working to create shared types
- aqwari.net is down HOT 1
- xsdgen problem with string
- Fix simpleType length restriction
- <xsd:attribute ref="xml:space" use="required"> Reference attribute conversion failed
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from go-xml.