Skip to content
This repository has been archived by the owner on Jun 29, 2023. It is now read-only.

Commit

Permalink
feat(sdk): move to &str instead of String and introduce builder.
Browse files Browse the repository at this point in the history
This will make the api much easier to use, as we can now rely on ""
instead of "".into() for normal string values.

Introduced builder as well, which makes it much easier to use *Opts, as
it can handle the building of that, and get the benefits from String ->
&str, as that is currently not allowed for optional values
  • Loading branch information
kjuulh committed Feb 19, 2023
1 parent 6e2292c commit 94336d0
Show file tree
Hide file tree
Showing 18 changed files with 509 additions and 308 deletions.
73 changes: 73 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 23 additions & 11 deletions crates/dagger-codegen/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use eyre::ContextCompat;
use crate::utility::OptionExt;

pub trait FormatTypeFuncs {
fn format_kind_list(&self, representation: &str) -> String;
fn format_kind_scalar_string(&self, representation: &str) -> String;
fn format_kind_list(&self, representation: &str, input: bool, immutable: bool) -> String;
fn format_kind_scalar_string(&self, representation: &str, input: bool) -> String;
fn format_kind_scalar_int(&self, representation: &str) -> String;
fn format_kind_scalar_float(&self, representation: &str) -> String;
fn format_kind_scalar_boolean(&self, representation: &str) -> String;
Expand Down Expand Up @@ -36,14 +36,18 @@ impl CommonFunctions {
}

pub fn format_input_type(&self, t: &TypeRef) -> String {
self.format_type(t, true)
self.format_type(t, true, false)
}

pub fn format_output_type(&self, t: &TypeRef) -> String {
self.format_type(t, false)
self.format_type(t, false, false)
}

fn format_type(&self, t: &TypeRef, input: bool) -> String {
pub fn format_immutable_input_type(&self, t: &TypeRef) -> String {
self.format_type(t, true, true)
}

fn format_type(&self, t: &TypeRef, input: bool, immutable: bool) -> String {
let mut representation = String::new();
let mut r = Some(t.clone());
while r.is_some() {
Expand All @@ -57,9 +61,14 @@ impl CommonFunctions {
Scalar::Float => self
.format_type_funcs
.format_kind_scalar_float(&mut representation),
Scalar::String => self
.format_type_funcs
.format_kind_scalar_string(&mut representation),
Scalar::String => {
if immutable {
"&'a str".into()
} else {
self.format_type_funcs
.format_kind_scalar_string(&mut representation, input)
}
}
Scalar::Boolean => self
.format_type_funcs
.format_kind_scalar_boolean(&mut representation),
Expand Down Expand Up @@ -87,12 +96,15 @@ impl CommonFunctions {
.as_ref()
.map(|t| t.clone())
.map(|t| *t)
.map(|t| self.format_type(&t, input))
.map(|t| self.format_type(&t, input, immutable))
.context("could not get inner type of list")
.unwrap();

representation =
self.format_type_funcs.format_kind_list(&mut inner_type);
representation = self.format_type_funcs.format_kind_list(
&mut inner_type,
input,
immutable,
);

return representation;
}
Expand Down
10 changes: 7 additions & 3 deletions crates/dagger-codegen/src/rust/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@ use super::functions::format_name;
pub struct FormatTypeFunc;

impl FormatTypeFuncs for FormatTypeFunc {
fn format_kind_list(&self, representation: &str) -> String {
fn format_kind_list(&self, representation: &str, _input: bool, _immutable: bool) -> String {
format!("Vec<{}>", representation)
}

fn format_kind_scalar_string(&self, representation: &str) -> String {
fn format_kind_scalar_string(&self, representation: &str, input: bool) -> String {
let mut rep = representation.to_string();
rep.push_str("String");
if input {
rep.push_str("impl Into<String>");
} else {
rep.push_str("String");
}
rep
}

Expand Down
38 changes: 36 additions & 2 deletions crates/dagger-codegen/src/rust/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use genco::quote;
use genco::tokens::quoted;

use crate::functions::{
type_field_has_optional, type_ref_is_list_of_objects, type_ref_is_object, type_ref_is_optional,
CommonFunctions,
type_field_has_optional, type_ref_is_list, type_ref_is_list_of_objects, type_ref_is_object,
type_ref_is_optional, type_ref_is_scalar, CommonFunctions, Scalar,
};
use crate::utility::OptionExt;

Expand Down Expand Up @@ -66,6 +66,40 @@ fn render_required_args(_funcs: &CommonFunctions, field: &FullTypeFields) -> Opt
let n = format_struct_name(&s.input_value.name);
let name = &s.input_value.name;

if type_ref_is_scalar(&s.input_value.type_) {
if let Scalar::String =
Scalar::from(&*s.input_value.type_.of_type.as_ref().unwrap().clone())
{
return Some(quote! {
query = query.arg($(quoted(name)), $(&n).into());
});
}
}

if type_ref_is_list(&s.input_value.type_) {
let inner = *s
.input_value
.type_
.of_type
.as_ref()
.unwrap()
.clone()
.of_type
.as_ref()
.unwrap()
.clone();
println!("type: {:?}", inner);
if type_ref_is_scalar(&inner) {
if let Scalar::String =
Scalar::from(&*inner.of_type.as_ref().unwrap().clone())
{
return Some(quote! {
query = query.arg($(quoted(name)), $(&n).into_iter().map(|i| i.into()).collect::<Vec<String>>());
});
}
}
}

Some(quote! {
query = query.arg($(quoted(name)), $(n));
})
Expand Down
2 changes: 1 addition & 1 deletion crates/dagger-codegen/src/rust/templates/enum_tmpl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub fn render_enum(t: &FullType) -> eyre::Result<rust::Tokens> {
let serialize = rust::import("serde", "Serialize");

Ok(quote! {
#[derive($serialize)]
#[derive($serialize, Clone, PartialEq, Debug)]
pub enum $(t.name.as_ref()) {
$(render_enum_values(t))
}
Expand Down
4 changes: 2 additions & 2 deletions crates/dagger-codegen/src/rust/templates/input_tmpl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub fn render_input(funcs: &CommonFunctions, t: &FullType) -> eyre::Result<rust:
let deserialize = rust::import("serde", "Deserialize");
let serialize = rust::import("serde", "Serialize");
Ok(quote! {
#[derive($serialize, $deserialize)]
#[derive($serialize, $deserialize, Debug, PartialEq, Clone)]
pub struct $(format_name(t.name.as_ref().unwrap())) {
$(render_input_fields(funcs, t.input_fields.as_ref().unwrap_or(&Vec::new()) ))
}
Expand All @@ -33,6 +33,6 @@ pub fn render_input_fields(

pub fn render_input_field(funcs: &CommonFunctions, field: &FullTypeInputFields) -> rust::Tokens {
quote! {
pub $(format_struct_name(&field.input_value.name)): $(funcs.format_input_type(&field.input_value.type_)),
pub $(format_struct_name(&field.input_value.name)): $(funcs.format_output_type(&field.input_value.type_)),
}
}
11 changes: 9 additions & 2 deletions crates/dagger-codegen/src/rust/templates/object_tmpl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,15 @@ fn render_optional_arg(funcs: &CommonFunctions, field: &FullTypeFields) -> Optio
.pipe(|t| render_optional_field_args(funcs, t))
.flatten();

let builder = rust::import("derive_builder", "Builder");
let phantom_data = rust::import("std::marker", "PhantomData");

if let Some(fields) = fields {
Some(quote! {
pub struct $output_type {
#[derive($builder, Debug, PartialEq)]
pub struct $output_type<'a> {
//#[builder(default, setter(skip))]
//pub marker: $(phantom_data)<&'a ()>,
$fields
}
})
Expand All @@ -81,7 +87,8 @@ fn render_optional_field_args(
}
let rendered_args = args.into_iter().map(|a| &a.input_value).map(|a| {
quote! {
pub $(format_struct_name(&a.name)): Option<$(funcs.format_output_type(&a.type_))>,
#[builder(setter(into, strip_option))]
pub $(format_struct_name(&a.name)): Option<$(funcs.format_immutable_input_type(&a.type_))>,
}
});

Expand Down
2 changes: 1 addition & 1 deletion crates/dagger-codegen/src/rust/templates/scalar_tmpl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub fn render_scalar(t: &FullType) -> eyre::Result<rust::Tokens> {
let serialize = rust::import("serde", "Serialize");

Ok(quote! {
#[derive($serialize, $deserialize)]
#[derive($serialize, $deserialize, PartialEq, Debug, Clone)]
pub struct $(t.name.pipe(|n|format_name(n)))(String);
})
}
1 change: 1 addition & 0 deletions crates/dagger-sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ gql_client = "1.0.7"
serde = { version = "1.0.152", features = ["derive"] }
serde_json = "1.0.93"
tokio = { version = "1.25.0", features = ["full"] }
derive_builder = "0.12.0"

[dev-dependencies]
pretty_assertions = "1.3.0"
Expand Down
29 changes: 11 additions & 18 deletions crates/dagger-sdk/examples/build-the-application/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,30 @@ fn main() -> eyre::Result<()> {
let client = dagger_sdk::connect()?;

let host_source_dir = client.host().directory(
"examples/build-the-application/app".into(),
"examples/build-the-application/app",
Some(HostDirectoryOpts {
exclude: Some(vec!["node_modules".into(), "ci/".into()]),
include: None,
marker: std::marker::PhantomData,
}),
);

let source = client
.container(None)
.from("node:16".into())
.with_mounted_directory("/src".into(), host_source_dir.id()?);
.from("node:16")
.with_mounted_directory("/src", host_source_dir.id()?);

let runner = source
.with_workdir("/src".into())
.with_exec(vec!["npm".into(), "install".into()], None);

let test = runner.with_exec(
vec![
"npm".into(),
"test".into(),
"--".into(),
"--watchAll=false".into(),
],
None,
);
.with_workdir("/src")
.with_exec(vec!["npm", "install"], None);

let test = runner.with_exec(vec!["npm", "test", "--", "--watchAll=false"], None);

let build_dir = test
.with_exec(vec!["npm".into(), "run".into(), "build".into()], None)
.directory("./build".into());
.with_exec(vec!["npm", "run", "build"], None)
.directory("./build");

let _ = build_dir.export("./build".into());
let _ = build_dir.export("./build");

let entries = build_dir.entries(None);

Expand Down
Loading

0 comments on commit 94336d0

Please sign in to comment.