Skip to content

Commit

Permalink
fix(go): nested types are not namespaced (#2650)
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



---

By submitting this pull request, I confirm that my contribution is made under the terms of the [Apache 2.0 license].

[Apache 2.0 license]: https://www.apache.org/licenses/LICENSE-2.0
  • Loading branch information
RomainMuller authored Mar 12, 2021
1 parent 7006e11 commit 45b527c
Show file tree
Hide file tree
Showing 16 changed files with 3,164 additions and 1,680 deletions.
8 changes: 4 additions & 4 deletions packages/@jsii/go-runtime-test/project/compliance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (suite *ComplianceSuite) TestPrimitiveTypes() {

func (suite *ComplianceSuite) TestUseNestedStruct() {
suite.FailTest("Nested types are not namespaced", "https://github.com/aws/jsii/pull/2650")
scopejsiicalcbase.StaticConsumer_Consume(submodule.NestedStruct{
scopejsiicalcbase.StaticConsumer_Consume(submodule.NestingClass_NestedStruct{
Name: "Bond, James Bond",
})
}
Expand Down Expand Up @@ -273,10 +273,10 @@ func (suite* ComplianceSuite) TestGetAndSetEnumValues() {
calc := calc.NewCalculator(calc.CalculatorProps{})
calc.Add(9)
calc.Pow(3)
assert.Equal(composition.CompositionStringStyle_NORMAL, calc.StringStyle())
assert.Equal(composition.CompositeOperation_CompositionStringStyle_NORMAL, calc.StringStyle())

calc.SetStringStyle(composition.CompositionStringStyle_DECORATED)
assert.Equal(composition.CompositionStringStyle_DECORATED, calc.StringStyle())
calc.SetStringStyle(composition.CompositeOperation_CompositionStringStyle_DECORATED)
assert.Equal(composition.CompositeOperation_CompositionStringStyle_DECORATED, calc.StringStyle())
assert.Equal("<<[[{{(((1 * (0 + 9)) * (0 + 9)) * (0 + 9))}}]]>>", calc.ToString())
}

Expand Down
3 changes: 3 additions & 0 deletions packages/jsii-pacmak/.eslintrc.yaml
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
---
extends: ../../eslint-config.yaml

ignorePatterns:
- test/generated-code/examples/**
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 with the 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.21.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
Loading

0 comments on commit 45b527c

Please sign in to comment.