Skip to content

Commit

Permalink
fix(go): missing imports required by collection types (#2691)
Browse files Browse the repository at this point in the history
When a foreign type is referenced within a complex type (array/list), it wasn't included in the import rendering.

Fixes #2689
  • Loading branch information
Elad Ben-Israel authored Mar 15, 2021
1 parent daca06f commit c9a36a6
Show file tree
Hide file tree
Showing 16 changed files with 1,635 additions and 10 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 @@ -12,3 +12,4 @@ export * as submodule from './submodule';
export * as onlystatic from './only-static';
export * as nodirect from './no-direct-types';
export * as module2647 from './module2647';
export * as module2689 from './module2689';
10 changes: 10 additions & 0 deletions packages/jsii-calc/lib/module2689/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//
// Verifies that the correct imports are generated for Go code when forien types
// are referenced in complex fields (array and map).
//
// @see https://github.com/aws/jsii/issues/2689

export * as structs from './structs';
export * as methods from './methods';
export * as props from './props';
export * as retval from './retval';
12 changes: 12 additions & 0 deletions packages/jsii-calc/lib/module2689/methods/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { BaseProps } from '@scope/jsii-calc-base';
import { Number as NumberClass } from '@scope/jsii-calc-lib';

export class MyClass {
public foo(_values: NumberClass[]) {
return;
}

public bar(_bar: { [k: string]: BaseProps }) {
return;
}
}
7 changes: 7 additions & 0 deletions packages/jsii-calc/lib/module2689/props/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { BaseProps } from '@scope/jsii-calc-base';
import { Number as NumberClass } from '@scope/jsii-calc-lib';

export class MyClass {
public readonly foo: NumberClass[] = [];
public readonly bar: { [key: string]: BaseProps } = {};
}
12 changes: 12 additions & 0 deletions packages/jsii-calc/lib/module2689/retval/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { BaseProps } from '@scope/jsii-calc-base';
import { Number as NumberClass } from '@scope/jsii-calc-lib';

export class MyClass {
public foo(): NumberClass[] {
return [];
}

public bar(): { [k: string]: BaseProps } {
return {};
}
}
7 changes: 7 additions & 0 deletions packages/jsii-calc/lib/module2689/structs/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { BaseProps } from '@scope/jsii-calc-base';
import { Number as NumberClass } from '@scope/jsii-calc-lib';

export interface MyStruct {
readonly numbers: NumberClass[];
readonly baseMap: { [key: string]: BaseProps };
}
276 changes: 275 additions & 1 deletion packages/jsii-calc/test/assembly.jsii
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,36 @@
"line": 14
}
},
"jsii-calc.module2689": {
"locationInModule": {
"filename": "lib/index.ts",
"line": 15
}
},
"jsii-calc.module2689.methods": {
"locationInModule": {
"filename": "lib/module2689/index.ts",
"line": 8
}
},
"jsii-calc.module2689.props": {
"locationInModule": {
"filename": "lib/module2689/index.ts",
"line": 9
}
},
"jsii-calc.module2689.retval": {
"locationInModule": {
"filename": "lib/module2689/index.ts",
"line": 10
}
},
"jsii-calc.module2689.structs": {
"locationInModule": {
"filename": "lib/module2689/index.ts",
"line": 7
}
},
"jsii-calc.nodirect": {
"locationInModule": {
"filename": "lib/index.ts",
Expand Down Expand Up @@ -14196,6 +14226,250 @@
"name": "ExtendAndImplement",
"namespace": "module2647"
},
"jsii-calc.module2689.methods.MyClass": {
"assembly": "jsii-calc",
"docs": {
"stability": "stable"
},
"fqn": "jsii-calc.module2689.methods.MyClass",
"initializer": {
"docs": {
"stability": "stable"
}
},
"kind": "class",
"locationInModule": {
"filename": "lib/module2689/methods/index.ts",
"line": 4
},
"methods": [
{
"docs": {
"stability": "stable"
},
"locationInModule": {
"filename": "lib/module2689/methods/index.ts",
"line": 9
},
"name": "bar",
"parameters": [
{
"name": "_bar",
"type": {
"collection": {
"elementtype": {
"fqn": "@scope/jsii-calc-base.BaseProps"
},
"kind": "map"
}
}
}
]
},
{
"docs": {
"stability": "stable"
},
"locationInModule": {
"filename": "lib/module2689/methods/index.ts",
"line": 5
},
"name": "foo",
"parameters": [
{
"name": "_values",
"type": {
"collection": {
"elementtype": {
"fqn": "@scope/jsii-calc-lib.Number"
},
"kind": "array"
}
}
}
]
}
],
"name": "MyClass",
"namespace": "module2689.methods"
},
"jsii-calc.module2689.props.MyClass": {
"assembly": "jsii-calc",
"docs": {
"stability": "stable"
},
"fqn": "jsii-calc.module2689.props.MyClass",
"initializer": {
"docs": {
"stability": "stable"
}
},
"kind": "class",
"locationInModule": {
"filename": "lib/module2689/props/index.ts",
"line": 4
},
"name": "MyClass",
"namespace": "module2689.props",
"properties": [
{
"docs": {
"stability": "stable"
},
"immutable": true,
"locationInModule": {
"filename": "lib/module2689/props/index.ts",
"line": 6
},
"name": "bar",
"type": {
"collection": {
"elementtype": {
"fqn": "@scope/jsii-calc-base.BaseProps"
},
"kind": "map"
}
}
},
{
"docs": {
"stability": "stable"
},
"immutable": true,
"locationInModule": {
"filename": "lib/module2689/props/index.ts",
"line": 5
},
"name": "foo",
"type": {
"collection": {
"elementtype": {
"fqn": "@scope/jsii-calc-lib.Number"
},
"kind": "array"
}
}
}
]
},
"jsii-calc.module2689.retval.MyClass": {
"assembly": "jsii-calc",
"docs": {
"stability": "stable"
},
"fqn": "jsii-calc.module2689.retval.MyClass",
"initializer": {
"docs": {
"stability": "stable"
}
},
"kind": "class",
"locationInModule": {
"filename": "lib/module2689/retval/index.ts",
"line": 4
},
"methods": [
{
"docs": {
"stability": "stable"
},
"locationInModule": {
"filename": "lib/module2689/retval/index.ts",
"line": 9
},
"name": "bar",
"returns": {
"type": {
"collection": {
"elementtype": {
"fqn": "@scope/jsii-calc-base.BaseProps"
},
"kind": "map"
}
}
}
},
{
"docs": {
"stability": "stable"
},
"locationInModule": {
"filename": "lib/module2689/retval/index.ts",
"line": 5
},
"name": "foo",
"returns": {
"type": {
"collection": {
"elementtype": {
"fqn": "@scope/jsii-calc-lib.Number"
},
"kind": "array"
}
}
}
}
],
"name": "MyClass",
"namespace": "module2689.retval"
},
"jsii-calc.module2689.structs.MyStruct": {
"assembly": "jsii-calc",
"datatype": true,
"docs": {
"stability": "stable"
},
"fqn": "jsii-calc.module2689.structs.MyStruct",
"kind": "interface",
"locationInModule": {
"filename": "lib/module2689/structs/index.ts",
"line": 4
},
"name": "MyStruct",
"namespace": "module2689.structs",
"properties": [
{
"abstract": true,
"docs": {
"stability": "stable"
},
"immutable": true,
"locationInModule": {
"filename": "lib/module2689/structs/index.ts",
"line": 6
},
"name": "baseMap",
"type": {
"collection": {
"elementtype": {
"fqn": "@scope/jsii-calc-base.BaseProps"
},
"kind": "map"
}
}
},
{
"abstract": true,
"docs": {
"stability": "stable"
},
"immutable": true,
"locationInModule": {
"filename": "lib/module2689/structs/index.ts",
"line": 5
},
"name": "numbers",
"type": {
"collection": {
"elementtype": {
"fqn": "@scope/jsii-calc-lib.Number"
},
"kind": "array"
}
}
}
]
},
"jsii-calc.nodirect.sub1.TypeFromSub1": {
"assembly": "jsii-calc",
"docs": {
Expand Down Expand Up @@ -14952,5 +15226,5 @@
}
},
"version": "3.20.120",
"fingerprint": "jhFZc5fa0gXEmH6s8wlAhaQm8CX/ZCHNvpY1jXxwMgE="
"fingerprint": "bEIqxJjC2BFJEo5AIzysYAdDCVZcHPAm+paBHx0vvKE="
}
32 changes: 32 additions & 0 deletions packages/jsii-pacmak/lib/targets/go/types/go-type-reference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,38 @@ export class GoTypeRef {
return this._typeMap;
}

/**
* The go `import`s required in order to be able to use this type in code.
*/
public get dependencies(): readonly Package[] {
const ret = new Array<Package>();

switch (this.typeMap.type) {
case 'interface':
if (this.type?.pkg) {
ret.push(this.type?.pkg);
}
break;

case 'array':
case 'map':
ret.push(...(this.typeMap.value.dependencies ?? []));
break;

case 'union':
for (const t of this.typeMap.value) {
ret.push(...(t.dependencies ?? []));
}
break;

case 'void':
case 'primitive':
break;
}

return ret;
}

/*
* Return the name of a type for reference from the `Package` passed in
*/
Expand Down
Loading

0 comments on commit c9a36a6

Please sign in to comment.