diff --git a/crates/dagger-codegen/src/handlers/fields.rs b/crates/dagger-codegen/src/handlers/fields.rs index 93fd614..33dc6e6 100644 --- a/crates/dagger-codegen/src/handlers/fields.rs +++ b/crates/dagger-codegen/src/handlers/fields.rs @@ -7,8 +7,11 @@ use super::{ utility::{render_description_from_field, render_description_from_input_value}, }; -pub fn render_fields(fields: &Vec) -> eyre::Result> { +pub fn render_fields( + fields: &Vec, +) -> eyre::Result> { let mut collected_fields: Vec = vec![]; + let mut collected_args: Vec = 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) -> eyre::Result 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);) @@ -51,9 +55,14 @@ pub fn render_fields(fields: &Vec) -> eyre::Result $field $['\n'] ) - })) + Ok(Some(( + quote! { + $(for arg in collected_args => $arg $['\n'] ) + }, + quote! { + $(for field in collected_fields => $field $['\n'] ) + }, + ))) } struct Arg { @@ -91,15 +100,9 @@ fn render_args(args: &[Option]) -> Option { }; 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(); } diff --git a/crates/dagger-codegen/src/handlers/object.rs b/crates/dagger-codegen/src/handlers/object.rs index 9d1d25c..7bb3ee4 100644 --- a/crates/dagger-codegen/src/handlers/object.rs +++ b/crates/dagger-codegen/src/handlers/object.rs @@ -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, + pub selection: Selection, +} impl CacheVolume { pub fn id( @@ -168,7 +187,43 @@ impl CacheVolume { 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, + pub platform: Option, +} + +pub struct Query { + pub conn: ConnectParams, + pub proc: Arc, + 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();