feat(sdk): move to &str instead of String and introduce builder.

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
This commit is contained in:
2023-02-19 17:21:40 +01:00
committed by Kasper Juul Hermansen
parent 6e2292cf11
commit 94336d0637
18 changed files with 505 additions and 304 deletions

View File

@@ -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;
@@ -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() {
@@ -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),
@@ -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;
}

View File

@@ -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
}

View File

@@ -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;
@@ -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));
})

View File

@@ -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))
}

View File

@@ -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()) ))
}
@@ -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_)),
}
}

View File

@@ -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
}
})
@@ -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_))>,
}
});

View File

@@ -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);
})
}