fix arguments

This commit is contained in:
2023-02-16 21:47:28 +01:00
parent e0db105cba
commit e04d12fa05
6 changed files with 538 additions and 271 deletions

View File

@@ -1,6 +1,6 @@
use std::sync::Arc;
use dagger_core::introspection::{FullType, InputValue, TypeRef, __TypeKind};
use dagger_core::introspection::{FullType, FullTypeFields, InputValue, TypeRef, __TypeKind};
use eyre::ContextCompat;
use crate::utility::OptionExt;
@@ -154,6 +154,19 @@ pub fn type_ref_is_optional(type_ref: &TypeRef) -> bool {
.unwrap_or(false)
}
pub fn type_field_has_optional(field: &FullTypeFields) -> bool {
field
.args
.pipe(|a| {
a.iter()
.map(|a| a.pipe(|a| &a.input_value))
.flatten()
.collect::<Vec<_>>()
})
.pipe(|s| input_values_has_optionals(s.as_slice()))
.unwrap_or(false)
}
pub fn type_ref_is_scalar(type_ref: &TypeRef) -> bool {
type_ref
.kind
@@ -175,7 +188,7 @@ pub fn type_ref_is_list(type_ref: &TypeRef) -> bool {
.unwrap_or(false)
}
pub fn input_values_has_optionals(input_values: &[InputValue]) -> bool {
pub fn input_values_has_optionals(input_values: &[&InputValue]) -> bool {
input_values
.into_iter()
.map(|k| type_ref_is_optional(&k.type_))
@@ -324,7 +337,7 @@ mod test {
},
];
let output = input_values_has_optionals(&input);
let output = input_values_has_optionals(input.iter().collect::<Vec<_>>().as_slice());
assert_eq!(output, true);
}
@@ -354,7 +367,7 @@ mod test {
},
];
let output = input_values_has_optionals(&input);
let output = input_values_has_optionals(input.iter().collect::<Vec<_>>().as_slice());
assert_eq!(output, false);
}

View File

@@ -40,7 +40,7 @@ impl FormatTypeFuncs for FormatTypeFunc {
input: bool,
) -> String {
let mut rep = representation.to_string();
rep.push_str(ref_name);
rep.push_str(&format_name(ref_name));
rep
}
@@ -58,7 +58,7 @@ impl FormatTypeFuncs for FormatTypeFunc {
fn format_kind_enum(&self, representation: &str, ref_name: &str) -> String {
let mut rep = representation.to_string();
rep.push_str(ref_name);
rep.push_str(&format_name(ref_name));
rep
}
}

View File

@@ -1,5 +1,12 @@
use convert_case::{Case, Casing};
use dagger_core::introspection::FullTypeFields;
use genco::prelude::rust;
use genco::quote;
use crate::functions::{
input_values_has_optionals, type_field_has_optional, type_ref_is_optional, CommonFunctions,
};
use crate::utility::OptionExt;
pub fn format_name(s: &str) -> String {
s.to_case(Case::Pascal)
@@ -18,3 +25,61 @@ pub fn field_options_struct_name(field: &FullTypeFields) -> Option<String> {
.zip(field.name.as_ref().map(|n| format_name(n)))
.map(|(parent_name, field_name)| format!("{parent_name}{field_name}Opts"))
}
pub fn format_function(funcs: &CommonFunctions, field: &FullTypeFields) -> Option<rust::Tokens> {
let signature = quote! {
pub fn $(field.name.pipe(|n | format_struct_name(n)))
};
let args = format_function_args(funcs, field);
let output_type = field
.type_
.pipe(|t| &t.type_ref)
.pipe(|t| funcs.format_output_type(t));
Some(quote! {
$(signature)(
$(args)
) -> $(output_type) {
todo!()
}
})
}
fn format_function_args(funcs: &CommonFunctions, field: &FullTypeFields) -> Option<rust::Tokens> {
if let Some(args) = field.args.as_ref() {
let args = args
.into_iter()
.map(|a| {
a.as_ref().and_then(|s| {
if type_ref_is_optional(&s.input_value.type_) {
return None;
}
let t = funcs.format_input_type(&s.input_value.type_);
let n = format_struct_name(&s.input_value.name);
Some(quote! {
$(n): $(t),
})
})
})
.flatten()
.collect::<Vec<_>>();
let required_args = quote! {
&self,
$(for arg in args join ($['\r']) => $arg)
};
if type_field_has_optional(field) {
Some(quote! {
$(required_args)
opts: Option<$(field_options_struct_name(field))>
})
} else {
Some(required_args)
}
} else {
None
}
}

View File

@@ -2,8 +2,12 @@ use dagger_core::introspection::{FullType, FullTypeFields, FullTypeFieldsArgs};
use genco::prelude::rust;
use genco::quote;
use crate::functions::CommonFunctions;
use crate::rust::functions::{field_options_struct_name, format_name, format_struct_name};
use crate::functions::{
input_values_has_optionals, type_field_has_optional, type_ref_is_optional, CommonFunctions,
};
use crate::rust::functions::{
field_options_struct_name, format_function, format_name, format_struct_name,
};
use crate::utility::OptionExt;
pub fn render_object(funcs: &CommonFunctions, t: &FullType) -> eyre::Result<rust::Tokens> {
@@ -15,7 +19,7 @@ pub fn render_object(funcs: &CommonFunctions, t: &FullType) -> eyre::Result<rust
$(t.fields.pipe(|f| render_optional_args(funcs, f)))
impl $(t.name.pipe(|s| format_name(s))) {
$(t.fields.pipe(|f| render_functions(funcs, f)))
}
})
}
@@ -44,6 +48,11 @@ fn render_optional_arg(funcs: &CommonFunctions, field: &FullTypeFields) -> Optio
let fields = field
.args
.pipe(|t| t.into_iter().flatten().collect::<Vec<_>>())
.map(|t| {
t.into_iter()
.filter(|t| type_ref_is_optional(&t.input_value.type_))
.collect::<Vec<_>>()
})
.pipe(|t| render_optional_field_args(funcs, t))
.flatten();
@@ -67,7 +76,7 @@ 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)): $(funcs.format_output_type(&a.type_)),
pub $(format_struct_name(&a.name)): Option<$(funcs.format_output_type(&a.type_))>,
}
});
@@ -75,3 +84,24 @@ fn render_optional_field_args(
$(for arg in rendered_args join ($['\r']) => $arg)
})
}
fn render_functions(funcs: &CommonFunctions, fields: &Vec<FullTypeFields>) -> Option<rust::Tokens> {
let rendered_functions = fields
.iter()
.map(|f| render_function(funcs, f))
.collect::<Vec<_>>();
if rendered_functions.len() > 0 {
Some(quote! {
$(for func in rendered_functions join ($['\r']) => $func)
})
} else {
None
}
}
fn render_function(funcs: &CommonFunctions, field: &FullTypeFields) -> Option<rust::Tokens> {
Some(quote! {
$(format_function(funcs, field))
})
}

View File

@@ -3,9 +3,10 @@ use genco::prelude::rust;
use genco::quote;
use crate::rust::functions::format_name;
use crate::utility::OptionExt;
pub fn render_scalar(t: &FullType) -> eyre::Result<rust::Tokens> {
Ok(quote! {
pub struct $(format_name(&t.name.as_ref().unwrap()))(String);
pub struct $(t.name.pipe(|n|format_name(n)))(String);
})
}