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

java-client/src/main/java/co/elastic/clients/elasticsearch/indices/update_aliases/Action.java The Builder of Action of updateAliases should use [return self()] instead of [return this] and change the return type to [Builder] #884

Closed
Jiuyoung opened this issue Sep 19, 2024 · 4 comments
Labels
Category: Question Not an issue but a question. May lead to enhancing docs

Comments

@Jiuyoung
Copy link

Java API client version

8.15.1

Java version

11

Elasticsearch Version

8.15.0

Problem description

The following is the relevant code, which is causing the inability to achieve chaining as expected.

public static class Builder extends WithJsonObjectBuilderBase<Builder> implements ObjectBuilder<Action> {
private Kind _kind;
private Object _value;
@Override
protected Builder self() {
return this;
}
public ObjectBuilder<Action> add(AddAction v) {
this._kind = Kind.Add;
this._value = v;
return this;
}
public ObjectBuilder<Action> add(Function<AddAction.Builder, ObjectBuilder<AddAction>> fn) {
return this.add(fn.apply(new AddAction.Builder()).build());
}
public ObjectBuilder<Action> remove(RemoveAction v) {
this._kind = Kind.Remove;
this._value = v;
return this;
}
public ObjectBuilder<Action> remove(Function<RemoveAction.Builder, ObjectBuilder<RemoveAction>> fn) {
return this.remove(fn.apply(new RemoveAction.Builder()).build());
}
public ObjectBuilder<Action> removeIndex(RemoveIndexAction v) {
this._kind = Kind.RemoveIndex;
this._value = v;
return this;
}

@Jiuyoung Jiuyoung changed the title The Builder of Action of updateAliases should use [return self()] instead of [return this] and change the return type to [Builder] java-client/src/main/java/co/elastic/clients/elasticsearch/indices/update_aliases/Action.java The Builder of Action of updateAliases should use [return self()] instead of [return this] and change the return type to [Builder] Sep 19, 2024
@l-trotta
Copy link
Contributor

Hello! I'm a bit confused, since this is the same pattern used in all the classes of the client. Could you show me which kind of chaining are you trying to achieve?

@Jiuyoung
Copy link
Author

@l-trotta

Thanks for your comment. The following code describes this situation.

public static void main(String[] args) throws IOException {
    // DESC: I would like to rewrite the following code to use chaining.
    es.indices().updateAliases(
            request -> request.actions(
                    actionsBuilder -> {
                        // DESC: these code used to build actions list
                        actionsBuilder.add(add -> add.alias("alias1").index("index1"));
                        actionsBuilder.add(add -> add.alias("alias1").index("index2"));
                        return actionsBuilder;
                    }
            )
    );
    // DESC: I am trying to write the following code
    es.indices().updateAliases(
            request -> request.actions(
                    actionsBuilder -> actionsBuilder
                            .add(add -> add.alias("alias1").index("index1"))
                            // ERROR: This will not work as expected with error [Cannot resolve method 'add' in 'ObjectBuilder']
                            .add(add -> add.alias("alias1").index("index2"))
            )
    );
}

This picture show code in IDEA.
Image

@l-trotta
Copy link
Contributor

l-trotta commented Sep 24, 2024

Got it! So for builders that allow multiple values we have 3 types of constructors:
Image
The last one just works for a single value, so to add multiple value you can either use a list:

es.indices().updateAliases(
    u -> u.actions(List.of(Action.of(a -> a
                .add(add -> add
                    .alias("alias1")
                    .index("index1")
                )
            ),
            Action.of(a -> a
                .add(add -> add
                    .alias("alias1")
                    .index("index1")
                )
            )
        )
    )
);

or comma separated values:

es.indices().updateAliases(
    u -> u.actions(Action.of(a -> a
            .add(add -> add
                .alias("alias1")
                .index("index1")
            )
        ),
        Action.of(a -> a
            .add(add -> add
                .alias("alias1")
                .index("index1")
            )
        )
    )
);

We chose this approach to keep the syntax of the request similar to the original json one, which accepts a list of values or a single value.

@l-trotta l-trotta added the Category: Question Not an issue but a question. May lead to enhancing docs label Sep 24, 2024
@Jiuyoung
Copy link
Author

^_^ Very nice! Thank you! I will close this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Category: Question Not an issue but a question. May lead to enhancing docs
Projects
None yet
Development

No branches or pull requests

2 participants