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 0c98773
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 131 deletions.
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();
}
}
}

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

0 comments on commit 0c98773

Please sign in to comment.