Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: pass data types (structs) by-value instead of by-ref #376

Merged
merged 21 commits into from
Mar 20, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
629 changes: 163 additions & 466 deletions packages/codemaker/package-lock.json

Large diffs are not rendered by default.

4 changes: 1 addition & 3 deletions packages/jsii-build-tools/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 37 additions & 0 deletions packages/jsii-calc/lib/compliance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1335,3 +1335,40 @@ export class Constructors {
return new PrivateClass();
}
}

// fixture to verify that null/undefined values in object hashes are treated
// as "unset". see awslabs/aws-cdk#965.
export interface EraseUndefinedHashValuesOptions {
option1?: string;
option2?: string;
}

export class EraseUndefinedHashValues {
/**
* Returns `true` if `key` is defined in `opts`. Used to check that undefined/null hash values
* are being erased when sending values from native code to JS.
*/
public static doesKeyExist(opts: EraseUndefinedHashValuesOptions, key: string): boolean {
return key in opts;
}

/**
* We expect "prop2" to be erased
*/
public static prop2IsUndefined(): any {
return {
prop1: 'value1',
prop2: undefined
};
}

/**
* We expect "prop1" to be erased
*/
public static prop1IsNull(): any {
return {
prop1: null,
prop2: 'value2'
};
}
}
7 changes: 2 additions & 5 deletions packages/jsii-calc/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

82 changes: 81 additions & 1 deletion packages/jsii-calc/test/assembly.jsii
Original file line number Diff line number Diff line change
Expand Up @@ -1452,6 +1452,86 @@
],
"name": "DoubleTrouble"
},
"jsii-calc.EraseUndefinedHashValues": {
"assembly": "jsii-calc",
"fqn": "jsii-calc.EraseUndefinedHashValues",
"initializer": {
"initializer": true
},
"kind": "class",
"methods": [
{
"docs": {
"comment": "Returns `true` if `key` is defined in `opts`. Used to check that undefined/null hash values\nare being erased when sending values from native code to JS."
},
"name": "doesKeyExist",
"parameters": [
{
"name": "opts",
"type": {
"fqn": "jsii-calc.EraseUndefinedHashValuesOptions"
}
},
{
"name": "key",
"type": {
"primitive": "string"
}
}
],
"returns": {
"primitive": "boolean"
},
"static": true
},
{
"docs": {
"comment": "We expect \"prop1\" to be erased"
},
"name": "prop1IsNull",
"returns": {
"primitive": "any"
},
"static": true
},
{
"docs": {
"comment": "We expect \"prop2\" to be erased"
},
"name": "prop2IsUndefined",
"returns": {
"primitive": "any"
},
"static": true
}
],
"name": "EraseUndefinedHashValues"
},
"jsii-calc.EraseUndefinedHashValuesOptions": {
"assembly": "jsii-calc",
"datatype": true,
"fqn": "jsii-calc.EraseUndefinedHashValuesOptions",
"kind": "interface",
"name": "EraseUndefinedHashValuesOptions",
"properties": [
{
"abstract": true,
"name": "option1",
"type": {
"optional": true,
"primitive": "string"
}
},
{
"abstract": true,
"name": "option2",
"type": {
"optional": true,
"primitive": "string"
}
}
]
},
"jsii-calc.ExportedBaseClass": {
"assembly": "jsii-calc",
"fqn": "jsii-calc.ExportedBaseClass",
Expand Down Expand Up @@ -3949,5 +4029,5 @@
}
},
"version": "0.7.15",
"fingerprint": "IWSOEhdZzuvrss5K2WBjZCawXayV13yCAKTj/kJ9+mo="
"fingerprint": "DXEqEDn4p/1Rc0OVCrsby4jumnxo9mWqSNF9sb/laGU="
}
10 changes: 3 additions & 7 deletions packages/jsii-dotnet-jsonmodel/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 3 additions & 7 deletions packages/jsii-dotnet-runtime-test/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 3 additions & 7 deletions packages/jsii-dotnet-runtime/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 2 additions & 5 deletions packages/jsii-java-runtime-test/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import software.amazon.jsii.tests.calculator.DerivedStruct;
import software.amazon.jsii.tests.calculator.DoNotOverridePrivates;
import software.amazon.jsii.tests.calculator.DoubleTrouble;
import software.amazon.jsii.tests.calculator.EraseUndefinedHashValues;
import software.amazon.jsii.tests.calculator.EraseUndefinedHashValuesOptions;
import software.amazon.jsii.tests.calculator.GiveMeStructs;
import software.amazon.jsii.tests.calculator.GreetingAugmenter;
import software.amazon.jsii.tests.calculator.IFriendlier;
Expand Down Expand Up @@ -63,6 +65,7 @@
import java.util.Map;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
Expand Down Expand Up @@ -982,6 +985,20 @@ public void objRefsAreLabelledUsingWithTheMostCorrectType() {
assertTrue(ifaceRef instanceof IPublicInterface);
}

