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

Generates bad enum constant if enum value is all punctuation #376

Open
mgabeler-lee-6rs opened this issue Jan 29, 2025 · 1 comment
Open

Comments

@mgabeler-lee-6rs
Copy link

Given a field like:

				"logic": {
					"type": "string",
					"enum": ["<", "<=", "==", "!=", ">", ">="]
				},

The generated code looks something like:

const Logic Logic = "=="
const Logic Logic = "!="
const Logic Logic = ">"
const Logic Logic = "<"
const Logic Logic = ">="
const Logic Logic = "<="

which is obviously invalid.

It looks like Caser.Identifierize needs handling for the case when the initially computed identifier is the same as the type name, and schemaGenerator.makeEnumType might also need some handling if multiple enum values map to the same constant name, to append _1,_2 etc or some other disambiguator.

@bbrcan
Copy link

bbrcan commented Feb 24, 2025

I've got the same issue. I've got an enum for timezones, with values copied from tzdata. I end up with this:

const TimezoneEtcGMT0 Timezone = "Etc/GMT0"
const TimezoneEtcGMT0 Timezone = "Etc/GMT+0"
const TimezoneEtcGMT0 Timezone = "Etc/GMT-0"
const TimezoneEtcGMT1 Timezone = "Etc/GMT+1"
const TimezoneEtcGMT1 Timezone = "Etc/GMT-1"
const TimezoneEtcGMT10 Timezone = "Etc/GMT+10"
const TimezoneEtcGMT10 Timezone = "Etc/GMT-10"
const TimezoneEtcGMT11 Timezone = "Etc/GMT+11"
const TimezoneEtcGMT11 Timezone = "Etc/GMT-11"
const TimezoneEtcGMT12 Timezone = "Etc/GMT-12"
const TimezoneEtcGMT12 Timezone = "Etc/GMT+12"

Which as you can see generate duplicate names, resulting in errors like TimezoneEtcGMT0 redeclared in this block

In my case, this would be resolved by some substitutions:

  • +Plus
  • -Minus

But then at the same time, I don't really want to substitute a / with ForwardSlash.

My current workaround is after running go-jsonschema I just use sed to remove the const declarations altogether as I don't need them.

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

2 participants