fix(sdk): fix builder pattern to actually work with default values

In previous versions the builder pattern required all values to be set.
This has not been fixed, so that default values are allowed.
This commit is contained in:
Kasper Juul Hermansen 2023-02-25 01:03:24 +01:00 committed by Kasper Juul Hermansen
parent 6a9a560cdc
commit ecca036bc6
4 changed files with 55 additions and 90 deletions

View File

@ -86,7 +86,7 @@ pub fn render_optional_field_args(
} }
quote! { quote! {
$(a.description.pipe(|d| format_struct_comment(d))) $(a.description.pipe(|d| format_struct_comment(d)))
#[builder(setter(into, strip_option))] #[builder(setter(into, strip_option), default)]
pub $(format_struct_name(&a.name)): Option<$(type_)>, pub $(format_struct_name(&a.name)): Option<$(type_)>,
} }
}); });

View File

@ -25,28 +25,3 @@ impl Engine {
self.from_cli(cfg).await self.from_cli(cfg).await
} }
} }
#[cfg(test)]
mod tests {
use crate::{config::Config, connect_params::ConnectParams};
use super::Engine;
// TODO: these tests potentially have a race condition
#[tokio::test]
async fn engine_can_start() {
let engine = Engine::new();
let params = engine
.start(&Config::new(None, None, None, None))
.await
.unwrap();
assert_ne!(
params.0,
ConnectParams {
port: 123,
session_token: "123".into()
}
)
}
}

View File

@ -12,13 +12,3 @@ pub async fn get_schema() -> eyre::Result<IntrospectionResponse> {
Ok(schema) Ok(schema)
} }
#[cfg(test)]
mod tests {
use super::get_schema;
#[tokio::test]
async fn can_get_schema() {
let _ = get_schema().await.unwrap();
}
}

View File

