Skip to content

Commit

Permalink
fix(go): duplicate conversion functions when parent structs have the …
Browse files Browse the repository at this point in the history
…same base name (#2697)

If a struct has two parent structs with the same base name (but different packages), the emitted conversion function will have the same name (`ToParentStruct` and `ToParentStruct`).

Since we are not even sure that these base conversion functions are required, omit them for now and we can decide to restore them at a later stage if the use case is clearer.

Fixes #2692
  • Loading branch information
Elad Ben-Israel authored Mar 15, 2021
1 parent c9a36a6 commit 52bd510
Show file tree
Hide file tree
Showing 15 changed files with 1,559 additions and 132 deletions.
2 changes: 2 additions & 0 deletions packages/jsii-calc/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ export * as onlystatic from './only-static';
export * as nodirect from './no-direct-types';
export * as module2647 from './module2647';
export * as module2689 from './module2689';
export * as module2692 from './module2692';
export * as module2530 from './module2530';
17 changes: 17 additions & 0 deletions packages/jsii-calc/lib/module2530/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Verifies a method with parameters "_" can be generated.
* @see https://github.com/aws/jsii/issues/2530
*/
export class MyClass {
public static bar(_: boolean) {
return;
}

public constructor(_: number) {
return;
}

public foo(_: string) {
return;
}
}
2 changes: 2 additions & 0 deletions packages/jsii-calc/lib/module2692/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * as submodule1 from './submodule1';
export * as submodule2 from './submodule2';
3 changes: 3 additions & 0 deletions packages/jsii-calc/lib/module2692/submodule1/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface Bar {
readonly bar1: string;
}
9 changes: 9 additions & 0 deletions packages/jsii-calc/lib/module2692/submodule2/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Bar as Bar1 } from '../submodule1';

export interface Bar {
readonly bar2: string;
}

