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 ToString and Default impls #7

Closed

Conversation

jsturtevant
Copy link
Collaborator

Add a few implementations that make it easier to use as a library for creating the config object.

Found while adding support to generate packages in runwasi containerd/runwasi#631

Signed-off-by: James Sturtevant <[email protected]>
Copy link
Collaborator

@thomastaylor312 thomastaylor312 left a comment

Choose a reason for hiding this comment

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

Some general discussion points on whether we need these additions or if there could be another way of doing them

@@ -152,6 +156,13 @@ impl ToConfig for WasmConfig {
}
}

impl ToString for WasmConfig {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Generally speaking, it isn't good practice to use serialization in ToString because of the overhead involved with serialization (plus the possibility of an error)

@@ -128,6 +128,10 @@ impl WasmConfig {
annotations,
}
}

pub fn to_string_pretty(&self) -> anyhow::Result<String> {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I feel like it would be easier for consumers to just use the serialization tool of their choice to get a string here. Based on what I see in your runwasi PR here, maybe the OciConfig trait could have a to_json method that encapsulates this instead

@@ -18,7 +18,7 @@ pub trait ToConfig {
}

/// The config type struct for `application/wasm`
#[derive(Serialize, Deserialize, Debug)]
#[derive(Serialize, Deserialize, Debug, Default)]
Copy link
Collaborator

Choose a reason for hiding this comment

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

So I actually avoided Default on purpose because the layer_digests needs to be set with the content of whatever this config is based on and should always be tied to a specific component (hence the loader functions). This was mostly to avoid people shooting themselves in the foot by not setting things on the config properly so it could just be my paranoia. If we want to go down this route, there are 2 ways we could handle this though:

  • Manually implement default and make sure the architecture field is set and the os field defaults to wasip2.
  • Create a new function that creates and empty one and takes os as a parameter so people can set it. You could also have new_component_config and new_module_config that are wrappers that just set the os field for you correctly.

With that said however, based on what I saw in the linked runwasi PR:

let mut builder = Builder::<WasmConfig>::default();

Could it just be better for your use case to do impl Default for Builder<WasmConfig>?

@jsturtevant
Copy link
Collaborator Author

These comments make sense to me, I was initially following what was done in https://github.com/containers/oci-spec-rs but can add these implementations as suggested directly in runwasi 👍

Thanks for the feedback!

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 this pull request may close these issues.

2 participants