Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add json serialisation support for golang #1848

Merged
merged 13 commits into from
Mar 7, 2024

Conversation

Souvikns
Copy link
Member

@Souvikns Souvikns commented Mar 1, 2024

Description

This PR is adding JSON serialization support by adding struct tags while generating go structs, by creating a new GO_PRESET.

package models

type AsyncApi_3Dot_0Dot_0SchemaDot struct {
  Asyncapi string `json:"asyncapi"`
  Id string `json:"id"`
  Info *Info `json:"info"`
  Servers map[string]interface{} `json:"servers"`
  DefaultContentType string `json:"defaultContentType"`
  Channels map[string]interface{} `json:"channels"`
  Operations map[string]interface{} `json:"operations"`
  Components *Components `json:"components"`
  AdditionalProperties map[string]interface{} `json:"-"`
}

Also adds custom marshaler functions for enums when generating struct-tags

func (op *ApiKeyIn) UnmarshalJSON(raw []byte) error {
	var v any
	if err := json.Unmarshal(raw, &v); err != nil {
		return err
	}
	*op = ValuesToApiKeyIn[v]
	return nil
}

func (op ApiKeyIn) MarshalJSON() ([]byte, error) {
	return json.Marshal(op.Value())
} 
          

Related Issue

Fixes #299

Checklist

  • The code follows the project's coding standards and is properly linted (npm run lint).
  • Tests have been added or updated to cover the changes.
  • Documentation has been updated to reflect the changes.
  • All tests pass successfully locally.(npm run test).

Additional Notes

Copy link

netlify bot commented Mar 1, 2024

Deploy Preview for modelina canceled.

Name Link
🔨 Latest commit 4896d06
🔍 Latest deploy log https://app.netlify.com/sites/modelina/deploys/65e9b619051e880008124b17

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Welcome to AsyncAPI. Thanks a lot for creating your first pull request. Please check out our contributors guide useful for opening a pull request.
Keep in mind there are also other channels you can use to interact with AsyncAPI community. For more details check out this issue.

@Souvikns Souvikns marked this pull request as ready for review March 4, 2024 07:55
Copy link
Member

@jonaslagoni jonaslagoni left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only one comment for the implementation 👍 Looking good 💪

Remember to do parts 4 and 5 of https://github.com/asyncapi/modelina/blob/master/docs/contributing.md#adding-a-new-preset

@coveralls
Copy link

coveralls commented Mar 4, 2024

Pull Request Test Coverage Report for Build 8183449856

Details

  • 17 of 18 (94.44%) changed or added relevant lines in 4 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.03%) to 92.27%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/generators/go/presets/CommonPreset.ts 14 15 93.33%
Totals Coverage Status
Change from base Build 8062206205: -0.03%
Covered Lines: 6010
Relevant Lines: 6342

💛 - Coveralls

@jonaslagoni jonaslagoni changed the base branch from master to next March 7, 2024 11:09
examples/go-json-tags/README.md Outdated Show resolved Hide resolved
examples/go-json-tags/package.json Outdated Show resolved Hide resolved
Copy link

sonarqubecloud bot commented Mar 7, 2024

Quality Gate Passed Quality Gate passed

Issues
0 New issues
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
No data about Duplication

See analysis details on SonarCloud

Copy link
Member

@jonaslagoni jonaslagoni left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

@jonaslagoni
Copy link
Member

/rtm

@jonaslagoni
Copy link
Member

/rtm

@asyncapi-bot asyncapi-bot merged commit 0b0f56e into asyncapi:next Mar 7, 2024
21 checks passed
@jonaslagoni
Copy link
Member

@all-contributors please add @Souvikns for code, test, example, docs

Copy link
Contributor

@jonaslagoni

I've put up a pull request to add @Souvikns! 🎉

@asyncapi-bot
Copy link
Contributor

🎉 This PR is included in version 4.0.0-next.12 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] go generator is unable to handle unions types correctly.
4 participants