export interface Foo extends Bar, Bar1 {
readonly foo2: string;
}
198 changes: 197 additions & 1 deletion packages/jsii-calc/test/assembly.jsii
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,12 @@
"line": 142
}
},
"jsii-calc.module2530": {
"locationInModule": {
"filename": "lib/index.ts",
"line": 17
}
},
"jsii-calc.module2647": {
"locationInModule": {
"filename": "lib/index.ts",
Expand Down Expand Up @@ -247,6 +253,24 @@
"line": 7
}
},
"jsii-calc.module2692": {
"locationInModule": {
"filename": "lib/index.ts",
"line": 16
}
},
"jsii-calc.module2692.submodule1": {
"locationInModule": {
"filename": "lib/module2692/index.ts",
"line": 1
}
},
"jsii-calc.module2692.submodule2": {
"locationInModule": {
"filename": "lib/module2692/index.ts",
"line": 2
}
},
"jsii-calc.nodirect": {
"locationInModule": {
"filename": "lib/index.ts",
Expand Down Expand Up @@ -14155,6 +14179,78 @@
"name": "CompositionStringStyle",
"namespace": "composition.CompositeOperation"
},
"jsii-calc.module2530.MyClass": {
"assembly": "jsii-calc",
"docs": {
"see": "https://github.com/aws/jsii/issues/2530",
"stability": "stable",
"summary": "Verifies a method with parameters \"_\" can be generated."
},
"fqn": "jsii-calc.module2530.MyClass",
"initializer": {
"docs": {
"stability": "stable"
},
"locationInModule": {
"filename": "lib/module2530/index.ts",
"line": 10
},
"parameters": [
{
"name": "_",
"type": {
"primitive": "number"
}
}
]
},
"kind": "class",
"locationInModule": {
"filename": "lib/module2530/index.ts",
"line": 5
},
"methods": [
{
"docs": {
"stability": "stable"
},
"locationInModule": {
"filename": "lib/module2530/index.ts",
"line": 6
},
"name": "bar",
"parameters": [
{
"name": "_",
"type": {
"primitive": "boolean"
}
}
],
"static": true
},
{
"docs": {
"stability": "stable"
},
"locationInModule": {
"filename": "lib/module2530/index.ts",
"line": 14
},
"name": "foo",
"parameters": [
{
"name": "_",
"type": {
"primitive": "string"
}
}
]
}
],
"name": "MyClass",
"namespace": "module2530"
},
"jsii-calc.module2647.ExtendAndImplement": {
"assembly": "jsii-calc",
"base": "@scope/jsii-calc-lib.BaseFor2647",
Expand Down Expand Up @@ -14470,6 +14566,106 @@
}
]
},
"jsii-calc.module2692.submodule1.Bar": {
"assembly": "jsii-calc",
"datatype": true,
"docs": {
"stability": "stable"
},
"fqn": "jsii-calc.module2692.submodule1.Bar",
"kind": "interface",
"locationInModule": {
"filename": "lib/module2692/submodule1/index.ts",
"line": 1
},
"name": "Bar",
"namespace": "module2692.submodule1",
"properties": [
{
"abstract": true,
"docs": {
"stability": "stable"
},
"immutable": true,
"locationInModule": {
"filename": "lib/module2692/submodule1/index.ts",
"line": 2
},
"name": "bar1",
"type": {
"primitive": "string"
}
}
]
},
"jsii-calc.module2692.submodule2.Bar": {
"assembly": "jsii-calc",
"datatype": true,
"docs": {
"stability": "stable"
},
"fqn": "jsii-calc.module2692.submodule2.Bar",
"kind": "interface",
"locationInModule": {
"filename": "lib/module2692/submodule2/index.ts",
"line": 3
},
"name": "Bar",
"namespace": "module2692.submodule2",
"properties": [
{
"abstract": true,
"docs": {
"stability": "stable"
},
"immutable": true,
"locationInModule": {
"filename": "lib/module2692/submodule2/index.ts",
"line": 4
},
"name": "bar2",
"type": {
"primitive": "string"
}
}
]
},
"jsii-calc.module2692.submodule2.Foo": {
"assembly": "jsii-calc",
"datatype": true,
"docs": {
"stability": "stable"
},
"fqn": "jsii-calc.module2692.submodule2.Foo",
"interfaces": [
"jsii-calc.module2692.submodule2.Bar",
"jsii-calc.module2692.submodule1.Bar"
],
"kind": "interface",
"locationInModule": {
"filename": "lib/module2692/submodule2/index.ts",
"line": 7
},
"name": "Foo",
"namespace": "module2692.submodule2",
"properties": [
{
"abstract": true,
"docs": {
"stability": "stable"
},
"immutable": true,
"locationInModule": {
"filename": "lib/module2692/submodule2/index.ts",
"line": 8
},
"name": "foo2",
"type": {
"primitive": "string"
}
}
]
},
"jsii-calc.nodirect.sub1.TypeFromSub1": {
"assembly": "jsii-calc",
"docs": {
Expand Down Expand Up @@ -15226,5 +15422,5 @@
}
},
"version": "3.20.120",
"fingerprint": "bEIqxJjC2BFJEo5AIzysYAdDCVZcHPAm+paBHx0vvKE="
"fingerprint": "hBZRskNm0XPeSO9U+PMqKKVZ2faqmKnE9eVN2tY1Wik="
}
33 changes: 0 additions & 33 deletions packages/jsii-pacmak/lib/targets/go/types/struct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { Package } from '../package';
import { JSII_RT_ALIAS } from '../runtime';
import { getMemberDependencies } from '../util';
import { GoType } from './go-type';
import { GoTypeRef } from './go-type-reference';
import { GoProperty } from './type-member';

/*
Expand Down Expand Up @@ -50,8 +49,6 @@ export class Struct extends GoType {
}
code.closeBlock();
code.line();

this.emitBaseConversions(context);
}

public emitRegistration(code: CodeMaker): void {
Expand All @@ -60,34 +57,4 @@ export class Struct extends GoType {
code.line(`reflect.TypeOf((*${this.name})(nil)).Elem(),`);
code.close(')');
}

private emitBaseConversions({ code }: EmitContext) {
for (const base of this.type.getInterfaces(true)) {
const baseType = this.pkg.root.findType(base.fqn) as Struct;
const funcName = `To${baseType.name}`;
const instanceVar = this.name[0].toLowerCase();
const valType = new GoTypeRef(this.pkg.root, base.reference).scopedName(
this.pkg,
);

code.line(
`// ${funcName} is a convenience function to obtain a new ${valType} from this ${this.name}.`,
);
// Note - using a pointer receiver here as a convenience, as otherwise
// user code that somehow has only a pointer would need to first
// dereference it, which tends to be a code smell.
code.openBlock(
`func (${instanceVar} *${this.name}) ${funcName}() ${valType}`,
);

code.openBlock(`return ${valType}`);
for (const prop of baseType.properties) {
code.line(`${prop.name}: ${instanceVar}.${prop.name},`);
}
code.closeBlock();

code.closeBlock();
code.line();
}
}
}
1 change: 1 addition & 0 deletions packages/jsii-pacmak/lib/targets/go/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ const RESERVED_WORDS: { [word: string]: string } = {
import: 'import_',
return: 'return_',
var: 'var_',
_: '_arg',
};

/*
Expand Down
Loading

0 comments on commit 52bd510

Please sign in to comment.