Skip to content

Commit

Permalink
feat(kernel): annotate implemented interfaces on "ObjRef"s (#825)
Browse files Browse the repository at this point in the history
* feat: annotate implemented interfaces on "ObjRef"s

Annotating the interfaces implemented by an object instance with the
outbound Object Reference, and allowing the kernel's "create" call to
receive a list of interfaces allows the kernel to use more run-time type
information and operate safer.

* WIP

* WIP progress

* finished fixing java

* fixup regression test expectations

* fix python runtime

* fixup licensing info in NuGet packages

* improve NuGet package metadata

* fix dotnet runtime

* fixup unit tests

* minor style fixes

* some minor tuning

* fixup expecations

* stop using deprecated API

* add some test coverage

* fix test I forgot to update as I changed the code
  • Loading branch information
RomainMuller authored and mergify[bot] committed Oct 29, 2019
1 parent f9c1335 commit a4e2095
Show file tree
Hide file tree
Showing 339 changed files with 2,804 additions and 1,213 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"bump": "bash scripts/bump.sh",
"fetch-dotnet-snk": "bash scripts/fetch-dotnet-snk.sh",
"package": "bash scripts/package.sh",
"test": "lerna run test --stream"
"test": "lerna run test --stream",
"test:update": "UPDATE_DIFF=1 lerna run test --stream"
},
"devDependencies": {
"@types/node": "^10.17.0",
Expand Down
3 changes: 2 additions & 1 deletion packages/jsii-calc-base-of-base/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
"types": "lib/index.d.ts",
"scripts": {
"build": "jsii",
"test": "diff-test test/assembly.jsii .jsii"
"test": "diff-test test/assembly.jsii .jsii",
"test:update": "npm run build && UPDATE_DIFF=1 npm run test"
},
"devDependencies": {
"jsii": "^0.19.0",
Expand Down
3 changes: 2 additions & 1 deletion packages/jsii-calc-base/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
"types": "lib/index.d.ts",
"scripts": {
"build": "jsii",
"test": "diff-test test/assembly.jsii .jsii"
"test": "diff-test test/assembly.jsii .jsii",
"test:update": "npm run build && UPDATE_DIFF=1 npm run test"
},
"dependencies": {
"@scope/jsii-calc-base-of-base": "^0.19.0"
Expand Down
3 changes: 2 additions & 1 deletion packages/jsii-calc-lib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"types": "lib/index.d.ts",
"scripts": {
"build": "jsii",
"test": "diff-test test/assembly.jsii .jsii"
"test": "diff-test test/assembly.jsii .jsii",
"test:update": "npm run build && UPDATE_DIFF=1 npm run test"
},
"dependencies": {
"@scope/jsii-calc-base": "^0.19.0"
Expand Down
36 changes: 34 additions & 2 deletions packages/jsii-calc/lib/compliance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1438,7 +1438,7 @@ export class EraseUndefinedHashValues {
/**
* We expect "prop2" to be erased
*/
public static prop2IsUndefined(): any {
public static prop2IsUndefined(): { [key: string]: any } {
return {
prop1: 'value1',
prop2: undefined
Expand All @@ -1448,7 +1448,7 @@ export class EraseUndefinedHashValues {
/**
* We expect "prop1" to be erased
*/
public static prop1IsNull(): any {
public static prop1IsNull(): { [key: string]: any } {
return {
prop1: null,
prop2: 'value2'
Expand Down Expand Up @@ -1944,3 +1944,35 @@ export interface SupportsNiceJavaBuilderProps {
*/
readonly bar: number;
}

/**
* We can return an anonymous interface implementation from an override without losing the interface
* declarations.
*/
export interface IAnonymousImplementationProvider {
provideAsInterface(): IAnonymouslyImplementMe;
provideAsClass(): Implementation;
}
export class AnonymousImplementationProvider implements IAnonymousImplementationProvider {
private readonly instance = new PrivateType();

public provideAsClass(): Implementation {
return this.instance;
}

public provideAsInterface(): IAnonymouslyImplementMe {
return this.instance;
}
}
export class Implementation {
readonly value = 1337;
}
export interface IAnonymouslyImplementMe {
readonly value: number;
verb(): string;
}
class PrivateType extends Implementation implements IAnonymouslyImplementMe {
public verb() {
return 'to implement';
}
}
193 changes: 190 additions & 3 deletions packages/jsii-calc/test/assembly.jsii
Original file line number Diff line number Diff line change
Expand Up @@ -1027,6 +1027,57 @@
],
"name": "AllowedMethodNames"
},
"jsii-calc.AnonymousImplementationProvider": {
"assembly": "jsii-calc",
"docs": {
"stability": "experimental"
},
"fqn": "jsii-calc.AnonymousImplementationProvider",
"initializer": {},
"interfaces": [
"jsii-calc.IAnonymousImplementationProvider"
],
"kind": "class",
"locationInModule": {
"filename": "lib/compliance.ts",
"line": 1956
},
"methods": [
{
"docs": {
"stability": "experimental"
},
"locationInModule": {
"filename": "lib/compliance.ts",
"line": 1959
},
"name": "provideAsClass",
"overrides": "jsii-calc.IAnonymousImplementationProvider",
"returns": {
"type": {
"fqn": "jsii-calc.Implementation"
}
}
},
{
"docs": {
"stability": "experimental"
},
"locationInModule": {
"filename": "lib/compliance.ts",
"line": 1963
},
"name": "provideAsInterface",
"overrides": "jsii-calc.IAnonymousImplementationProvider",
"returns": {
"type": {
"fqn": "jsii-calc.IAnonymouslyImplementMe"
}
}
}
],
"name": "AnonymousImplementationProvider"
},
"jsii-calc.AsyncVirtualMethods": {
"assembly": "jsii-calc",
"docs": {
Expand Down Expand Up @@ -3318,7 +3369,12 @@
"name": "prop1IsNull",
"returns": {
"type": {
"primitive": "any"
"collection": {
"elementtype": {
"primitive": "any"
},
"kind": "map"
}
}
},
"static": true
Expand All @@ -3335,7 +3391,12 @@
"name": "prop2IsUndefined",
"returns": {
"type": {
"primitive": "any"
"collection": {
"elementtype": {
"primitive": "any"
},
"kind": "map"
}
}
},
"static": true
Expand Down Expand Up @@ -3790,6 +3851,102 @@
],
"name": "GreetingAugmenter"
},
"jsii-calc.IAnonymousImplementationProvider": {
"assembly": "jsii-calc",
"docs": {
"stability": "experimental",
"summary": "We can return an anonymous interface implementation from an override without losing the interface declarations."
},
"fqn": "jsii-calc.IAnonymousImplementationProvider",
"kind": "interface",
"locationInModule": {
"filename": "lib/compliance.ts",
"line": 1952
},
"methods": [
{
"abstract": true,
"docs": {
"stability": "experimental"
},
"locationInModule": {
"filename": "lib/compliance.ts",
"line": 1954
},
"name": "provideAsClass",
"returns": {
"type": {
"fqn": "jsii-calc.Implementation"
}
}
},
{
"abstract": true,
"docs": {
"stability": "experimental"
},
"locationInModule": {
"filename": "lib/compliance.ts",
"line": 1953
},
"name": "provideAsInterface",
"returns": {
"type": {
"fqn": "jsii-calc.IAnonymouslyImplementMe"
}
}
}
],
"name": "IAnonymousImplementationProvider"
},
"jsii-calc.IAnonymouslyImplementMe": {
"assembly": "jsii-calc",
"docs": {
"stability": "experimental"
},
"fqn": "jsii-calc.IAnonymouslyImplementMe",
"kind": "interface",
"locationInModule": {
"filename": "lib/compliance.ts",
"line": 1970
},
"methods": [
{
"abstract": true,
"docs": {
"stability": "experimental"
},
"locationInModule": {
"filename": "lib/compliance.ts",
"line": 1972
},
"name": "verb",
"returns": {
"type": {
"primitive": "string"
}
}
}
],
"name": "IAnonymouslyImplementMe",
"properties": [
{
"abstract": true,
"docs": {
"stability": "experimental"
},
"immutable": true,
"locationInModule": {
"filename": "lib/compliance.ts",
"line": 1971
},
"name": "value",
"type": {
"primitive": "number"
}
}
]
},
"jsii-calc.IAnotherPublicInterface": {
"assembly": "jsii-calc",
"docs": {
Expand Down Expand Up @@ -4740,6 +4897,36 @@
}
]
},
"jsii-calc.Implementation": {
"assembly": "jsii-calc",
"docs": {
"stability": "experimental"
},
"fqn": "jsii-calc.Implementation",
"initializer": {},
"kind": "class",
"locationInModule": {
"filename": "lib/compliance.ts",
"line": 1967
},
"name": "Implementation",
"properties": [
{
"docs": {
"stability": "experimental"
},
"immutable": true,
"locationInModule": {
"filename": "lib/compliance.ts",
"line": 1968
},
"name": "value",
"type": {
"primitive": "number"
}
}
]
},
"jsii-calc.ImplementsInterfaceWithInternal": {
"assembly": "jsii-calc",
"docs": {
Expand Down Expand Up @@ -9932,5 +10119,5 @@
}
},
"version": "0.19.0",
"fingerprint": "98k0qPH9S801EvX681aTDIxgufKA90iJ4SlfZKrm90c="
"fingerprint": "MZT68aeJ7MEeDJORDPKkIyqOqOLC+Yb3LcDU7XrxgCQ="
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Amazon.JSII.JsonModel.Api
{
[JsonDictionary]
public class ObjectReference : JsonDictionaryBase<string, string>
public class ObjectReference : JsonDictionaryBase<string, object>
{
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@

[assembly: CollectionBehavior(DisableTestParallelization = true)]

#pragma warning disable CS0612

namespace Amazon.JSII.Runtime.IntegrationTests
{
/// <summary>
/// Ported from packages/jsii-java-runtime/src/test/java/org/jsii/testing/ComplianceTest.java.
/// </summary>
public class ComplianceTests : IClassFixture<ServiceContainerFixture>
public sealed class ComplianceTests : IClassFixture<ServiceContainerFixture>
{
class RuntimeException : Exception
{
Expand Down Expand Up @@ -127,12 +129,18 @@ public void DynamicTypes()
Assert.Equal(UnixEpoch.AddSeconds(1234), types.AnyProperty);

// json (notice that when deserialized, it is deserialized as a map).
types.AnyProperty = new JObject(new JProperty("Goo",
new JArray(
"Hello",
new JObject(new JProperty("World", 123))
)
));
types.AnyProperty = new Dictionary<string, object>
{
{ "Goo", new object[]
{
"Hello",
new Dictionary<string, object>
{
{ "World", 123 }
}
}
}
};
var @object = (IDictionary<string, object>) types.AnyProperty;
var array = (object[]) @object["Goo"];
var innerObject = (IDictionary<string, object>) array[1];
Expand Down Expand Up @@ -999,6 +1007,15 @@ public void MethodCanReturnArraysOfInterfaces()
Assert.Equal(4, interfaces.Length);
}

[Fact(DisplayName = Prefix + nameof(CanLeverageIndirectInterfacePolymorphism))]
public void CanLeverageIndirectInterfacePolymorphism()
{
var provider = new AnonymousImplementationProvider();
Assert.Equal(1337d, provider.ProvideAsClass().Value);
Assert.Equal(1337d, provider.ProvideAsInterface().Value);
Assert.Equal("to implement", provider.ProvideAsInterface().Verb());
}

class DataRendererSubclass : DataRenderer
{
[JsiiMethod("renderMap", returnsJson: "{\"type\":{\"primitive\":\"string\"}}", parametersJson: "[{\"name\":\"map\",\"type\":{\"collection\":{\"kind\":\"map\",\"elementtype\":{\"primitive\":\"any\"}}}}]", isOverride: true)]
Expand Down
Loading

0 comments on commit a4e2095

Please sign in to comment.