Repository navigation
v1.9.2 - Go: field-initializing constructors, reserved words, declaration order
Go — a class with a field-initializing constructor now survives Dart → Go
Three defects made Dart → Go → parse → run fail — or, in the first case, silently produce a wrong answer — for programs the 1.9.1 fixtures did not cover.
- A field-initializing constructor parameter was dropped. Dart's
Point(this.x, this.y)shorthand generatedfunc NewPoint(x int, y int) *Point { o := &Point{}; return o }— the arguments were never assigned, so every field stayed zero-valued and the program ran to a wrong result rather than failing. The factory now emitso.x = xfor eachthis.parameter, after the field's declared initial value so an explicit argument wins. The long-hand form (Point(int x, int y) { this.x = x; }) was already correct. - A source identifier colliding with a Go keyword emitted invalid Go. A Dart
var map = ..., or a field namedtype, becamemap := .../o.type, which does not parse —mapandtypeare 2 of Go's 25 reserved words. Such a name is now escaped with a trailing_(map_,type_) at every site: declarations, reads, parameters, struct fields, methods, and the members written after a.. - Structs were emitted after the functions that call them. ApolloVM's Go parser resolves
NewPoint(...)against the declarations it has already seen, so a top-level function emitted first could not resolve the struct.generateASTRootnow emits structs (and their factories) before the top-level function block.
Supporting change
New ApolloGenerator.normalizeIdentifier(String) hook, applied by the base generator to the member names written after a . (fields, getters, setters and methods). It is the identity for every language except Go, which uses it to escape reserved words, so a declaration and its uses always agree. Type identifiers keep going through normalizeTypeFunction, so Math.max is untouched.
Tests
4 new tests, each asserting the generated source and running it back through the Go parser. Full suite: 1659 passing (was 1655).
Known gap
ApolloVM's Go parser remains order-dependent: hand-written Go that calls NewPoint(...) before declaring type Point struct still fails to resolve, though Go itself is order-independent. The generator no longer produces that shape.
🤖 Generated with Claude Code