Skip to content

fix(codegen): sanitise XSD enum values for valid Go identifiers - #11

Open
tnymlr wants to merge 1 commit into
way-platform:mainfrom
tnymlr:fix/enum-identifier-sanitisation
Open

tnymlr wants to merge 1 commit into
way-platform:mainfrom
tnymlr:fix/enum-identifier-sanitisation

Conversation

@tnymlr

@tnymlr tnymlr commented Apr 8, 2026

Copy link
Copy Markdown

Problem

toGoName() only splits on _, -, and ., so XSD enumeration values containing spaces, slashes, colons, plus signs, or asterisks produce invalid Go identifiers in generated const names.

Examples of values that break code generation:

  • "EIR Sync Error"ExceptionTypeEIR Sync Error (space)
  • "application/xml"MediaTypeCodeApplication/xml (slash)
  • "application/atom+xml"MediaTypeCodeApplication/atom+xml (slash, plus)
  • "*/*"MediaTypeCode*/* (asterisk, slash)
  • "1:M"ExceptionType1:M (colon, digit-start)

This causes go/format to reject the generated source with errors like:

unparsable Go source: 367:30: expected ';', found ':'

Fix

Change toGoName() to split on any character that is not a letter or digit (!unicode.IsLetter(r) && !unicode.IsDigit(r)). This handles all non-identifier characters uniformly.

Additional changes:

  • Values that produce digit-leading identifiers are prefixed with V (e.g. "1.0"V10)
  • Values that are entirely non-alphanumeric produce "Value" as a fallback
  • Original casing within each word part is preserved to maintain existing camelCase names like GetWeather and DownloadRequest

Test plan

  • Added TestToGoName with 30 test cases covering spaces, slashes, colons, plus signs, asterisks, MIME types, charset names, and digit-leading values
  • Updated golden files for the two cases affected by the change (digit-prefixed enum consts, preserved camelCase in inline type names)
  • Full test suite passes
  • Tested against real-world Optus wholesale WSDL files containing these enum patterns

toGoName now splits on any non-letter, non-digit character instead of
only underscore, hyphen, and dot. This handles XSD enumeration values
containing spaces ("EIR Sync Error"), slashes ("application/xml"),
colons ("1:M"), plus signs ("application/atom+xml"), and asterisks
("*/*") that previously produced unparsable Go source.

Values starting with a digit after sanitisation are prefixed with "V"
to remain valid Go identifiers (e.g. "1.0" → "V10").

Original casing within each word part is preserved to maintain
camelCase names like "GetWeather" and "DownloadRequest".
@tnymlr
tnymlr force-pushed the fix/enum-identifier-sanitisation branch from 1fe4213 to 0766525 Compare April 8, 2026 04:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant