Giter VIP home page Giter VIP logo

Comments (10)

kdeloach avatar kdeloach commented on September 4, 2024 3

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.

ianlopshire avatar ianlopshire commented on September 4, 2024

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.

homanchou avatar homanchou commented on September 4, 2024

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.

droyo avatar droyo commented on September 4, 2024

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.

droyo avatar droyo commented on September 4, 2024

@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.

ianlopshire avatar ianlopshire commented on September 4, 2024

@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.

leth avatar leth commented on September 4, 2024

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.

droyo avatar droyo commented on September 4, 2024

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.

droyo avatar droyo commented on September 4, 2024

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.

agusti-t avatar agusti-t commented on September 4, 2024

What happened with this issue? Would be great to have it merged.

from go-xml.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.