Skip to content

api/types/filters: GetBoolOrDefault: remove unreachableCode#48745

Merged
thaJeztah merged 1 commit intomoby:masterfrom
thaJeztah:filters_remove_unreachableCode
Oct 25, 2024
Merged

api/types/filters: GetBoolOrDefault: remove unreachableCode#48745
thaJeztah merged 1 commit intomoby:masterfrom
thaJeztah:filters_remove_unreachableCode

Conversation

@thaJeztah
Copy link
Copy Markdown
Member

We already check if

  • the key is set (otherwise default)
  • a value is set (otherwise default and error)

This check can be simplified to check if they're equal (boolean cannot be both true and false), or both false (boolean must be either true or false), although the latter could be considered for a tri-state boolean (but we already do this through the "not set" case).

We may need some additional checks, for example, currently it ignores invalid values if the filter contains at least one valid one (e.g. ["true", "bananas"]).

- A picture of a cute animal (not mandatory but encouraged)

@thaJeztah thaJeztah added status/2-code-review kind/refactor PR's that refactor, or clean-up code labels Oct 24, 2024
@thaJeztah thaJeztah added this to the 28.0.0 milestone Oct 24, 2024
@thaJeztah thaJeztah self-assigned this Oct 24, 2024
invalid := !isFalse && !isTrue

if conflicting || invalid {
if isFalse == isTrue {
Copy link
Copy Markdown
Contributor

@vvoland vvoland Oct 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will be true if the there are conflicting values specified (both truthy and falsy), in which case it will be treated as truthy value.

(like filter=true,filter=false)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, but that was the case before as well, correct?

conflicting := isFalse && isTrue
invalid := !isFalse && !isTrue
if conflicting || invalid {

The above; if both are false or both are true we return default value (with an error), so that's the same as if isFalse == isTrue`

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, right.
I guess that's why I went with a more explicit code, so it's easier to wrap your head around truthness and falseness of isFalse and isTrue variables 😅

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can add a comment "a boolean cannot be both true and false" ?

Copy link
Copy Markdown
Contributor

@vvoland vvoland Oct 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure that's helpful 🙈

Maybe something like:

Suggested change
if isFalse == isTrue {
// Either no or conflicting truthy/falsy value were provided
if isFalse == isTrue {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vvoland updated; added comment; PTAL

We already check if

- the key is set (otherwise default)
- a value is set (otherwise default and error)

This check can be simplified to check if they're equal (boolean cannot be both
true and false), or both false (boolean must be either true or false), although
the latter could be considered for a tri-state boolean (but we already do this
through the "not set" case).

We may need some additional checks, for example, currently it ignores invalid
values if the filter contains at least one valid one (e.g. ["true", "bananas"]).

Signed-off-by: Sebastiaan van Stijn <[email protected]>
@thaJeztah thaJeztah force-pushed the filters_remove_unreachableCode branch from 40efc3c to 7d70892 Compare October 24, 2024 21:00
@thaJeztah thaJeztah merged commit 9b2bff8 into moby:master Oct 25, 2024
@thaJeztah thaJeztah deleted the filters_remove_unreachableCode branch October 25, 2024 10:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind/refactor PR's that refactor, or clean-up code status/2-code-review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants