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

Consider replacing/augmenting MutableStructFields with an idiomatic builder class #98

Open
popematt opened this issue Jul 25, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@popematt
Copy link
Contributor

popematt commented Jul 25, 2024

The StructElement.update() method accepts a Consumer<MutableStructFields>, which has Collection/Map-like APIs. This leads to less-than-ideal code, such as this:

StructElement updated = original.update(fields -> { 
    fields.set("last_updated_date", ionTimestamp(Instant.now());
    fields.set("scores", orginal.get("scores").asStruct().update(scoreFields -> {
        scoreFields.add("ping_pong", ionInt(200)); 
        scoreFields.set("billiards", ionInt(30)); 
        scoreFields.remove("lawn_bowling");
    }));
});

If we had a StructElementBuilder with proper builder-style APIs (i.e. that return this) and specialized functions for modifying fields that have a container value, then we could write code like this instead:

StructElement updated = original.update(fields -> fields
    .set("last_updated_date", ionTimestamp(Instant.now())
    .updateStruct("scores", scores -> scores
        .add("ping_pong", ionInt(200))
        .set("billiards", ionInt(30))
        .remove("lawn_bowling")));

Formatting quibbles aside, the second example is more concise because we can chain together a series of add, set, and remove calls.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant