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

experiment defining runtime Condition in terms of option functions #1498

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

clux
Copy link
Member

@clux clux commented May 22, 2024

Unimportant nerd-snipe xperiment that takes advantage of ? on Option to allow simpler Condition impls (sometimes...).

Small, but breaking change due to not finding a way to have overlapping Condition impls for Fn. Maybe it's possible with an indirection trait. LMK if interested and have ideas. Not even sure if this is super useful due to the amount of as_ref() calls you need. Maybe it could be useful if we could define these fns in both ways. i don't know.

The good case is nice tho:

    fn is_pod_running() -> impl Condition<Pod> {
        |obj: Option<&Pod>| Some(obj?.status.as_ref()?.phase.as_ref()? == "Running")
    }

Takes advantage of `?` on `Option` to allow simpler `Condition` impls.

Not a huge change internally, but it is a breaking change.

Signed-off-by: clux <[email protected]>
@clux clux added the question Direction unclear; possibly a bug, possibly could be improved. label May 22, 2024
@clux clux changed the title Try defining runtime Condition in terms of option functions experiment defining runtime Condition in terms of option functions May 22, 2024
Copy link
Member

@Danil-Grigorev Danil-Grigorev left a comment

Choose a reason for hiding this comment

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

Lookup of the conditions is one of the most common scenarios in controller code. If anything can make it simpler, I think it is worth implementing.

}
}
false
let cond = obj
Copy link
Member

Choose a reason for hiding this comment

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

This looks much better and simpler in terms of readability to me.

self.matches_object_option(obj).unwrap_or_default()
}
/// Condition function giving an option wrapped answer
fn matches_object_option(&self, _obj: Option<&K>) -> Option<bool>;
Copy link
Member

Choose a reason for hiding this comment

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

This method can also be named just matches?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Direction unclear; possibly a bug, possibly could be improved.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants