modelina icon indicating copy to clipboard operation
modelina copied to clipboard

Add json tags to Go generated struct

Open smoya opened this issue 3 years ago • 14 comments

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

smoya avatar Jul 07 '21 11:07 smoya

This issue has been automatically marked as stale because it has not had recent activity :sleeping: It will be closed in 60 days if no further activity occurs. To unstale this issue, add a comment with detailed explanation. Thank you for your contributions :heart:

github-actions[bot] avatar Sep 08 '21 00:09 github-actions[bot]

This issue has been automatically marked as stale because it has not had recent activity :sleeping:

It will be closed in 120 days if no further activity occurs. To unstale this issue, add a comment with a detailed explanation.

There can be many reasons why some specific issue has no activity. The most probable cause is lack of time, not lack of interest. AsyncAPI Initiative is a Linux Foundation project not owned by a single for-profit company. It is a community-driven initiative ruled under open governance model.

Let us figure out together how to push this issue forward. Connect with us through one of many communication channels we established here.

Thank you for your patience :heart:

github-actions[bot] avatar Jan 07 '22 00:01 github-actions[bot]

I think that it's still important? @smoya :)

magicmatatjahu avatar Jan 07 '22 10:01 magicmatatjahu

This issue has been automatically marked as stale because it has not had recent activity :sleeping:

It will be closed in 120 days if no further activity occurs. To unstale this issue, add a comment with a detailed explanation.

There can be many reasons why some specific issue has no activity. The most probable cause is lack of time, not lack of interest. AsyncAPI Initiative is a Linux Foundation project not owned by a single for-profit company. It is a community-driven initiative ruled under open governance model.

Let us figure out together how to push this issue forward. Connect with us through one of many communication channels we established here.

Thank you for your patience :heart:

github-actions[bot] avatar May 08 '22 00:05 github-actions[bot]

@jonaslagoni I think this is still relevant.

smoya avatar May 09 '22 09:05 smoya

This issue has been automatically marked as stale because it has not had recent activity :sleeping:

It will be closed in 120 days if no further activity occurs. To unstale this issue, add a comment with a detailed explanation.

There can be many reasons why some specific issue has no activity. The most probable cause is lack of time, not lack of interest. AsyncAPI Initiative is a Linux Foundation project not owned by a single for-profit company. It is a community-driven initiative ruled under open governance model.

Let us figure out together how to push this issue forward. Connect with us through one of many communication channels we established here.

Thank you for your patience :heart:

github-actions[bot] avatar Sep 07 '22 00:09 github-actions[bot]

This issue has been automatically marked as stale because it has not had recent activity :sleeping:

It will be closed in 120 days if no further activity occurs. To unstale this issue, add a comment with a detailed explanation.

There can be many reasons why some specific issue has no activity. The most probable cause is lack of time, not lack of interest. AsyncAPI Initiative is a Linux Foundation project not owned by a single for-profit company. It is a community-driven initiative ruled under open governance model.

Let us figure out together how to push this issue forward. Connect with us through one of many communication channels we established here.

Thank you for your patience :heart:

github-actions[bot] avatar Jan 27 '23 00:01 github-actions[bot]

Any progress on this? Without this, it renders the code generation for different languages kind of useless as for example the current GOLANG structs are not in sync with their TS counterparts

For ref: Tackled again by https://github.com/asyncapi/modelina/pull/867, but stale?

H3rby7 avatar May 11 '23 20:05 H3rby7

Its still definitely needed... I just dont have the bandwidth to finish the PR, feel free to take it over, happy to help facilitate this 👍

jonaslagoni avatar May 12 '23 11:05 jonaslagoni

It just looks like a lot to me, judging from the amount of code (changes) and I am not sure exactly where to start with it. As far as a (my) simple use case goes it would be a sufficient start to have the json tags simply be the lowerCamelCase variant of the field name in the struct to give us compability to other generated sources

H3rby7 avatar May 12 '23 12:05 H3rby7

@H3rby7 you can always change the naming rules: https://github.com/asyncapi/modelina/blob/master/docs/advanced.md#changing-the-constrain-rules

const generator = new GoGenerator({
  constraints: {
    propertyKey: ({objectPropertyModel}) => {return objectPropertyModel.propertyName;}
  }
});

This means that it takes the input property names directly as is, without making sure any arbitrary input property name is valid in Go.

jonaslagoni avatar May 12 '23 13:05 jonaslagoni

@jonaslagoni do you think someone without any prior idea of code generation will be able to take this up ?

kaushik-rishi avatar May 24 '23 14:05 kaushik-rishi

This issue has been automatically marked as stale because it has not had recent activity :sleeping:

It will be closed in 120 days if no further activity occurs. To unstale this issue, add a comment with a detailed explanation.

There can be many reasons why some specific issue has no activity. The most probable cause is lack of time, not lack of interest. AsyncAPI Initiative is a Linux Foundation project not owned by a single for-profit company. It is a community-driven initiative ruled under open governance model.

Let us figure out together how to push this issue forward. Connect with us through one of many communication channels we established here.

Thank you for your patience :heart:

github-actions[bot] avatar Jan 13 '24 00:01 github-actions[bot]

still valid? @jonaslagoni @smoya

AnimeshKumar923 avatar Jan 14 '24 14:01 AnimeshKumar923