-
Notifications
You must be signed in to change notification settings - Fork 246
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(go): Method call return reference an implMap
Fixes code generation for go method calls to correctly pass an implMap where needed and a reference to the return value instead of a concrete type by value. Adds test for method call with named return type in TestReturnSpecialParam to cover this. Fixes logic for `usesReflectionPackage` which only needs to know if the type has methods or parameters since all getters and methods need to reference `reflect.Type` regardless of the contents of the implMap. Refactor runtime calls to not reimplement the emission logic for implMaps.
- Loading branch information
1 parent
1373cb3
commit af5b27b
Showing
13 changed files
with
205 additions
and
153 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
packages/jsii-pacmak/lib/targets/go/runtime/function-call.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import {CodeMaker} from 'codemaker'; | ||
import {JSII_IMPL_MAP_TYPE} from './constants'; | ||
import {GoTypeMember} from "../types"; | ||
|
||
export abstract class FunctionCall { | ||
public constructor(public readonly parent: GoTypeMember) {} | ||
|
||
protected get implMap(): string[] { | ||
return this.parent.reference?.scopedImplMap(this.parent.parent.pkg) ?? []; | ||
} | ||
|
||
/** | ||
* Emits map of interface type to concrete struct type for use in runtime to | ||
* cast data to expected return type. | ||
*/ | ||
protected emitImplMapVal(code: CodeMaker) { | ||
if (this.implMap.length) { | ||
const [interfaceName, structName] = this.implMap; | ||
code.open(`${JSII_IMPL_MAP_TYPE}{`); | ||
|
||
// `reflect.TypeOf((*SomeType)(nil)).Elem()` is a reliable way to create | ||
// an instance of reflect.Type for any type. `(*SomeInterface)(nil)` | ||
// creates a "zero value" with the type `SomeInterface` which otherwise | ||
// has no way to instantiate. | ||
code.line(`reflect.TypeOf((*${interfaceName})(nil)).Elem(): reflect.TypeOf((*${structName})(nil)).Elem(),`); | ||
code.close('},'); | ||
} else { | ||
code.line(`${JSII_IMPL_MAP_TYPE}{},`); | ||
} | ||
} | ||
|
||
protected get returnsVal(): boolean { | ||
return Boolean(this.parent.reference && !this.parent.reference.void); | ||
} | ||
|
||
protected get returnType(): string { | ||
return this.parent.returnType || 'interface{}'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.