Add json tags to Go generated struct
See original GitHub issueReason/Context
Current generated Go structs does not have json tags. Those are useful for encoding/decoding JSON.
Description
Currently, for a given doc like:
const doc = {
$id: "Address",
type: "object",
properties: {
street_name: { type: "string" },
city: { type: "string", description: "City description" },
house_number: { type: "number" },
marriage: { type: "boolean", description: "Status if marriage live in given house" },
pet_names: { type: "array", items: { type: "string" } },
state: { type: "string", enum: ["Texas", "Alabama", "California", "other"] },
},
required: ["street_name", "city", "state", "house_number", "state"],
};
The generator generates the following struct:
// Address represents a Address model.
type Address struct {
StreetName string
City string
HouseNumber float64
Marriage bool
PetNames []string
State *State
}
// State represents an enum of string.
type State string
After adding Json tags would look like:
// Address represents a Address model.
type Address struct {
StreetName string `json:"street_name"`
City string `json:"city"`
HouseNumber float64 `json:"house_number"`
Marriage bool `json:"marriage"`
PetNames []string `json:"pet_names"`
State *State `json:"state"`
}
// State represents an enum of string.
type State string
Related issue
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:6 (6 by maintainers)
Top Results From Across the Web
How To Use Struct Tags in Go - DigitalOcean
The JSON encoder in the standard library makes use of struct tags as annotations indicating to the encoder how you would like to...
Read more >How do I dynamically change the struct's json tag?
Implement the json.Marshaler (golang.org/pkg/encoding/json/#Marshaler) interface and you can ignore struct tags or name your fields whatever you like.
Read more >Generate struct tags for golang - IntelliJ IDEs Plugin
Edit struct tags for golang. Include: standard json tags; standard bson tags; standard both tags; camel json tags; camel bson tags; camel both...
Read more >go:generate to automate json tag generation for Go structs
Here is a way you can use it to automatically generate json,xml,sql or any other tags for your struct fields. Usually you would...
Read more >Go struct tags - working with struct tags in Golang - ZetCode
In the next example, we use the json struct tag with the encoding/json package. ... The example uses struct tags to configure how...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@jonaslagoni I think this is still relevant.
I think that it’s still important? @smoya 😃