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

[FEATURE]: Optionally append enum type name to Golang enum constants #2624

Open
alpoi-x opened this issue Jun 29, 2024 · 0 comments
Open

[FEATURE]: Optionally append enum type name to Golang enum constants #2624

alpoi-x opened this issue Jun 29, 2024 · 0 comments

Comments

@alpoi-x
Copy link

alpoi-x commented Jun 29, 2024

Context (Input, Language)

Input Format: schema
Output Language: go

Description

When generating go code from schemas within a single package, different enums with the same named members will conflict with each-other. With a --enum-type-name-suffix flag (default false), we can instead append the enum type name to the end of the constant (which is a common approach to avoid these conflicts). See a worked example below 😁

Current Behaviour / Output

Consider the following json schemas:

{
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "$id": "tree_part.json",
    "title": "TreePart",
    "additionalProperties": false,
    "type": "string",
    "enum": ["BARK", "LEAF", "ROOT"]
}
{
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "$id": "dog_noise.json",
    "title": "DogNoise",
    "additionalProperties": false,
    "type": "string",
    "enum": ["BARK", "GROWL", "HOWL"]
}

Currently, running the following two commands:

quicktype --src-lang schema --src "schemas/dog_noise.json" --lang go --just-types-and-package --package types --multi-file-output --out "schemas/DogNoise.go"
quicktype --src-lang schema --src "schemas/tree_part.json" --lang go --just-types-and-package --package types --multi-file-output --out "schemas/TreePart.go"

will generate the following files:

package types

type TreePart string

const (
	Bark TreePart = "BARK"
	Leaf TreePart = "LEAF"
	Root TreePart = "ROOT"
)
package types

type DogNoise string

const (
	Bark  DogNoise = "BARK"
	Growl DogNoise = "GROWL"
	Howl  DogNoise = "HOWL"
)

which will cause a redeclared in this block error.

Proposed Behaviour / Output

When passing an additional flag --enum-type-name-suffix to the quicktype command, generate:

package types

type TreePart string

const (
	BarkTreePart TreePart = "BARK"
	LeafTreePart TreePart = "LEAF"
	RootTreePart TreePart = "ROOT"
)
package types

type DogNoise string

const (
	BarkDogNoise  DogNoise = "BARK"
	GrowlDogNoise DogNoise = "GROWL"
	HowlDogNoise  DogNoise = "HOWL"
)

Solution

a two line change (that I'll PR after submitting this issue)

Alternatives

Generating with different package names would "resolve" the conflicts, but does not work for our use case (we have many enums that conflict with each-other in this manner, and would rather not create a separate package for each).

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

No branches or pull requests

1 participant