Comments (3)
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.
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.
Great!
from go-jsonschema.
Related Issues (20)
- Object Properties do not generate valid Unmarshal code
- Handling of `required` properties in objects HOT 1
- Handling duplicate types HOT 15
- Enum integers are generated as floats. HOT 5
- local file ref failure HOT 4
- Fails to generate schema for github-workflow HOT 3
- Go install complains on go.mod HOT 4
- `maxLength` is not working as expected according to the jsonschema docs HOT 3
- Generated Unmarshal for fields with `time.Time` HOT 5
- How to specify a root directory for `$ref` resolution? HOT 1
- CVE Schema: could not generate type for field "...": definition does not exist in schema: HOT 6
- feat: Use of project as package
- Nested struct support? HOT 1
- Fails to generate for Signal K, cannot generate referenced type. HOT 1
- Nullable nested object support
- Incorrect string length validation for some strings. HOT 1
- Does not generate anything for the GeoJson schema
- improve support for additionalProperties HOT 1
- Problems generating structs from schema.
- No option to indicate an empty value for optional array field
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-jsonschema.