question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add json tags to Go generated struct

See original GitHub issue

Reason/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

https://github.com/asyncapi/modelina/issues/262

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:1
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
smoyacommented, May 9, 2022

@jonaslagoni I think this is still relevant.

2reactions
magicmatatjahucommented, Jan 7, 2022

I think that it’s still important? @smoya 😃

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found