Skip to content

Commit

Permalink
fix(go): optional values (#2705)
Browse files Browse the repository at this point in the history
Adds support for passing `nil` to jsii nullable types in generated go code.

Changes go code generation to always use "pointer types" for all struct fields, method arguments, and method return values. Update runtime type conversion to correctly handle serializing and deserializing to these pointer types where necessary.

Removes unnecessary functionality for resolving scoped interface names for generated go types as each generated type now has only one public name. Adds support for resolving `scopedReferenceName` to easily generate "pointer types" for complex data structures from a type reference.

[RFC](aws/aws-cdk-rfcs#297)
fix: #2442
fix: #2671

---

By submitting this pull request, I confirm that my contribution is made under the terms of the [Apache 2.0 license].

[Apache 2.0 license]: https://www.apache.org/licenses/LICENSE-2.0
  • Loading branch information
MrArnoldPalmer authored Mar 18, 2021
1 parent c38f5e3 commit 7d0cfc5
Show file tree
Hide file tree
Showing 18 changed files with 1,649 additions and 1,637 deletions.
6 changes: 3 additions & 3 deletions gh-pages/content/specification/6-compliance-report.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
This section details the current state of each language binding with respect to our standard compliance suite.


| number | test | java (100.00%) | golang (34.19%) | Dotnet | Python |
| number | test | java (100.00%) | golang (35.90%) | Dotnet | Python |
| ------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------- | -------------------------------------------- | ------ | ------ |
| 1 | asyncOverrides_overrideCallsSuper | 🟢 | [🔴](https://github.com/aws/jsii/issues/2670) |||
| 2 | [arrayReturnedByMethodCanBeRead]("Array created in the kernel can be queried for its elements") | 🟢 | 🟢 |||
Expand All @@ -27,7 +27,7 @@ This section details the current state of each language binding with respect to
| 18 | [testJsiiAgent]("Asserts the correct value of the JSII_AGENT env variable for the kernel process") | 🟢 | 🟢 |||
| 19 | [doNotOverridePrivates_method_private]("Non public methods on the guest class do not override methods in the host class") | 🟢 | 🟢 |||
| 20 | [pureInterfacesCanBeUsedTransparently]("Guest implementation of a pure host interface can be used by host consumers accepting that interface") | 🟢 | 🟢 |||
| 21 | [nullShouldBeTreatedAsUndefined]("Null value of target language is treated as undefined by the host") | 🟢 | [🔴](https://github.com/aws/jsii/issues/2442) |||
| 21 | [nullShouldBeTreatedAsUndefined]("Null value of target language is treated as undefined by the host") | 🟢 | 🟢 |||
| 22 | [primitiveTypes]("All Primitive types are set and read with their respective types") | 🟢 | [🔴](https://github.com/aws/jsii/issues/2659) |||
| 23 | [reservedKeywordsAreSlugifiedInClassProperties]("TS code that uses reserved words as class property names get slugified so it is usable in the target language") | 🟢 ||||
| 24 | [objectIdDoesNotGetReallocatedWhenTheConstructorPassesThisOut]("Ensure the JSII kernel can pass 'this' out to JSII remotes from within the constructor") | 🟢 | [🔴](https://github.com/aws/jsii/issues/2048) |||
Expand Down Expand Up @@ -63,7 +63,7 @@ This section details the current state of each language binding with respect to
| 54 | creationOfNativeObjectsFromJavaScriptObjects | 🟢 | [🔴](??) |||
| 55 | canOverrideProtectedMethod | 🟢 | [🔴](https://github.com/aws/jsii/issues/2048) |||
| 56 | canLoadEnumValues | 🟢 | 🟢 |||
| 57 | eraseUnsetDataValues | 🟢 | [🔴](https://github.com/aws/jsii/issues/2671) |||
| 57 | eraseUnsetDataValues | 🟢 | 🟢 |||
| 58 | maps | 🟢 | 🟢 |||
| 59 | structs_containsNullChecks | 🟢 | [🔴](https://github.com/aws/jsii/issues/2672) |||
| 60 | canOverrideProtectedSetter | 🟢 | [🔴](https://github.com/aws/jsii/issues/2673) |||
Expand Down
2 changes: 1 addition & 1 deletion packages/@jsii/go-runtime-test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"fmt": "(cd project && go fmt ./...)",
"lint": "(cd project && go vet ./...)",
"test": "(cd project && go test ./...)",
"lint:fix": "yarn lint --fix",
"lint:fix": "yarn lint && yarn fmt",
"gen:calc": "node build-tools/gen-calc.js"
},
"keywords": [],
Expand Down
13 changes: 7 additions & 6 deletions packages/@jsii/go-runtime-test/project/callbacks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,21 @@ import (
)

func TestPureInterfacesCanBeUsedTransparently(t *testing.T) {
expected := calc.StructB{RequiredString: "It's Britney b**ch!"}
delegate := &StructReturningDelegate{expected: expected}
requiredstring := "It's Britney b**ch!"
expected := calc.StructB{RequiredString: &requiredstring}
delegate := &StructReturningDelegate{expected: &expected}
consumer := calc.NewConsumePureInterface(delegate)
actual := consumer.WorkItBaby()

if actual != expected {
t.Errorf("Expected %v; actual: %v", expected, actual)
if *actual.RequiredString != *expected.RequiredString {
t.Errorf("Expected %v; actual: %v", *expected.RequiredString, *actual.RequiredString)
}
}

type StructReturningDelegate struct {
expected calc.StructB
expected *calc.StructB
}

func (o *StructReturningDelegate) ReturnStruct() calc.StructB {
func (o *StructReturningDelegate) ReturnStruct() *calc.StructB {
return o.expected
}
2 changes: 1 addition & 1 deletion packages/@jsii/go-runtime-test/project/compliance.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (suite *ComplianceSuite) Assert() *assert.Assertions {
return assert.New(suite.T())
}

func (suite* ComplianceSuite) reportForTest() map[string]string {
func (suite *ComplianceSuite) reportForTest() map[string]string {
fullName := suite.T().Name()
testName := strings.Split(fullName, "/")[1]
name := strings.Replace(testName, "Test", "", 1)
Expand Down
Loading

0 comments on commit 7d0cfc5

Please sign in to comment.