Skip to content

Commit

Permalink
feat(go): packageName and versionSuffix (#2687)
Browse files Browse the repository at this point in the history
Add two features to the `go` targets config in package.json:

- `packageName` can be used to customize the name of the go package (previously this was always derived from the javascript name).
- `versionSuffix` can be used to append a suffix to the version number (e.g. `-devpreview`).

Resolves #2632

This is needed in order to control the name of the AWS CDK go bindings so they are the same between v1 and v2. Currently the v1 name would have been `monocdk` and v2 `awscdklib` and we want both to actually be `awscdk` per the decision in [this RFC](https://github.com/aws/aws-cdk-rfcs/blob/master/text/0079-cdk-2.0.md#releasing-to-package-managers).
  • Loading branch information
Elad Ben-Israel authored Mar 14, 2021
1 parent bf600ec commit 9562108
Show file tree
Hide file tree
Showing 17 changed files with 431 additions and 364 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,32 @@
!!! danger
The **go** target is currently unstable and not suitable for production use.

To enable go package generation, add the **go** key with an empty object to the jsii targets configuration.
To enable go package generation, add the `go` key to the jsii targets configuration:

This will add generated go package code to your specified `outDir` for testing and experimentation.
- `packageName` (optional) - The name of the Go package name. If not specified,
package name will be derived from the JavaScript module name by removing
non-alphanumeric characters (e.g. `@aws-cdk/aws-s3` will be `awscdkawss3`). If
this is set on a submodule config file (`.jsiirc.json`), it refers to the
submodule package name.
- `moduleName` (required) - The name of the **target repository** in which this
module will be published (e.g. `github.com/foo/bar`). The module itself will
*always* be published under a subdirectory named according to the Go package
name of the module (e.g. `github.com/foo/bar/awscdk`).
- `versionSuffix` (optional) - Can be provided that will be appended at the end
of the module version.

```json
This will add generated go package code to your specified `outDir` under
`go/PACKAGE_NAME` (e.g. `dist/go/awscdklib`).

```js
{
"jsii": {
"targets": {
"go": {},
"go": {
"moduleName": "github.com/foo/bar", // REQUIRED
"packageName": "hello", // OPTIONAL
"versionSuffix": "-devprefix" // OPTIONAL
},
// ...
},
// ...
Expand Down
104 changes: 51 additions & 53 deletions packages/@jsii/go-runtime-test/project/compliance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@ package tests

import (
"fmt"
"math"
"runtime"
"testing"
"time"

"github.com/aws/jsii/jsii-calc/go/jcb"
calc "github.com/aws/jsii/jsii-calc/go/jsiicalc/v3"
"github.com/aws/jsii/jsii-calc/go/jsiicalc/v3/composition"
"github.com/aws/jsii/jsii-calc/go/jsiicalc/v3/submodule/child"
"github.com/aws/jsii/jsii-calc/go/scopejsiicalcbase"
calclib "github.com/aws/jsii/jsii-calc/go/scopejsiicalclib"
"github.com/aws/jsii/jsii-calc/go/scopejsiicalclib/submodule"
"github.com/aws/jsii/jsii-calc/go/scopejsiicalclib/customsubmodulename"
"github.com/stretchr/testify/suite"
"math"
"runtime"
"testing"
"time"
)

func (suite *ComplianceSuite) TestStatics() {
Expand Down Expand Up @@ -62,7 +63,7 @@ func (suite *ComplianceSuite) TestPrimitiveTypes() {

func (suite *ComplianceSuite) TestUseNestedStruct() {
suite.FailTest("Nested types are not namespaced", "https://github.com/aws/jsii/pull/2650")
scopejsiicalcbase.StaticConsumer_Consume(submodule.NestingClass_NestedStruct{
jcb.StaticConsumer_Consume(customsubmodulename.NestingClass_NestedStruct{
Name: "Bond, James Bond",
})
}
Expand Down Expand Up @@ -143,7 +144,7 @@ func (suite *ComplianceSuite) TestCallMethods() {
assert.Equal(float64(20), calc.Value())

calc.Pow(5)
assert.Equal(float64(20 * 20 * 20 * 20 * 20), calc.Value())
assert.Equal(float64(20*20*20*20*20), calc.Value())

calc.Neg()
assert.Equal(float64(-3200000), calc.Value())
Expand All @@ -161,7 +162,7 @@ func (suite *ComplianceSuite) TestNodeStandardLibrary() {
assert.Equal("Hello, resource!", obj.FsReadFile())
}

func (suite* ComplianceSuite) TestDynamicTypes() {
func (suite *ComplianceSuite) TestDynamicTypes() {
assert := suite.Assert()
types := calc.NewAllTypes()

Expand Down Expand Up @@ -204,11 +205,11 @@ func (suite* ComplianceSuite) TestDynamicTypes() {
assert.Equal(float64(123), (types.AnyProperty()).([]interface{})[2])

// map
types.SetAnyProperty(map[string]string{ "MapKey": "MapValue" })
types.SetAnyProperty(map[string]string{"MapKey": "MapValue"})
assert.Equal("MapValue", ((types.AnyProperty()).(map[string]interface{}))["MapKey"])

// map of any
types.SetAnyProperty(map[string]interface{}{ "Goo": 19289812 })
types.SetAnyProperty(map[string]interface{}{"Goo": 19289812})
assert.Equal(float64(19289812), ((types.AnyProperty()).(map[string]interface{}))["Goo"])

// classes
Expand Down Expand Up @@ -260,14 +261,14 @@ func (suite *ComplianceSuite) TestUseEnumFromScopedModule() {
obj.SetFoo(calclib.EnumFromScopedModule_VALUE1)
assert.Equal(calclib.EnumFromScopedModule_VALUE1, obj.LoadFoo())
obj.SaveFoo(calclib.EnumFromScopedModule_VALUE2)
assert.Equal( calclib.EnumFromScopedModule_VALUE2, obj.Foo())
assert.Equal(calclib.EnumFromScopedModule_VALUE2, obj.Foo())
}

func (suite *ComplianceSuite) TestCreateObjectAndCtorOverloads() {
suite.NotApplicableTest("Golang does not have overloaded functions so the genearated class only has a single New function")
}

func (suite* ComplianceSuite) TestGetAndSetEnumValues() {
func (suite *ComplianceSuite) TestGetAndSetEnumValues() {
assert := suite.Assert()

calc := calc.NewCalculator(calc.CalculatorProps{})
Expand All @@ -280,7 +281,7 @@ func (suite* ComplianceSuite) TestGetAndSetEnumValues() {
assert.Equal("<<[[{{(((1 * (0 + 9)) * (0 + 9)) * (0 + 9))}}]]>>", calc.ToString())
}

func (suite* ComplianceSuite) TestListInClassCanBeReadCorrectly() {
func (suite *ComplianceSuite) TestListInClassCanBeReadCorrectly() {
assert := suite.Assert()

classWithCollections := calc.NewClassWithCollections(map[string]string{}, []string{"one", "two"})
Expand All @@ -299,7 +300,7 @@ func newDerivedFromAllTypes() derivedFromAllTypes {
}
}

func (suite* ComplianceSuite) TestTestFluentApiWithDerivedClasses() {
func (suite *ComplianceSuite) TestTestFluentApiWithDerivedClasses() {
assert := suite.Assert()

obj := newDerivedFromAllTypes()
Expand All @@ -309,13 +310,13 @@ func (suite* ComplianceSuite) TestTestFluentApiWithDerivedClasses() {
assert.Equal(float64(12), obj.NumberProperty())
}

func (suite* ComplianceSuite) TestCanLoadEnumValues() {
func (suite *ComplianceSuite) TestCanLoadEnumValues() {
assert := suite.Assert()
assert.NotEmpty(calc.EnumDispenser_RandomStringLikeEnum())
assert.NotEmpty(calc.EnumDispenser_RandomIntegerLikeEnum())
}

func (suite* ComplianceSuite) TestCollectionOfInterfaces_ListOfStructs() {
func (suite *ComplianceSuite) TestCollectionOfInterfaces_ListOfStructs() {
assert := suite.Assert()

list := calc.InterfaceCollections_ListOfStructs()
Expand All @@ -332,15 +333,15 @@ func newDoNotOverridePrivates() doNotOverridePrivates {
}
}

func (x* doNotOverridePrivates) PrivateProperty() string {
func (x *doNotOverridePrivates) PrivateProperty() string {
return "privateProperty-Override"
}

func (x* doNotOverridePrivates) SetPrivateProperty(value string) {
func (x *doNotOverridePrivates) SetPrivateProperty(value string) {
panic("Boom")
}

func (suite* ComplianceSuite) TestDoNotOverridePrivates_property_getter_public() {
func (suite *ComplianceSuite) TestDoNotOverridePrivates_property_getter_public() {
assert := suite.Assert()

obj := newDoNotOverridePrivates()
Expand All @@ -351,11 +352,11 @@ func (suite* ComplianceSuite) TestDoNotOverridePrivates_property_getter_public()
assert.Equal("MyNewValue", obj.PrivatePropertyValue())
}

func (suite* ComplianceSuite) TestEqualsIsResistantToPropertyShadowingResultVariable() {
func (suite *ComplianceSuite) TestEqualsIsResistantToPropertyShadowingResultVariable() {
assert := suite.Assert()
first := calc.StructWithJavaReservedWords{ Default: "one" }
second := calc.StructWithJavaReservedWords { Default: "one" }
third := calc.StructWithJavaReservedWords { Default: "two" }
first := calc.StructWithJavaReservedWords{Default: "one"}
second := calc.StructWithJavaReservedWords{Default: "one"}
third := calc.StructWithJavaReservedWords{Default: "two"}
assert.Equal(first, second)
assert.NotEqual(first, third)
}
Expand All @@ -370,15 +371,15 @@ func newOverridableProtectedMemberDerived() overridableProtectedMemberDerived {
}
}

func (x* overridableProtectedMemberDerived) OverrideReadOnly() string {
func (x *overridableProtectedMemberDerived) OverrideReadOnly() string {
return "Cthulhu "
}

func (x* overridableProtectedMemberDerived) OverrideReadeWrite() string {
func (x *overridableProtectedMemberDerived) OverrideReadeWrite() string {
return "Fhtagn!"
}

func (suite* ComplianceSuite) TestCanOverrideProtectedGetter() {
func (suite *ComplianceSuite) TestCanOverrideProtectedGetter() {
suite.FailTest("Overrides are not supported yet", "https://github.com/aws/jsii/issues/2048")

assert := suite.Assert()
Expand All @@ -402,18 +403,17 @@ func newImplementsAdditionalInterface(s calc.StructB) implementsAdditionalInterf
}
}

func (suite* ComplianceSuite) TestInterfacesCanBeUsedTransparently_WhenAddedToJsiiType() {
func (suite *ComplianceSuite) TestInterfacesCanBeUsedTransparently_WhenAddedToJsiiType() {
suite.FailTest("Overrides not supported", "https://github.com/aws/jsii/issues/2048")
assert := suite.Assert()


expected := calc.StructB{RequiredString: "It's Britney b**ch!"}
delegate := newImplementsAdditionalInterface(expected)
consumer := calc.NewConsumePureInterface(delegate)
assert.Equal(expected, consumer.WorkItBaby())
}

func (suite* ComplianceSuite) TestStructs_nonOptionalequals() {
func (suite *ComplianceSuite) TestStructs_nonOptionalequals() {
assert := suite.Assert()

structA := calc.StableStruct{ReadonlyProperty: "one"}
Expand All @@ -423,10 +423,9 @@ func (suite* ComplianceSuite) TestStructs_nonOptionalequals() {
assert.NotEqual(structC, structA)
}

func (suite* ComplianceSuite) TestTestInterfaceParameter() {
func (suite *ComplianceSuite) TestTestInterfaceParameter() {
assert := suite.Assert()


obj := calc.NewJsObjectLiteralForInterface()
friendly := obj.GiveMeFriendly()
assert.Equal("I am literally friendly!", friendly.Hello())
Expand All @@ -436,7 +435,7 @@ func (suite* ComplianceSuite) TestTestInterfaceParameter() {
assert.Equal("I am literally friendly! Let me buy you a drink!", betterGreeting)
}

func (suite* ComplianceSuite) TestLiftedKwargWithSameNameAsPositionalArg() {
func (suite *ComplianceSuite) TestLiftedKwargWithSameNameAsPositionalArg() {
assert := suite.Assert()

// This is a replication of a test that mostly affects languages with keyword arguments (e.g: Python, Ruby, ...)
Expand Down Expand Up @@ -468,7 +467,7 @@ func newMulTen(value float64) mulTen {
}
}

func (suite* ComplianceSuite) TestCreationOfNativeObjectsFromJavaScriptObjects() {
func (suite *ComplianceSuite) TestCreationOfNativeObjectsFromJavaScriptObjects() {
assert := suite.Assert()

types := calc.NewAllTypes()
Expand All @@ -480,7 +479,6 @@ func (suite* ComplianceSuite) TestCreationOfNativeObjectsFromJavaScriptObjects()

suite.FailTest("??", "??")


nativeObj := newAddTen(10)
types.SetAnyProperty(nativeObj)
result1 := types.AnyProperty()
Expand Down Expand Up @@ -558,7 +556,7 @@ func (suite *ComplianceSuite) TestPropertyOverrides_Interfaces() {
assert.Equal("READ_ONLY_STRING", interact.JustRead())

suite.FailTest("Not sure. Most likely related to the missing setters on interfaces", "https://github.com/aws/jsii/issues/2665")
assert.Equal( "Hello!?", interact.WriteAndRead("Hello"))
assert.Equal("Hello!?", interact.WriteAndRead("Hello"))
}

type TestPropertyOverridesInterfacesIInterfaceWithProperties struct {
Expand Down Expand Up @@ -677,7 +675,7 @@ func (suite *ComplianceSuite) TestStructs_containsNullChecks() {
suite.FailTest("No validation of required fields in structs", "https://github.com/aws/jsii/issues/2672")

// we expect a failure here when we pass the struct to js
assert.PanicsWithError("", func() {obj.ReadFirstNumber(s)})
assert.PanicsWithError("", func() { obj.ReadFirstNumber(s) })
}

func (suite *ComplianceSuite) TestUnionPropertiesWithBuilder() {
Expand Down Expand Up @@ -776,9 +774,9 @@ func (suite *ComplianceSuite) TestReturnAbstract() {
obj := calc.NewAbstractClassReturner()
obj2 := obj.GiveMeAbstract()

assert.Equal("Hello, John!!", obj2.AbstractMethod("John"));
assert.Equal("propFromInterfaceValue", obj2.PropFromInterface());
assert.Equal(float64(42), obj2.NonAbstractMethod());
assert.Equal("Hello, John!!", obj2.AbstractMethod("John"))
assert.Equal("propFromInterfaceValue", obj2.PropFromInterface())
assert.Equal(float64(42), obj2.NonAbstractMethod())

iface := obj.GiveMeInterface()
assert.Equal("propFromInterfaceValue", iface.PropFromInterface())
Expand All @@ -795,22 +793,22 @@ func (suite *ComplianceSuite) TestCollectionOfInterfaces_MapOfInterfaces() {
func (suite *ComplianceSuite) TestStructs_multiplePropertiesEquals() {
assert := suite.Assert()
structA := calc.DiamondInheritanceTopLevelStruct{
BaseLevelProperty: "one",
FirstMidLevelProperty: "two",
BaseLevelProperty: "one",
FirstMidLevelProperty: "two",
SecondMidLevelProperty: "three",
TopLevelProperty: "four",
TopLevelProperty: "four",
}
structB := calc.DiamondInheritanceTopLevelStruct{
BaseLevelProperty: "one",
FirstMidLevelProperty: "two",
BaseLevelProperty: "one",
FirstMidLevelProperty: "two",
SecondMidLevelProperty: "three",
TopLevelProperty: "four",
TopLevelProperty: "four",
}
structC := calc.DiamondInheritanceTopLevelStruct{
BaseLevelProperty: "one",
FirstMidLevelProperty: "two",
BaseLevelProperty: "one",
FirstMidLevelProperty: "two",
SecondMidLevelProperty: "different",
TopLevelProperty: "four",
TopLevelProperty: "four",
}

assert.Equal(structA, structB)
Expand All @@ -829,7 +827,7 @@ type myDoNotOverridePrivates struct {
calc.DoNotOverridePrivates
}

func (s *myDoNotOverridePrivates) PrivateProperty() string {
func (s *myDoNotOverridePrivates) PrivateProperty() string {
return "privateProperty-Override"
}

Expand All @@ -851,10 +849,10 @@ func (suite *ComplianceSuite) TestDoNotOverridePrivates_property_getter_private(
func (suite *ComplianceSuite) TestStructs_withDiamondInheritance_correctlyDedupeProperties() {
assert := suite.Assert()
s := calc.DiamondInheritanceTopLevelStruct{
BaseLevelProperty: "base",
FirstMidLevelProperty: "mid1",
BaseLevelProperty: "base",
FirstMidLevelProperty: "mid1",
SecondMidLevelProperty: "mid2",
TopLevelProperty: "top",
TopLevelProperty: "top",
}

assert.Equal("base", s.BaseLevelProperty)
Expand Down
4 changes: 2 additions & 2 deletions packages/@jsii/go-runtime-test/project/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ require (
github.com/aws/jsii-runtime-go v0.0.0
github.com/aws/jsii/jsii-calc/go/jsiicalc/v3 v3.20.120
github.com/aws/jsii/jsii-calc/go/scopejsiicalclib v0.0.0
github.com/aws/jsii/jsii-calc/go/scopejsiicalcbase v0.0.0
github.com/aws/jsii/jsii-calc/go/jcb v0.0.0
github.com/stretchr/testify v1.7.0
)

replace (
github.com/aws/jsii-runtime-go => ../../go-runtime/jsii-runtime-go
github.com/aws/jsii/jsii-calc/go/jsiicalc/v3 => ../jsii-calc/go/jsiicalc
github.com/aws/jsii/jsii-calc/go/scopejsiicalcbase => ../jsii-calc/go/scopejsiicalcbase
github.com/aws/jsii/jsii-calc/go/jcb => ../jsii-calc/go/jcb
github.com/aws/jsii/jsii-calc/go/scopejsiicalcbaseofbase/v2 => ../jsii-calc/go/scopejsiicalcbaseofbase
github.com/aws/jsii/jsii-calc/go/scopejsiicalclib => ../jsii-calc/go/scopejsiicalclib
)
4 changes: 2 additions & 2 deletions packages/@jsii/go-runtime-test/project/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/aws/jsii/jsii-calc/go/jsiicalc/v3/submodule/param"
returnsParam "github.com/aws/jsii/jsii-calc/go/jsiicalc/v3/submodule/returnsparam"
calclib "github.com/aws/jsii/jsii-calc/go/scopejsiicalclib"
"github.com/aws/jsii/jsii-calc/go/scopejsiicalclib/submodule"
"github.com/aws/jsii/jsii-calc/go/scopejsiicalclib/customsubmodulename"
)

func TestMain(m *testing.M) {
Expand Down Expand Up @@ -115,7 +115,7 @@ func TestUpcasingReflectable(t *testing.T) {
}

actual := entries[0]
expected := submodule.ReflectableEntry{Key: strings.ToUpper(key), Value: val}
expected := customsubmodulename.ReflectableEntry{Key: strings.ToUpper(key), Value: val}
if actual != expected {
t.Errorf("Expected %v; Received: %v", expected, actual)
}
Expand Down
1 change: 1 addition & 0 deletions packages/@jsii/kernel/test/kernel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,7 @@ defineTest(
},
go: {
moduleName: 'github.com/aws/jsii/jsii-calc/go',
versionSuffix: '-devpreview',
},
java: {
package: 'software.amazon.jsii.tests.calculator.lib',
Expand Down
Loading

0 comments on commit 9562108

Please sign in to comment.