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 treating null as "none" in ChannelPipeline#add(..) functionality #10728

Closed
Bennett-Lynch opened this issue Oct 23, 2020 · 1 comment · Fixed by #10751
Closed

Consider treating null as "none" in ChannelPipeline#add(..) functionality #10728

Bennett-Lynch opened this issue Oct 23, 2020 · 1 comment · Fixed by #10751

Comments

@Bennett-Lynch
Copy link
Contributor

Bennett-Lynch commented Oct 23, 2020

When adding handlers to a pipeline using the varargs methods, if Netty encounters a handler that is null, it will cease the insertion logic:

public final ChannelPipeline addLast(EventExecutorGroup executor, ChannelHandler... handlers) {
ObjectUtil.checkNotNull(handlers, "handlers");
for (ChannelHandler h: handlers) {
if (h == null) {
break;
}
addLast(executor, null, h);
}

I propose that it would be more intuitive to use continue here, rather than break.

This would allow for handlers to be conditionally added with ease. E.g.,

ch.pipeline().addLast(
        new FooHandler(),
        condition ? new BarHandler() : null,
        new BazHandler()
);

This is technically a behavior change so I'm not sure if it would be allowed in Netty 4 or not, but it seems reasonably safe.

@normanmaurer
Copy link
Member

yeah sounds a good idea...can you do a PR against master ?

Bennett-Lynch added a commit to Bennett-Lynch/netty that referenced this issue Oct 29, 2020
Motivation

Allowing null handlers allows for more convenient idioms in
conditionally adding handlers, e.g.,

ch.pipeline().addLast(
        new FooHandler(),
        condition ? new BarHandler() : null,
        new BazHandler()
);

Modifications

* Change addFirst(..) and addLast(..) to skip null handlers, rather than
break or short-circuit.
* Add new unit tests.

Result

* Makes addFirst(..) and addLast(..) behavior more consistent
* Resolves netty#10728
Bennett-Lynch added a commit to Bennett-Lynch/netty that referenced this issue Oct 30, 2020
Motivation

Allowing null handlers allows for more convenient idioms in
conditionally adding handlers, e.g.,

ch.pipeline().addLast(
        new FooHandler(),
        condition ? new BarHandler() : null,
        new BazHandler()
);

Modifications

* Change addFirst(..) and addLast(..) to skip null handlers, rather than
break or short-circuit.
* Add new unit tests.

Result

* Makes addFirst(..) and addLast(..) behavior more consistent
* Resolves netty#10728
normanmaurer pushed a commit that referenced this issue Nov 3, 2020
…10751)

Motivation:

Allowing null handlers allows for more convenient idioms in
conditionally adding handlers, e.g.,

ch.pipeline().addLast(
        new FooHandler(),
        condition ? new BarHandler() : null,
        new BazHandler()
);

Modifications:

* Change addFirst(..) and addLast(..) to skip null handlers, rather than
break or short-circuit.
* Add new unit tests.

Result:

* Makes addFirst(..) and addLast(..) behavior more consistent
* Resolves #10728
Bennett-Lynch added a commit to Bennett-Lynch/netty that referenced this issue Nov 4, 2020
Motivation

Allowing null handlers allows for more convenient idioms in
conditionally adding handlers, e.g.,

ch.pipeline().addLast(
        new FooHandler(),
        condition ? new BarHandler() : null,
        new BazHandler()
);

Modifications

* Change addFirst(..) and addLast(..) to skip null handlers, rather than
break or short-circuit.
* Add new unit tests.

Result

* Makes addFirst(..) and addLast(..) behavior more consistent
* Resolves netty#10728
Bennett-Lynch added a commit to Bennett-Lynch/netty that referenced this issue Nov 4, 2020
Motivation

Allowing null handlers allows for more convenient idioms in
conditionally adding handlers, e.g.,

ch.pipeline().addLast(
        new FooHandler(),
        condition ? new BarHandler() : null,
        new BazHandler()
);

Modifications

* Change addFirst(..) and addLast(..) to skip null handlers, rather than
break or short-circuit.
* Add new unit tests.

Result

* Makes addFirst(..) and addLast(..) behavior more consistent
* Resolves netty#10728
Bennett-Lynch added a commit to Bennett-Lynch/netty that referenced this issue Jan 19, 2021
Motivation

Allowing null handlers allows for more convenient idioms in
conditionally adding handlers, e.g.,

ch.pipeline().addLast(
        new FooHandler(),
        condition ? new BarHandler() : null,
        new BazHandler()
);

Modifications

* Change addFirst(..) and addLast(..) to skip null handlers, rather than
break or short-circuit.
* Add new unit tests.

Result

* Makes addFirst(..) and addLast(..) behavior more consistent
* Resolves netty#10728
Bennett-Lynch added a commit to Bennett-Lynch/netty that referenced this issue Jan 28, 2021
Motivation

Allowing null handlers allows for more convenient idioms in
conditionally adding handlers, e.g.,

ch.pipeline().addLast(
        new FooHandler(),
        condition ? new BarHandler() : null,
        new BazHandler()
);

Modifications

* Change addFirst(..) and addLast(..) to skip null handlers, rather than
break or short-circuit.
* Add new unit tests.

Result

* Makes addFirst(..) and addLast(..) behavior more consistent
* Resolves netty#10728
chrisvest pushed a commit that referenced this issue Jan 29, 2021
…ndlers (#10776)

Allow and skip null handlers when adding a vararg list of handlers

Motivation

Allowing null handlers allows for more convenient idioms in
conditionally adding handlers, e.g.,

ch.pipeline().addLast(
        new FooHandler(),
        condition ? new BarHandler() : null,
        new BazHandler()
);

Modifications

* Change addFirst(..) and addLast(..) to skip null handlers, rather than
break or short-circuit.
* Add new unit tests.

Result

* Makes addFirst(..) and addLast(..) behavior more consistent
* Resolves #10728
raidyue pushed a commit to raidyue/netty that referenced this issue Jul 8, 2022
…etty#10751)

Motivation:

Allowing null handlers allows for more convenient idioms in
conditionally adding handlers, e.g.,

ch.pipeline().addLast(
        new FooHandler(),
        condition ? new BarHandler() : null,
        new BazHandler()
);

Modifications:

* Change addFirst(..) and addLast(..) to skip null handlers, rather than
break or short-circuit.
* Add new unit tests.

Result:

* Makes addFirst(..) and addLast(..) behavior more consistent
* Resolves netty#10728
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants