Comments (10)
I think it'd be immensely helpful to merge the omitempty-structs
branch to resolve this. Currently, the generated output is far too noisy, especially for complex schemas. I don't mind adding null guards if necessary, since it allows you to differentiate between null and empty data.
For what it's worth, other projects like gqlgen make pointers the default for generated structs, and expose a setting to omit non-pointer structs.
from go-xml.
What if a nullable struct was generated for each type. The nullable struct would conditionally be used if a field is nullable or optional. Something like:
type Foo struct {
StringField string
IntField int
}
type FooNullable struct {
Foo
Valid bool
}
type Bar {
RequiredField Foo
OptionalField FooNullable
}
I think this provides a number of advantages:
- Prevents the need for nil guards
- Explicitly set if a zero value should be marshaled
- Explicitly see if a zero value was unmarshaled
- Marshal and Unmarshal functions would be fairly trivial
from go-xml.
I've successfully generated structs from an xsd. However when I marshal the struct, which is full of optional fields it renders a huge xml file with even empty fields still rendering. If I'm interested in a solution now to ignore empty fields, what's the quickest way to implement solution 1? I think pointers for all struct fields is the easiest way? I much prefer using pointers as the way to leave a node undefined. Omitempty doesn't do what I want. Sometimes you need a zero value to come thru, such as setting quantity
to 0
, we don't want that to be ignored. Thanks!
from go-xml.
Using pointers for all struct fields is the easiest way, I think.
@homanchou , I've just pushed the branch omitempty-structs
which makes this change. Can you give it a try and let me know if it meets your needs?
If this approach works out, I think I'd like to hide it behind a non-default config flag.
from go-xml.
@homanchou , have you been able to try the changes made in the omitempty-structs
branch? If not, could you share the XSD and sample data you used, so I can make sure it fixes the issue?
from go-xml.
@droyo I've been able to use the omitempty-structs
branch on a number of complex schemas with out any issues.
The xsd and wsdl files for the services I'v been working with can be found here.
from go-xml.
I have a similar issue, except the struct is a time.Time
. I just tried the omitempty-structs
branch, but it doesn't cover that scenario.
from go-xml.
There has been some movement on golang/go#11939 which would resolve our problem. That said, that issue has been open for 3 years, and I don't think everyone will adopt Go 1.12 immediately, so it's still worth having a workaround here.
from go-xml.
the omitempty-structs branch implements solution 1, using pointers for all fields. this seems to work well for those who have used it, so i'm thinking we can move it into the master
branch and put it behind a flag.
from go-xml.
What happened with this issue? Would be great to have it merged.
from go-xml.
Related Issues (20)
- Add "Code generated ..." comment line automatically HOT 1
- Allow wsdlgen to take build tags HOT 2
- 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
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.