/**
* Verifies that data values that are not set are recognized as unset keys
* in JavaScript-land. See https://github.com/awslabs/jsii/issues/375
*/
@Test
public void eraseUnsetDataValues() {
EraseUndefinedHashValuesOptions opts = EraseUndefinedHashValuesOptions.builder()
.withOption1("option1")
.build();

assertTrue(EraseUndefinedHashValues.doesKeyExist(opts, "option1"));
assertFalse(EraseUndefinedHashValues.doesKeyExist(opts, "option2"));
}

static class MulTen extends Multiply {
public MulTen(final int value) {
super(new Number(value), new Number(10));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import software.amazon.jsii.JsiiClient;
import software.amazon.jsii.JsiiException;
import software.amazon.jsii.JsiiObjectMapper;
import software.amazon.jsii.JsiiObjectRef;
import software.amazon.jsii.JsiiPromise;
import software.amazon.jsii.JsiiRuntime;
import software.amazon.jsii.JsiiSerializable;
import software.amazon.jsii.api.Callback;
import software.amazon.jsii.api.JsiiOverride;
import org.junit.Before;
Expand Down Expand Up @@ -217,6 +220,25 @@ public void staticMethods() {
assertEquals("hello ,Foo!", result.textValue());
}

/**
* If a JsiiSerializable object has a method named "$jsii$toJson", it will be
* used to serialize the object instead of the normal
*/
@Test
public void serializeViaJsiiToJsonIfExists() {
JsiiObjectMapper om = JsiiObjectMapper.instance;
JsonNode result = om.valueToTree(new JsiiSerializable() {
public JsonNode $jsii$toJson() {
ObjectNode node = JSON.objectNode();
node.set("foo", OM.valueToTree("bar"));
node.set("hey", OM.valueToTree(42));
return node;
}
});

assertEquals("{\"foo\":\"bar\",\"hey\":42}", result.toString());
}

private ArrayNode toSandboxArray(final Object... values) {
return OM.valueToTree(values);
}
Expand Down
10 changes: 3 additions & 7 deletions packages/jsii-java-runtime/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public final class JsiiClient {
/**
* Jsii custom object mapper.
*/
private static final JsiiObjectMapper JSII_OM = new JsiiObjectMapper();
private static final JsiiObjectMapper JSII_OM = JsiiObjectMapper.instance;

/**
* TCP port to connect to (always "localhost").
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public final class JsiiEngine implements JsiiCallbackHandler {
/**
* JSON object mapper.
*/
private static final JsiiObjectMapper OM = new JsiiObjectMapper();
private static final JsiiObjectMapper OM = JsiiObjectMapper.instance;

/**
* The set of modules we already loaded into the VM.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package software.amazon.jsii;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.TreeNode;
import com.fasterxml.jackson.databind.JsonNode;

import javax.annotation.Nullable;
Expand All @@ -13,7 +14,7 @@ public class JsiiObject implements JsiiSerializable {
/**
* JSON object mapper.
*/
private static final JsiiObjectMapper OM = new JsiiObjectMapper();
private static final JsiiObjectMapper OM = JsiiObjectMapper.instance;

/**
* The jsii engine used by this object.
Expand Down
Loading