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

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 committed Mar 15, 2021
1 parent fc80dfe commit 99b3b71
Show file tree
Hide file tree
Showing 12 changed files with 1,176 additions and 34 deletions.
1 change: 1 addition & 0 deletions packages/jsii-calc/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ 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';
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;
}
120 changes: 119 additions & 1 deletion packages/jsii-calc/test/assembly.jsii
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,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 @@ -14470,6 +14488,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 +15344,5 @@
}
},
"version": "3.20.120",
"fingerprint": "bEIqxJjC2BFJEo5AIzysYAdDCVZcHPAm+paBHx0vvKE="
"fingerprint": "zzayVZr0GAzkOZJ5SHHy3UvRxdZ1DZAPChbY/MkerHU="
}
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();
}
}
}
Loading

0 comments on commit 99b3b71

Please sign in to comment.