aws / jsii

jsii allows code in any language to naturally interact with JavaScript classes. It is the technology that enables the AWS Cloud Development Kit to deliver polyglot libraries from a single codebase!
https://aws.github.io/jsii
Apache License 2.0
2.66k stars 247 forks source link

Java Transpiled Builder classes to have a method or way to copy the instance #4679

Open bdoyle0182 opened 1 month ago

bdoyle0182 commented 1 month ago

Describe the feature

When transpiling from typescript interfaces to java, Jsii creates builder classes for the interface to. We have places in our library where we want to take in an existing pre-built instance of the interface and be able to copy the existing state and make some overrides. Right now it seems the only way is to manually copy all of the existing stateful members of the class into a new builder.

     JsiiClassProps newProps = new JsiiClassBuilder()
          // call all setting methods to copy state like so .myJsiiInterfaceMember(props.getMyJsiiInterfaceMember())
     .build();

versus something like this where the builder can just take the state of the existing inteface object as a common pattern w/ a constructor param

     JsiiClassProps newProps = new JsiiClassBuilder(props)
          .myJsiiInterfaceMemberToOverride(props.getMyJsiiInterfaceMemberToOverride())
     .build();

whereas in typescript this can be easily accomplished already w/ the spread operator on instantiation

Use Case

See description, we regularly need to perform some overrides on an existing base level instance, having to override everything using all of the base level instance member getters is tedious, verbose, and error prone.

Proposed Solution

Have an additional constructor on the builder that takes in an existing instance of the built type T class and creates the initial state of the builder

     JsiiClassProps newProps = new JsiiClassBuilder(props)
          .myJsiiInterfaceMemberToOverride(props.getMyJsiiInterfaceMemberToOverride())
     .build();

Other Information

No response

Acknowledgements

CDK version used

2.162.1

Environment details (OS name and version, etc.)

MacOS