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

support for generating enum types #83

Open
chrisguiney opened this issue Jan 30, 2022 · 0 comments
Open

support for generating enum types #83

chrisguiney opened this issue Jan 30, 2022 · 0 comments

Comments

@chrisguiney
Copy link

Given an input like

{
  "$schema" : "http://json-schema.org/draft-04/schema#",
  "title" : "myObject",
  "type" : "object",
  "additionalProperties" : false,
  "properties" : {
    "type" : {
      "type" : "string",
      "enum" : [ "x", "y", "z" ],
      "default" : "x"
    }
}

this would currently get generated like

type MyObject {
    Type string `json:"type"`
}

It'd be really handy to generate a type for the enum, such as:

type MyObjectType string

const (
    MyObjectType_X  MyObjectType = "x"
    MyObjectType_Y = "y"
    MyObjectType_Z = "z"
)

type MyObject {
    Type MyObjectType `json:"type"`
}

Where MyObjectType would have a MarshalJSON/UnmarshalJSON that validated that the value was a valid MyObjectType, and produce an error if it was not.

If there's concerns about backwards compatibility, some additional niceties might be to keep the Type field as a string, but add validation to MarshalJSON/UnmarshalJSON to ensure it's a valid value, and error if it was not.

This is something I can probably dedicate a little bit of time into imlpementing, but I'm not sure what approach would be best in the eyes of the maintainer, or if there are other options I hadn't considered that may be desirable.

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

No branches or pull requests

1 participant