Compare commits

...

3 Commits

Author SHA1 Message Date
58de5386e2 add implementation 2023-02-11 16:31:20 +01:00
3453e8cac6 with object gen and args 2023-02-11 16:17:04 +01:00
d0483b0434 format code 2023-02-11 15:51:26 +01:00
3 changed files with 2044 additions and 728 deletions

View File

@@ -7,8 +7,11 @@ use super::{
utility::{render_description_from_field, render_description_from_input_value},
};
pub fn render_fields(fields: &Vec<FullTypeFields>) -> eyre::Result<Option<rust::Tokens>> {
pub fn render_fields(
fields: &Vec<FullTypeFields>,
) -> eyre::Result<Option<(rust::Tokens, rust::Tokens)>> {
let mut collected_fields: Vec<rust::Tokens> = vec![];
let mut collected_args: Vec<rust::Tokens> = vec![];
for field in fields.iter() {
let name = field.name.as_ref().map(|n| n.to_case(Case::Snake)).unwrap();
let output = render_field_output(field)?;
@@ -18,22 +21,23 @@ pub fn render_fields(fields: &Vec<FullTypeFields>) -> eyre::Result<Option<rust::
None => None,
};
let mut tkns = rust::Tokens::new();
if let Some(args) = args.as_ref() {
tkns.append(quote! {
let mut args_tkns = rust::Tokens::new();
args_tkns.append(quote! {
$description
pub struct $(&name)Args {
pub struct $(&field.name.as_ref().map(|n| n.to_case(Case::Pascal)).unwrap())Args {
$(&args.args)
}
});
tkns.push();
}
args_tkns.push();
collected_args.push(args_tkns);
}
let mut tkns = rust::Tokens::new();
tkns.append(quote! {
pub fn $(&name)(
&self,
$(if let Some(_) = args.as_ref() => args: $(&name)Args)
$(if let Some(_) = args.as_ref() => args: &$(&field.name.as_ref().map(|n| n.to_case(Case::Pascal)).unwrap())Args)
) -> $(&output) {
let query = self.selection.select($(field.name.as_ref().map(|n| format!("\"{}\"", n))));
$(if let Some(_) = args.as_ref() => query.args(args);)
@@ -43,17 +47,20 @@ pub fn render_fields(fields: &Vec<FullTypeFields>) -> eyre::Result<Option<rust::
proc: self.proc.clone(),
selection: query,
}
todo!()
}
});
collected_fields.push(tkns);
}
Ok(Some(quote! {
$(for field in collected_fields => $field $['\n'] )
}))
Ok(Some((
quote! {
$(for arg in collected_args => $arg $['\n'] )
},
quote! {
$(for field in collected_fields => $field $['\n'] )
},
)))
}
struct Arg {
@@ -91,15 +98,9 @@ fn render_args(args: &[Option<FullTypeFieldsArgs>]) -> Option<CollectedArgs> {
};
for arg in collected_args {
if let Some(desc) = arg.description {
if let Some(inner_desc) = collected_arg.description.as_mut() {
inner_desc.append(desc);
inner_desc.push();
}
}
collected_arg.args.append(quote! {
$(arg.name.to_case(Case::Snake)): $(arg.type_),
$(arg.description)
pub $(arg.name.to_case(Case::Snake)): $(arg.type_),
});
collected_arg.args.push();
}

View File

@@ -29,14 +29,24 @@ impl Handler for Object {
None => None,
};
let child = rust::import("std::process", "Child");
let connect_params = rust::import("dagger_core::connect_params", "ConnectParams");
let selection = rust::import("crate::querybuilder", "Selection");
let arc = rust::import("std::sync", "Arc");
let out = quote! {
$(if fields.as_ref().is_some() => $(fields.as_ref().map(|f| &f.0)))
$(if description.is_some() => $description)
pub struct $name {
$(if input_fields.is_some() => $input_fields)
pub conn: $connect_params,
pub proc: $arc<$child>,
pub selection: $selection,
}
impl $name {
$(if fields.is_some() => $fields)
$(if fields.is_some() => $(fields.map(|f| f.1)))
}
};
@@ -85,9 +95,18 @@ mod tests {
enum_values: None,
possible_types: None,
};
let expected = r#"
let expected = r#"use crate::querybuilder::Selection;
use dagger_core::connect_params::ConnectParams;
use std::process::Child;
use std::sync::Arc;
/// A directory whose contents persists across sessions
pub struct CacheVolume {}
pub struct CacheVolume {
pub conn: ConnectParams,
pub proc: Arc<Child>,
pub selection: Selection,
}
impl CacheVolume {
pub fn id(
@@ -114,27 +133,40 @@ impl CacheVolume {
#[test]
fn can_render_query_container() {
let description = "Loads a container from ID.\nNull ID returns an empty container (scratch).\nOptional platform argument initializes new containers to execute and publish as that platform. Platform defaults to that of the builder's host.".into();
let t: FullType = FullType {
kind: Some(__TypeKind::OBJECT),
name: Some("Query".into()),
description: None,
fields: Some(vec![FullTypeFields {
name: Some("container".into()),
description: Some("Loads a container from ID.\nNull ID returns an empty container (scratch).\nOptional platform argument initializes new containers to execute and publish as that platform. Platform defaults to that of the builder's host.".into()),
args: Some(
vec![
Some(
FullTypeFieldsArgs
{
input_value: InputValue { name: "id".into(), description: None, type_: TypeRef { kind: Some(__TypeKind::SCALAR), name: Some("ContainerID".into()), of_type: None }, default_value: None }
}
),
Some(
FullTypeFieldsArgs {
description: Some(description),
args: Some(vec![
Some(FullTypeFieldsArgs {
input_value: InputValue {
name: "platform".into(), description: None, type_: TypeRef { kind: Some(__TypeKind::SCALAR), name: Some("Platform".into()), of_type: None },
default_value: None }
})
name: "id".into(),
description: None,
type_: TypeRef {
kind: Some(__TypeKind::SCALAR),
name: Some("ContainerID".into()),
of_type: None,
},
default_value: None,
},
}),
Some(FullTypeFieldsArgs {
input_value: InputValue {
name: "platform".into(),
description: None,
type_: TypeRef {
kind: Some(__TypeKind::SCALAR),
name: Some("Platform".into()),
of_type: None,
},
default_value: None,
},
}),
]),
type_: Some(FullTypeFieldsType {
type_ref: TypeRef {
@@ -149,14 +181,49 @@ impl CacheVolume {
}),
is_deprecated: Some(false),
deprecation_reason: None,
}
]),
}]),
input_fields: None,
interfaces: None,
enum_values: None,
possible_types: None,
};
let expected = r#"
let expected = r#"use crate::querybuilder::Selection;
use dagger_core::connect_params::ConnectParams;
use std::process::Child;
use std::sync::Arc;
/// Loads a container from ID.
/// Null ID returns an empty container (scratch).
/// Optional platform argument initializes new containers to execute and publish as that platform. Platform defaults to that of the builder's host.
pub struct ContainerArgs {
pub id: Option<ContainerID>,
pub platform: Option<Platform>,
}
pub struct Query {
pub conn: ConnectParams,
pub proc: Arc<Child>,
pub selection: Selection,
}
impl Query {
pub fn container(
&self,
args: &ContainerArgs
) -> CacheID {
let query = self.selection.select("container");
query.args(args);
CacheID {
conn: self.conn.clone(),
proc: self.proc.clone(),
selection: query,
}
todo!()
}
}
"#;
let handler = Object {};
let obj = handler.render(&t).unwrap();

File diff suppressed because it is too large Load Diff