Code Monkey home page Code Monkey logo

Comments (3)

atombender avatar atombender commented on May 28, 2024 1

Not sure I understand what you mean. If the field is optional, we use a pointer (*string) because otherwise a missing string would be unmarshalled to an empty string. While Go developers sometimes use an empty string to mean "not present", but this is often not desirable, because that means it's not possible for your code to determine whether the original JSON had an empty string, a null string or a missing key.

Clearly string pointers are less convenient because you have to check whether they're nil, and you could accidentally write code they accessed the value without checking this first. But semantically there's no way around it. (Maybe in the future we could have a flag to control this, though.)

Just to be clear, UnmarshalJSON overrides the JSON decoder's built-in logic. We only generate it if we need to modify its behaviour or inject validation checks. If we don't, we don't generate it, and the normal JSON decoding will take place. In this case, the string field becomes either nil or a string -- it is initialized.

Did I address your concern or did you mean something else?

from go-jsonschema.

c0d3x42 avatar c0d3x42 commented on May 28, 2024

Thank you for detailed response. Yep, its all perfectly clear now. My issue was not understanding go and how pointers worked when UnmarshalJSON was called. My First foray into go was like stumbling in the dark.

from go-jsonschema.

atombender avatar atombender commented on May 28, 2024

Great!

from go-jsonschema.

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.