pulumi / pulumi/pulumi-java

Difficult to mutate nested args

Open
#240 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

api/suggestion area/languages kind/enhancement language/java
Dominant language
Java
Stars
85
Forks
26
Avg merge
11h 49m
Merged PRs (30d)
22

Description

Hello!

  • Vote on this issue by adding a 👍 reaction
  • To contribute a fix for this issue, leave a comment (and link to your pull request, if you've opened one already)

Issue details

I followed the ideas in https://github.com/pulumi/pulumi/issues/8584 to see how Java SDK stacks up for mutating nested properties of args. The idea is that users might want to write methods that abstracts some aspect of argument construction, and apply these methods to mutate something. Mutation is just what is easy with nested arguments. An immutable API is clunky and gives rise to helpers lenses, optics, which I don't think we have scope to support here.

The outcome of a quick investigation is positive: users can currently write functions that modify nested args. It just takes a little patience as it has to be done via builders and via the Output layer. For example:

public class MutableArgsClass extends Stack {
    public MutableArgsClass() {

        var deploymentArgsBuilder = DeploymentArgs.builder();

        // Cannot get DeploymentSpecArgs builder from DeploymentArgsBuilder. Can only set the args wholesale,
        // not modify them.
        deploymentArgsBuilder.spec(DeploymentSpecArgs.builder().replicas(2).build());
        var deploymentArgs = deploymentArgsBuilder.build();

        // We cat get nested args like .spec.replicas, but we cannot set them. Everything is also
        // wrapped with Output making it extra hard.
        Output<Integer> n = deploymentArgs.getSpec().apply(x -> x.getReplicas().applyValue(y -> y + 1));

        // Perhaps we can after all "modify" the nested replicas argument through some tricks?
        new Deployment("d", withExtraReplicas(deploymentArgs, 3));
    }

    public DeploymentArgs withExtraReplicas(DeploymentArgs args, int extraReplicas) {
        Output<DeploymentSpecArgs> deploymentSpecArgs = args.getSpec()
                .apply(spec ->
                    spec.getReplicas().applyValue(replicas ->
                        DeploymentSpecArgs.builder(spec)
                                .replicas(replicas + extraReplicas)
                                .build()));
        return DeploymentArgs.builder(args).spec(deploymentSpecArgs).build();
    }
}

So good news is that this is possible, bad news is that it's clunky. Any ideas how to simplify this are welcome.

Steps to reproduce

Expected:
Actual:

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the nested DeploymentArgs and DeploymentSpecArgs builder usage in the issue example, then review the Output-based mutation path and the related pulumi/pulumi#8584 discussion. Done would mean agreeing on and implementing a simpler supported way to mutate nested arguments, with validation that the example remains usable.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
backend-api-design
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.