Skip to content

Commit

Permalink
fix(go): nested types are not namespaced
Browse files Browse the repository at this point in the history
Nested types were generated in the same package as they parent, without
any namespacing additions, meaning that if two types in the same package
have nested types with the same name, the generated code would be
invalid.

This namespaces the nested types in go by prefixing their names with
their nesting type's name, using an `_` delimiter, which is the same as
what is done for static methods.

Also added a validation in the `jsii` compiler that prohibits that a
nested type and a static method share the same PascalCase
transformation, as this would result in conflicts in Go, but also in C#.

Fixes #2649
  • Loading branch information
RomainMuller committed Mar 4, 2021
1 parent 2781ff4 commit 0c52cf3
Show file tree
Hide file tree
Showing 14 changed files with 2,126 additions and 1,676 deletions.
19 changes: 15 additions & 4 deletions packages/jsii-pacmak/lib/targets/go/types/go-type.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CodeMaker, toCamelCase, toPascalCase } from 'codemaker';
import { CodeMaker, toPascalCase } from 'codemaker';
import { Type } from 'jsii-reflect';

import { EmitContext } from '../emit-context';
Expand All @@ -14,9 +14,20 @@ export abstract class GoType {

public constructor(public pkg: Package, public type: Type) {
this.name = toPascalCase(type.name);
// add "_jsiiProxy" postfix to private struct name to avoid keyword
// conflicts such as "default". see https://github.com/aws/jsii/issues/2637
this.proxyName = `${toCamelCase(type.name)}_jsiiProxy`;

// Prefix witht he nesting parent name(s), using an _ delimiter.
for (
let parent = type.nestingParent;
parent != null;
parent = parent.nestingParent
) {
this.name = `${toPascalCase(parent.name)}_${this.name}`;
}

// Add "jsiiProxy_" prefix to private struct name to avoid keyword conflicts
// such as "default". See https://github.com/aws/jsii/issues/2637
this.proxyName = `jsiiProxy_${this.name}`;

this.fqn = type.fqn;
}

Expand Down
1 change: 1 addition & 0 deletions packages/jsii-pacmak/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"@types/yargs": "^16.0.0",
"eslint": "^7.20.0",
"jest": "^26.6.3",
"jsii": "^0.0.0",
"jsii-build-tools": "^0.0.0",
"jsii-calc": "^3.20.120",
"prettier": "^2.2.1",
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 0c52cf3

Please sign in to comment.