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

Add FromIterator<Process> for Launch #401

Closed
wants to merge 3 commits into from
Closed

Add FromIterator<Process> for Launch #401

wants to merge 3 commits into from

Conversation

joshwlewis
Copy link
Member

@joshwlewis joshwlewis commented Jun 15, 2022

Currently, collecting a dynamically created list of Processes into a Launch has to be done manually. Something like this:

let mut launch = Launch::new();
let procs: Vec<Process> = ["web", "worker"]
    .iter()
    .map(|name| ProcessBuilder::new(name.parse().unwrap(), name.to_string()).build())
    .collect();
for proc in procs.iter() {
    launch = launch.process(proc.clone());
}

With a FromIterator, it gets a bit easier:

let launch: Launch = ["web", "worker"]
    .iter()
    .map(|name| ProcessBuilder::new(name.parse().unwrap(), name.to_string()).build())
    .collect();

@joshwlewis joshwlewis requested a review from a team as a code owner June 15, 2022 19:35
@edmorley edmorley requested a review from Malax June 17, 2022 09:00
@edmorley edmorley added the enhancement New feature or request label Jun 17, 2022
@Malax
Copy link
Member

Malax commented Jun 20, 2022

I'm not 💯 sold on the idea that an iterator of Process should be collectable into a CNB launch config - from a semantics standpoint. A launch config is more than just a list of processes and when we add SBOM support, the non-process bits of the launch config become more important.

I however do agree that it should be easier to construct a launch config when you just have a collection of processes.

What do folks think about adding a Launch::processes that allows users to add anything that can be turned into an Iterator of Process (via the appropriate traits) as the list of processes for a launch config? I've seen this pattern for builders before where you can either add single items or multiples using a different function. The most prominent example is probably std::process::Command (arg and args). See code below:

#[must_use]
pub fn processes<P: Into<Process>, I: IntoIterator<Item = P>>(mut self, processes: I) -> Self {
    for process in processes {
        self.processes.push(process.into())
    }

    self
}

Using the API above, the launch_from_iterator test from this PR would look like this:

#[test]
fn launch_from_iterator() {
    let launch: Launch = Launch::new().processes(
        ["web", "worker"]
            .iter()
            .map(|&name| ProcessBuilder::new(name.parse().unwrap(), name).build()),
    );

    assert_eq!(launch.processes[0].command, "web");
}

IMO, this is not worse in terms of verbosity than the original API proposal from this PR as this only adds one line, but also removes the collect call. But we are much more explicit about what happens here.

Copy link
Member

@Malax Malax left a comment

Choose a reason for hiding this comment

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

See comment on the PR.

@edmorley
Copy link
Member

I've seen this pattern for builders before where you can either add single items or multiples using a different function. The most prominent example is probably std::process::Command (arg and args).

Plus several similar APIs in libcnb, eg env vs env in libcnb-test:
https://docs.rs/libcnb-test/latest/libcnb_test/struct.PrepareContainerContext.html#method.env

@Malax Malax mentioned this pull request Jun 20, 2022
@joshwlewis
Copy link
Member Author

A launch config is more than just a list of processes and when we add SBOM support, the non-process bits of the launch config become more important.

Great point.

This seems fine:

    let launch: Launch = Launch::new().processes(
        ["web", "worker"]
            .iter()
            .map(|&name| ProcessBuilder::new(name.parse().unwrap(), name).build()),
    );

Though, I've kind of developed a taste for chaining methods over nested arguments. I guess I'm noticing a pattern in my buildpacks that looks like

ThingBuilder::new(
  Other::new(
    Blah::new().build()
  ).build()
).build()

These just read inside out to me compared to a chained approach, and I guess that was partly motivating the FromIterator for me. I think I may just wanting a |> type operator for Rust.

@joshwlewis joshwlewis closed this Jul 6, 2022
@joshwlewis joshwlewis deleted the launch-iter branch July 6, 2022 15:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request libcnb-data
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants