-
Notifications
You must be signed in to change notification settings - Fork 246
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
Conversation
Implement a serialization method for Java POJOs (produced by calling `build()` on the generated builders such that they are passed by-value to JavaScript instead of by-reference. Also, erase any nulls passed in objects to JS, so they are treated as unset values for the purpose of `key in obj`. This fixes aws/aws-cdk#965 and fixes #375.
4c59d8b
to
5ca8aa2
Compare
....jsii-calc-base/java/src/main/java/software/amazon/jsii/tests/calculator/base/BaseProps.java
Show resolved
Hide resolved
…em matcher Emit jsii diagnostics error when in watch mode, and also format the errors with a "TS9999" error code so that VSCode's $tsc problem matcher will show them as "Problems". Prefix "JSII" in the message to distinguish that these are jsii errors. Fixes #382
I'm trying to get a sense of what is different for .NET developers. You mention:
What does this look like? Are the conversions between .NET and JSII still transparent? |
Yes, they are still transparent. The main change is that now when an object that implements a data type interface is passed down to jsii, it will be serialized as a json map instead of allocated as an anonymous reference. |
Copy: @NetaNir |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wow, what a change
That's going to be fun! |
Any chance it's a small effort to update the C# generator to emit (an overload with?) keyword arguments? And maybe I know it should be a different change, but while you're in here anyway... |
That sounds fine to me! |
Data types ("structs") are defined through a TypeScript interface that only contains properties and doesn't begin with an
I
. This change also adds a requirement that all properties (shallow) are markedreadonly
.This allows passing around structs by-value safely because any consuming code would not expect to be able to write to the object.
Python already passes structs by value, especially in the context of when a struct is used as keyword arguments. This use case will also exist in Ruby.
This is also the general perception around data that's passed around in most programming languages.
To enforce this, the jsii compiler will now fail if a struct includes properties that are not marked
readonly
.Both the Java and .NET generators have been modified to generate builders which allow users to construct structs by serializing them as JSON hashes instead of passed down by reference.
Existing compliance tests in all languages have been fixed.
This change fixes aws/aws-cdk#965 and fixes #375 by erasing any
null
s orundefined
values from passed in objects, so they do not appear to have been defined at all. Added a compliance test called erase-unset-data-values to verify.BREAKING CHANGE: all properties in interfaces which represent data types must be marked as
readonly
. Otherwise, jsii compilation will fail.By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.