@ -50,134 +50,134 @@ pub struct Container {
pub struct ContainerBuildOpts<'a> { pub struct ContainerBuildOpts<'a> {
/// Path to the Dockerfile to use. /// Path to the Dockerfile to use.
/// Default: './Dockerfile'. /// Default: './Dockerfile'.
#[builder(setter(into, strip_option))] #[builder(setter(into, strip_option), default)]
pub dockerfile: Option<&'a str>, pub dockerfile: Option<&'a str>,
/// Additional build arguments. /// Additional build arguments.
#[builder(setter(into, strip_option))] #[builder(setter(into, strip_option), default)]
pub build_args: Option<Vec<BuildArg>>, pub build_args: Option<Vec<BuildArg>>,
/// Target build stage to build. /// Target build stage to build.
#[builder(setter(into, strip_option))] #[builder(setter(into, strip_option), default)]
pub target: Option<&'a str>, pub target: Option<&'a str>,
} }
#[derive(Builder, Debug, PartialEq)] #[derive(Builder, Debug, PartialEq)]
pub struct ContainerEndpointOpts<'a> { pub struct ContainerEndpointOpts<'a> {
/// The exposed port number for the endpoint /// The exposed port number for the endpoint
#[builder(setter(into, strip_option))] #[builder(setter(into, strip_option), default)]
pub port: Option<isize>, pub port: Option<isize>,
/// Return a URL with the given scheme, eg. http for http:// /// Return a URL with the given scheme, eg. http for http://
#[builder(setter(into, strip_option))] #[builder(setter(into, strip_option), default)]
pub scheme: Option<&'a str>, pub scheme: Option<&'a str>,
} }
#[derive(Builder, Debug, PartialEq)] #[derive(Builder, Debug, PartialEq)]
pub struct ContainerExecOpts<'a> { pub struct ContainerExecOpts<'a> {
/// Command to run instead of the container's default command (e.g., ["run", "main.go"]). /// Command to run instead of the container's default command (e.g., ["run", "main.go"]).
#[builder(setter(into, strip_option))] #[builder(setter(into, strip_option), default)]
pub args: Option<Vec<&'a str>>, pub args: Option<Vec<&'a str>>,
/// Content to write to the command's standard input before closing (e.g., "Hello world"). /// Content to write to the command's standard input before closing (e.g., "Hello world").
#[builder(setter(into, strip_option))] #[builder(setter(into, strip_option), default)]
pub stdin: Option<&'a str>, pub stdin: Option<&'a str>,
/// Redirect the command's standard output to a file in the container (e.g., "/tmp/stdout"). /// Redirect the command's standard output to a file in the container (e.g., "/tmp/stdout").
#[builder(setter(into, strip_option))] #[builder(setter(into, strip_option), default)]
pub redirect_stdout: Option<&'a str>, pub redirect_stdout: Option<&'a str>,
/// Redirect the command's standard error to a file in the container (e.g., "/tmp/stderr"). /// Redirect the command's standard error to a file in the container (e.g., "/tmp/stderr").
#[builder(setter(into, strip_option))] #[builder(setter(into, strip_option), default)]
pub redirect_stderr: Option<&'a str>, pub redirect_stderr: Option<&'a str>,
/// Provide dagger access to the executed command. /// Provide dagger access to the executed command.
/// Do not use this option unless you trust the command being executed. /// Do not use this option unless you trust the command being executed.
/// The command being executed WILL BE GRANTED FULL ACCESS TO YOUR HOST FILESYSTEM. /// The command being executed WILL BE GRANTED FULL ACCESS TO YOUR HOST FILESYSTEM.
#[builder(setter(into, strip_option))] #[builder(setter(into, strip_option), default)]
pub experimental_privileged_nesting: Option<bool>, pub experimental_privileged_nesting: Option<bool>,
} }
#[derive(Builder, Debug, PartialEq)] #[derive(Builder, Debug, PartialEq)]
pub struct ContainerExportOpts { pub struct ContainerExportOpts {
/// Identifiers for other platform specific containers. /// Identifiers for other platform specific containers.
/// Used for multi-platform image. /// Used for multi-platform image.
#[builder(setter(into, strip_option))] #[builder(setter(into, strip_option), default)]
pub platform_variants: Option<Vec<ContainerId>>, pub platform_variants: Option<Vec<ContainerId>>,
} }
#[derive(Builder, Debug, PartialEq)] #[derive(Builder, Debug, PartialEq)]
pub struct ContainerPipelineOpts<'a> { pub struct ContainerPipelineOpts<'a> {
#[builder(setter(into, strip_option))] #[builder(setter(into, strip_option), default)]
pub description: Option<&'a str>, pub description: Option<&'a str>,
} }
#[derive(Builder, Debug, PartialEq)] #[derive(Builder, Debug, PartialEq)]
pub struct ContainerPublishOpts { pub struct ContainerPublishOpts {
/// Identifiers for other platform specific containers. /// Identifiers for other platform specific containers.
/// Used for multi-platform image. /// Used for multi-platform image.
#[builder(setter(into, strip_option))] #[builder(setter(into, strip_option), default)]
pub platform_variants: Option<Vec<ContainerId>>, pub platform_variants: Option<Vec<ContainerId>>,
} }
#[derive(Builder, Debug, PartialEq)] #[derive(Builder, Debug, PartialEq)]
pub struct ContainerWithDefaultArgsOpts<'a> { pub struct ContainerWithDefaultArgsOpts<'a> {
/// Arguments to prepend to future executions (e.g., ["-v", "--no-cache"]). /// Arguments to prepend to future executions (e.g., ["-v", "--no-cache"]).
#[builder(setter(into, strip_option))] #[builder(setter(into, strip_option), default)]
pub args: Option<Vec<&'a str>>, pub args: Option<Vec<&'a str>>,
} }
#[derive(Builder, Debug, PartialEq)] #[derive(Builder, Debug, PartialEq)]
pub struct ContainerWithDirectoryOpts<'a> { pub struct ContainerWithDirectoryOpts<'a> {
/// Patterns to exclude in the written directory (e.g., ["node_modules/**", ".gitignore", ".git/"]). /// Patterns to exclude in the written directory (e.g., ["node_modules/**", ".gitignore", ".git/"]).
#[builder(setter(into, strip_option))] #[builder(setter(into, strip_option), default)]
pub exclude: Option<Vec<&'a str>>, pub exclude: Option<Vec<&'a str>>,
/// Patterns to include in the written directory (e.g., ["*.go", "go.mod", "go.sum"]). /// Patterns to include in the written directory (e.g., ["*.go", "go.mod", "go.sum"]).
#[builder(setter(into, strip_option))] #[builder(setter(into, strip_option), default)]
pub include: Option<Vec<&'a str>>, pub include: Option<Vec<&'a str>>,
} }
#[derive(Builder, Debug, PartialEq)] #[derive(Builder, Debug, PartialEq)]
pub struct ContainerWithExecOpts<'a> { pub struct ContainerWithExecOpts<'a> {
/// Content to write to the command's standard input before closing (e.g., "Hello world"). /// Content to write to the command's standard input before closing (e.g., "Hello world").
#[builder(setter(into, strip_option))] #[builder(setter(into, strip_option), default)]
pub stdin: Option<&'a str>, pub stdin: Option<&'a str>,
/// Redirect the command's standard output to a file in the container (e.g., "/tmp/stdout"). /// Redirect the command's standard output to a file in the container (e.g., "/tmp/stdout").
#[builder(setter(into, strip_option))] #[builder(setter(into, strip_option), default)]
pub redirect_stdout: Option<&'a str>, pub redirect_stdout: Option<&'a str>,
/// Redirect the command's standard error to a file in the container (e.g., "/tmp/stderr"). /// Redirect the command's standard error to a file in the container (e.g., "/tmp/stderr").
#[builder(setter(into, strip_option))] #[builder(setter(into, strip_option), default)]
pub redirect_stderr: Option<&'a str>, pub redirect_stderr: Option<&'a str>,
/// Provides dagger access to the executed command. /// Provides dagger access to the executed command.
/// Do not use this option unless you trust the command being executed. /// Do not use this option unless you trust the command being executed.
/// The command being executed WILL BE GRANTED FULL ACCESS TO YOUR HOST FILESYSTEM. /// The command being executed WILL BE GRANTED FULL ACCESS TO YOUR HOST FILESYSTEM.
#[builder(setter(into, strip_option))] #[builder(setter(into, strip_option), default)]
pub experimental_privileged_nesting: Option<bool>, pub experimental_privileged_nesting: Option<bool>,
} }
#[derive(Builder, Debug, PartialEq)] #[derive(Builder, Debug, PartialEq)]
pub struct ContainerWithExposedPortOpts<'a> { pub struct ContainerWithExposedPortOpts<'a> {
/// Transport layer network protocol /// Transport layer network protocol
#[builder(setter(into, strip_option))] #[builder(setter(into, strip_option), default)]
pub protocol: Option<NetworkProtocol>, pub protocol: Option<NetworkProtocol>,
/// Optional port description /// Optional port description
#[builder(setter(into, strip_option))] #[builder(setter(into, strip_option), default)]
pub description: Option<&'a str>, pub description: Option<&'a str>,
} }
#[derive(Builder, Debug, PartialEq)] #[derive(Builder, Debug, PartialEq)]
pub struct ContainerWithFileOpts { pub struct ContainerWithFileOpts {
/// Permission given to the copied file (e.g., 0600). /// Permission given to the copied file (e.g., 0600).
/// Default: 0644. /// Default: 0644.
#[builder(setter(into, strip_option))] #[builder(setter(into, strip_option), default)]
pub permissions: Option<isize>, pub permissions: Option<isize>,
} }
#[derive(Builder, Debug, PartialEq)] #[derive(Builder, Debug, PartialEq)]
pub struct ContainerWithMountedCacheOpts { pub struct ContainerWithMountedCacheOpts {
/// Identifier of the directory to use as the cache volume's root. /// Identifier of the directory to use as the cache volume's root.
#[builder(setter(into, strip_option))] #[builder(setter(into, strip_option), default)]
pub source: Option<DirectoryId>, pub source: Option<DirectoryId>,
/// Sharing mode of the cache volume. /// Sharing mode of the cache volume.
#[builder(setter(into, strip_option))] #[builder(setter(into, strip_option), default)]
pub sharing: Option<CacheSharingMode>, pub sharing: Option<CacheSharingMode>,
} }
#[derive(Builder, Debug, PartialEq)] #[derive(Builder, Debug, PartialEq)]
pub struct ContainerWithNewFileOpts<'a> { pub struct ContainerWithNewFileOpts<'a> {
/// Content of the file to write (e.g., "Hello world!"). /// Content of the file to write (e.g., "Hello world!").
#[builder(setter(into, strip_option))] #[builder(setter(into, strip_option), default)]
pub contents: Option<&'a str>, pub contents: Option<&'a str>,
/// Permission given to the written file (e.g., 0600). /// Permission given to the written file (e.g., 0600).
/// Default: 0644. /// Default: 0644.
#[builder(setter(into, strip_option))] #[builder(setter(into, strip_option), default)]
pub permissions: Option<isize>, pub permissions: Option<isize>,
} }
#[derive(Builder, Debug, PartialEq)] #[derive(Builder, Debug, PartialEq)]
pub struct ContainerWithoutExposedPortOpts { pub struct ContainerWithoutExposedPortOpts {
/// Port protocol to unexpose /// Port protocol to unexpose
#[builder(setter(into, strip_option))] #[builder(setter(into, strip_option), default)]
pub protocol: Option<NetworkProtocol>, pub protocol: Option<NetworkProtocol>,
} }
@ -1371,57 +1371,57 @@ pub struct Directory {
pub struct DirectoryDockerBuildOpts<'a> { pub struct DirectoryDockerBuildOpts<'a> {
/// Path to the Dockerfile to use (e.g., "frontend.Dockerfile"). /// Path to the Dockerfile to use (e.g., "frontend.Dockerfile").
/// Defaults: './Dockerfile'. /// Defaults: './Dockerfile'.
#[builder(setter(into, strip_option))] #[builder(setter(into, strip_option), default)]
pub dockerfile: Option<&'a str>, pub dockerfile: Option<&'a str>,
/// The platform to build. /// The platform to build.
#[builder(setter(into, strip_option))] #[builder(setter(into, strip_option), default)]
pub platform: Option<Platform>, pub platform: Option<Platform>,
/// Build arguments to use in the build. /// Build arguments to use in the build.
#[builder(setter(into, strip_option))] #[builder(setter(into, strip_option), default)]
pub build_args: Option<Vec<BuildArg>>, pub build_args: Option<Vec<BuildArg>>,
/// Target build stage to build. /// Target build stage to build.
#[builder(setter(into, strip_option))] #[builder(setter(into, strip_option), default)]
pub target: Option<&'a str>, pub target: Option<&'a str>,
} }
#[derive(Builder, Debug, PartialEq)] #[derive(Builder, Debug, PartialEq)]
pub struct DirectoryEntriesOpts<'a> { pub struct DirectoryEntriesOpts<'a> {
/// Location of the directory to look at (e.g., "/src"). /// Location of the directory to look at (e.g., "/src").
#[builder(setter(into, strip_option))] #[builder(setter(into, strip_option), default)]
pub path: Option<&'a str>, pub path: Option<&'a str>,
} }
#[derive(Builder, Debug, PartialEq)] #[derive(Builder, Debug, PartialEq)]
pub struct DirectoryPipelineOpts<'a> { pub struct DirectoryPipelineOpts<'a> {
#[builder(setter(into, strip_option))] #[builder(setter(into, strip_option), default)]
pub description: Option<&'a str>, pub description: Option<&'a str>,
} }
#[derive(Builder, Debug, PartialEq)] #[derive(Builder, Debug, PartialEq)]
pub struct DirectoryWithDirectoryOpts<'a> { pub struct DirectoryWithDirectoryOpts<'a> {
/// Exclude artifacts that match the given pattern (e.g., ["node_modules/", ".git*"]). /// Exclude artifacts that match the given pattern (e.g., ["node_modules/", ".git*"]).
#[builder(setter(into, strip_option))] #[builder(setter(into, strip_option), default)]
pub exclude: Option<Vec<&'a str>>, pub exclude: Option<Vec<&'a str>>,
/// Include only artifacts that match the given pattern (e.g., ["app/", "package.*"]). /// Include only artifacts that match the given pattern (e.g., ["app/", "package.*"]).
#[builder(setter(into, strip_option))] #[builder(setter(into, strip_option), default)]
pub include: Option<Vec<&'a str>>, pub include: Option<Vec<&'a str>>,
} }
#[derive(Builder, Debug, PartialEq)] #[derive(Builder, Debug, PartialEq)]
pub struct DirectoryWithFileOpts { pub struct DirectoryWithFileOpts {
/// Permission given to the copied file (e.g., 0600). /// Permission given to the copied file (e.g., 0600).
/// Default: 0644. /// Default: 0644.
#[builder(setter(into, strip_option))] #[builder(setter(into, strip_option), default)]
pub permissions: Option<isize>, pub permissions: Option<isize>,
} }
#[derive(Builder, Debug, PartialEq)] #[derive(Builder, Debug, PartialEq)]
pub struct DirectoryWithNewDirectoryOpts { pub struct DirectoryWithNewDirectoryOpts {
/// Permission granted to the created directory (e.g., 0777). /// Permission granted to the created directory (e.g., 0777).
/// Default: 0755. /// Default: 0755.
#[builder(setter(into, strip_option))] #[builder(setter(into, strip_option), default)]
pub permissions: Option<isize>, pub permissions: Option<isize>,
} }
#[derive(Builder, Debug, PartialEq)] #[derive(Builder, Debug, PartialEq)]
pub struct DirectoryWithNewFileOpts { pub struct DirectoryWithNewFileOpts {
/// Permission given to the copied file (e.g., 0600). /// Permission given to the copied file (e.g., 0600).
/// Default: 0644. /// Default: 0644.
#[builder(setter(into, strip_option))] #[builder(setter(into, strip_option), default)]
pub permissions: Option<isize>, pub permissions: Option<isize>,
} }
@ -1948,9 +1948,9 @@ pub struct GitRef {
#[derive(Builder, Debug, PartialEq)] #[derive(Builder, Debug, PartialEq)]
pub struct GitRefTreeOpts<'a> { pub struct GitRefTreeOpts<'a> {
#[builder(setter(into, strip_option))] #[builder(setter(into, strip_option), default)]
pub ssh_known_hosts: Option<&'a str>, pub ssh_known_hosts: Option<&'a str>,
#[builder(setter(into, strip_option))] #[builder(setter(into, strip_option), default)]
pub ssh_auth_socket: Option<SocketId>, pub ssh_auth_socket: Option<SocketId>,
} }
@ -2077,19 +2077,19 @@ pub struct Host {
#[derive(Builder, Debug, PartialEq)] #[derive(Builder, Debug, PartialEq)]
pub struct HostDirectoryOpts<'a> { pub struct HostDirectoryOpts<'a> {
/// Exclude artifacts that match the given pattern (e.g., ["node_modules/", ".git*"]). /// Exclude artifacts that match the given pattern (e.g., ["node_modules/", ".git*"]).
#[builder(setter(into, strip_option))] #[builder(setter(into, strip_option), default)]
pub exclude: Option<Vec<&'a str>>, pub exclude: Option<Vec<&'a str>>,
/// Include only artifacts that match the given pattern (e.g., ["app/", "package.*"]). /// Include only artifacts that match the given pattern (e.g., ["app/", "package.*"]).
#[builder(setter(into, strip_option))] #[builder(setter(into, strip_option), default)]
pub include: Option<Vec<&'a str>>, pub include: Option<Vec<&'a str>>,
} }
#[derive(Builder, Debug, PartialEq)] #[derive(Builder, Debug, PartialEq)]
pub struct HostWorkdirOpts<'a> { pub struct HostWorkdirOpts<'a> {
/// Exclude artifacts that match the given pattern (e.g., ["node_modules/", ".git*"]). /// Exclude artifacts that match the given pattern (e.g., ["node_modules/", ".git*"]).
#[builder(setter(into, strip_option))] #[builder(setter(into, strip_option), default)]
pub exclude: Option<Vec<&'a str>>, pub exclude: Option<Vec<&'a str>>,
/// Include only artifacts that match the given pattern (e.g., ["app/", "package.*"]). /// Include only artifacts that match the given pattern (e.g., ["app/", "package.*"]).
#[builder(setter(into, strip_option))] #[builder(setter(into, strip_option), default)]
pub include: Option<Vec<&'a str>>, pub include: Option<Vec<&'a str>>,
} }
@ -2343,39 +2343,39 @@ pub struct Query {
#[derive(Builder, Debug, PartialEq)] #[derive(Builder, Debug, PartialEq)]
pub struct QueryContainerOpts { pub struct QueryContainerOpts {
#[builder(setter(into, strip_option))] #[builder(setter(into, strip_option), default)]
pub id: Option<ContainerId>, pub id: Option<ContainerId>,
#[builder(setter(into, strip_option))] #[builder(setter(into, strip_option), default)]
pub platform: Option<Platform>, pub platform: Option<Platform>,
} }
#[derive(Builder, Debug, PartialEq)] #[derive(Builder, Debug, PartialEq)]
pub struct QueryDirectoryOpts { pub struct QueryDirectoryOpts {
#[builder(setter(into, strip_option))] #[builder(setter(into, strip_option), default)]
pub id: Option<DirectoryId>, pub id: Option<DirectoryId>,
} }
#[derive(Builder, Debug, PartialEq)] #[derive(Builder, Debug, PartialEq)]
pub struct QueryGitOpts { pub struct QueryGitOpts {
/// Set to true to keep .git directory. /// Set to true to keep .git directory.
#[builder(setter(into, strip_option))] #[builder(setter(into, strip_option), default)]
pub keep_git_dir: Option<bool>, pub keep_git_dir: Option<bool>,
/// A service which must be started before the repo is fetched. /// A service which must be started before the repo is fetched.
#[builder(setter(into, strip_option))] #[builder(setter(into, strip_option), default)]
pub experimental_service_host: Option<ContainerId>, pub experimental_service_host: Option<ContainerId>,
} }
#[derive(Builder, Debug, PartialEq)] #[derive(Builder, Debug, PartialEq)]
pub struct QueryHttpOpts { pub struct QueryHttpOpts {
/// A service which must be started before the URL is fetched. /// A service which must be started before the URL is fetched.
#[builder(setter(into, strip_option))] #[builder(setter(into, strip_option), default)]
pub experimental_service_host: Option<ContainerId>, pub experimental_service_host: Option<ContainerId>,
} }
#[derive(Builder, Debug, PartialEq)] #[derive(Builder, Debug, PartialEq)]
pub struct QueryPipelineOpts<'a> { pub struct QueryPipelineOpts<'a> {
#[builder(setter(into, strip_option))] #[builder(setter(into, strip_option), default)]
pub description: Option<&'a str>, pub description: Option<&'a str>,
} }
#[derive(Builder, Debug, PartialEq)] #[derive(Builder, Debug, PartialEq)]
pub struct QuerySocketOpts { pub struct QuerySocketOpts {
#[builder(setter(into, strip_option))] #[builder(setter(into, strip_option), default)]
pub id: Option<SocketId>, pub id: Option<SocketId>,
} }
@ -2720,6 +2720,6 @@ pub enum CacheSharingMode {
} }
#[derive(Serialize, Deserialize, Clone, PartialEq, Debug)] #[derive(Serialize, Deserialize, Clone, PartialEq, Debug)]
pub enum NetworkProtocol { pub enum NetworkProtocol {
TCP,
UDP, UDP,
TCP,
} }