-
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.
feat(go): runtime type check type unions (#3712)
Runtime type-checking can be disabled by adding `--tag=no_runtime_type_checking` to go compiler invocations. This can be used to avoid paying the performance toll of runtime type checking (which should be modest in most cases), or to work around a bug in the type checking code.
- Loading branch information
1 parent
362326e
commit c73c2ee
Showing
49 changed files
with
14,038 additions
and
3,646 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 was deleted.
Oops, something went wrong.
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
91 changes: 91 additions & 0 deletions
91
packages/@jsii/go-runtime-test/project/runtime_type_checking_test.go
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,91 @@ | ||
package tests | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/aws/jsii-runtime-go" | ||
"github.com/aws/jsii/jsii-calc/go/jsiicalc/v3" | ||
"github.com/aws/jsii/jsii-calc/go/jsiicalc/v3/anonymous" | ||
) | ||
|
||
func TestConstructor(t *testing.T) { | ||
defer expectPanic(t, "parameter unionProperty[0][\"bad\"] must be one of the allowed types: *StructA, *StructB; received \"Not a StructA or StructB\" (a string)") | ||
jsiicalc.NewClassWithCollectionOfUnions(&[]*map[string]interface{}{ | ||
{ | ||
"good": jsiicalc.StructA{ | ||
RequiredString: jsii.String("present"), | ||
}, | ||
"bad": "Not a StructA or StructB", | ||
}, | ||
}) | ||
} | ||
|
||
func TestSetter(t *testing.T) { | ||
subject := jsiicalc.NewClassWithCollectionOfUnions(&[]*map[string]interface{}{}) | ||
|
||
defer expectPanic(t, "parameter val[0][\"bad\"] must be one of the allowed types: *StructA, *StructB; received \"Not a StructA or StructB\" (a string)") | ||
subject.SetUnionProperty(&[]*map[string]interface{}{ | ||
{ | ||
"good": jsiicalc.StructA{ | ||
RequiredString: jsii.String("present"), | ||
}, | ||
"bad": "Not a StructA or StructB", | ||
}, | ||
}) | ||
} | ||
|
||
func TestStaticMethod(t *testing.T) { | ||
defer expectPanic(t, "parameter struct_ must be one of the allowed types: *StructA, *StructB; received \"Not a StructA\" (a string)") | ||
jsiicalc.StructUnionConsumer_IsStructA("Not a StructA") | ||
} | ||
|
||
func TestAnonymousObjectIsValid(t *testing.T) { | ||
strct := jsiicalc.StructUnionConsumer_ProvideStruct(jsii.String("A")) | ||
if !*jsiicalc.StructUnionConsumer_IsStructA(strct) { | ||
t.Error("Expected strct to be a StructA") | ||
} | ||
|
||
iface := anonymous.UseOptions_Provide(jsii.String("A")) | ||
if *anonymous.UseOptions_Consume(iface) != "A" { | ||
t.Error("Expected iface to produce A") | ||
} | ||
} | ||
|
||
func TestNestedUnion(t *testing.T) { | ||
func() { | ||
defer expectPanic(t, "parameter unionProperty[0] must be one of the allowed types: *map[string]interface{}, *[]interface{}; received 1337.42 (a float64)") | ||
jsiicalc.NewClassWithNestedUnion(&[]interface{}{1337.42}) | ||
}() | ||
|
||
func() { | ||
defer expectPanic(t, "parameter unionProperty[0][1] must be one of the allowed types: *StructA, *StructB; received 1337.42 (a float64)") | ||
jsiicalc.NewClassWithNestedUnion(&[]interface{}{ | ||
[]interface{}{ | ||
jsiicalc.StructA{RequiredString: jsii.String("present")}, | ||
1337.42, | ||
}, | ||
}) | ||
}() | ||
|
||
func() { | ||
defer expectPanic(t, "parameter unionProperty[0][\"bad\"] must be one of the allowed types: *StructA, *StructB; received \"Not a StructA or StructB\" (a string)") | ||
jsiicalc.NewClassWithNestedUnion(&[]interface{}{ | ||
map[string]interface{}{ | ||
"good": jsiicalc.StructA{RequiredString: jsii.String("present")}, | ||
"bad": "Not a StructA or StructB", | ||
}, | ||
}) | ||
}() | ||
} | ||
|
||
func expectPanic(t *testing.T, expected string) { | ||
if err := recover(); err != nil { | ||
actual := fmt.Sprintf("%v", err) | ||
if actual != expected { | ||
t.Errorf("Failed with error:\n%#v\nexpected to receive:\n%#v", actual, expected) | ||
} | ||
} else { | ||
t.Errorf("Expected test to fail with error:\n%#v", expected) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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.