mirror of
https://github.com/kjuulh/dagger-rs.git
synced 2025-07-25 19:09:22 +02:00
add fields
This commit is contained in:
@@ -11,7 +11,6 @@ dagger-core = { path = "../dagger-core" }
|
||||
|
||||
eyre = "0.6.8"
|
||||
genco = "0.17.3"
|
||||
graphql-introspection-query = "0.2.0"
|
||||
itertools = "0.10.5"
|
||||
pretty_assertions = "1.3.0"
|
||||
serde = { version = "1.0.152", features = ["derive"] }
|
||||
|
@@ -3,10 +3,8 @@ use std::{
|
||||
sync::Arc,
|
||||
};
|
||||
|
||||
use dagger_core::introspection::{FullType, IntrospectionResponse, Schema};
|
||||
use genco::{fmt, prelude::rust, prelude::*, quote};
|
||||
use graphql_introspection_query::introspection_response::{
|
||||
FullType, IntrospectionResponse, Schema,
|
||||
};
|
||||
|
||||
use crate::handlers::{
|
||||
enumeration::Enumeration, input::Input, object::Object, scalar::Scalar, DynHandler, Handlers,
|
||||
|
@@ -1,5 +1,5 @@
|
||||
use dagger_core::introspection::FullType;
|
||||
use genco::{prelude::rust, quote};
|
||||
use graphql_introspection_query::introspection_response::FullType;
|
||||
|
||||
use crate::predicates::is_enum_type;
|
||||
|
||||
|
@@ -1,10 +1,10 @@
|
||||
use convert_case::{Case, Casing};
|
||||
use dagger_core::introspection::{FullTypeFields, FullTypeFieldsArgs};
|
||||
use genco::{prelude::rust, quote};
|
||||
use graphql_introspection_query::introspection_response::FullTypeFields;
|
||||
|
||||
use super::{
|
||||
type_ref,
|
||||
utility::{render_description, render_description_from_field},
|
||||
type_ref::{self, render_type_ref},
|
||||
utility::{render_description_from_field, render_description_from_input_value},
|
||||
};
|
||||
|
||||
pub fn render_fields(fields: &Vec<FullTypeFields>) -> eyre::Result<Option<rust::Tokens>> {
|
||||
@@ -13,13 +13,38 @@ pub fn render_fields(fields: &Vec<FullTypeFields>) -> eyre::Result<Option<rust::
|
||||
let name = field.name.as_ref().map(|n| n.to_case(Case::Snake)).unwrap();
|
||||
let output = render_field_output(field)?;
|
||||
let description = render_description_from_field(field);
|
||||
let args = match field.args.as_ref() {
|
||||
Some(a) => render_args(a),
|
||||
None => None,
|
||||
};
|
||||
|
||||
collected_fields.push(quote! {
|
||||
$(if description.is_some() => $description)
|
||||
pub fn $name(&self) -> $output {
|
||||
let mut tkns = rust::Tokens::new();
|
||||
if let Some(desc) = &description {
|
||||
tkns.append(desc);
|
||||
tkns.push()
|
||||
}
|
||||
|
||||
if let Some(args) = args.as_ref() {
|
||||
if let Some(desc) = args.description.as_ref() {
|
||||
tkns.append("/// # Arguments");
|
||||
tkns.push();
|
||||
tkns.append("///");
|
||||
tkns.push();
|
||||
tkns.append(desc);
|
||||
tkns.push();
|
||||
}
|
||||
}
|
||||
|
||||
tkns.append(quote! {
|
||||
pub fn $name(
|
||||
&self,
|
||||
$(if let Some(args) = args.as_ref() => $(&args.args))
|
||||
) -> $output {
|
||||
todo!()
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
collected_fields.push(tkns);
|
||||
}
|
||||
|
||||
Ok(Some(quote! {
|
||||
@@ -27,6 +52,66 @@ pub fn render_fields(fields: &Vec<FullTypeFields>) -> eyre::Result<Option<rust::
|
||||
}))
|
||||
}
|
||||
|
||||
struct Arg {
|
||||
name: String,
|
||||
description: Option<rust::Tokens>,
|
||||
type_: rust::Tokens,
|
||||
}
|
||||
|
||||
struct CollectedArgs {
|
||||
description: Option<rust::Tokens>,
|
||||
args: rust::Tokens,
|
||||
}
|
||||
|
||||
fn render_args(args: &[Option<FullTypeFieldsArgs>]) -> Option<CollectedArgs> {
|
||||
let mut collected_args: Vec<Arg> = vec![];
|
||||
|
||||
for arg in args {
|
||||
if let Some(arg) = arg.as_ref().map(|a| &a.input_value) {
|
||||
let name = arg.name.clone();
|
||||
let description = render_description_from_input_value(&arg, &name);
|
||||
let t = render_type_ref(&arg.type_).unwrap();
|
||||
|
||||
collected_args.push(Arg {
|
||||
name,
|
||||
description,
|
||||
type_: t,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
if collected_args.len() > 0 {
|
||||
let mut collected_arg = CollectedArgs {
|
||||
description: Some(rust::Tokens::new()),
|
||||
args: rust::Tokens::new(),
|
||||
};
|
||||
|
||||
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_),
|
||||
});
|
||||
collected_arg.args.push();
|
||||
}
|
||||
|
||||
if let Some(desc) = collected_arg.description.as_ref() {
|
||||
if desc.is_empty() {
|
||||
collected_arg.description = None;
|
||||
}
|
||||
}
|
||||
|
||||
Some(collected_arg)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub fn render_field_output(field: &FullTypeFields) -> eyre::Result<rust::Tokens> {
|
||||
let inner = &field.type_.as_ref().unwrap();
|
||||
type_ref::render_type_ref(&inner.type_ref)
|
||||
|
@@ -1,6 +1,6 @@
|
||||
use dagger_core::introspection::FullType;
|
||||
use genco::prelude::rust;
|
||||
use genco::prelude::*;
|
||||
use graphql_introspection_query::introspection_response::FullType;
|
||||
|
||||
use crate::predicates::is_input_object_type;
|
||||
|
||||
@@ -42,7 +42,7 @@ impl Handler for Input {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use graphql_introspection_query::introspection_response::{
|
||||
use dagger_core::introspection::{
|
||||
FullType, FullTypeInputFields, InputValue, TypeRef, __TypeKind,
|
||||
};
|
||||
use pretty_assertions::assert_eq;
|
||||
@@ -101,9 +101,9 @@ mod tests {
|
||||
let expected = r#"use dagger_core::Input;
|
||||
|
||||
pub struct BuildArg {
|
||||
pub name: Option<String>
|
||||
pub name: Option<String>,
|
||||
|
||||
pub value: Option<String>
|
||||
pub value: Option<String>,
|
||||
}
|
||||
|
||||
impl Input for BuildArg {}
|
||||
|
@@ -1,5 +1,5 @@
|
||||
use dagger_core::introspection::FullTypeInputFields;
|
||||
use genco::{prelude::rust, quote};
|
||||
use graphql_introspection_query::introspection_response::FullTypeInputFields;
|
||||
|
||||
use super::type_ref;
|
||||
|
||||
|
@@ -9,9 +9,9 @@ mod utility;
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
use dagger_core::introspection::FullType;
|
||||
use genco::prelude::rust::Tokens;
|
||||
use genco::prelude::*;
|
||||
use graphql_introspection_query::introspection_response::FullType;
|
||||
|
||||
pub trait Handler {
|
||||
fn predicate(&self, _t: &FullType) -> bool {
|
||||
@@ -55,7 +55,7 @@ pub type Handlers = Vec<DynHandler>;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use graphql_introspection_query::introspection_response::FullType;
|
||||
use dagger_core::introspection::FullType;
|
||||
use pretty_assertions::assert_eq;
|
||||
|
||||
use super::Handler;
|
||||
|
@@ -1,5 +1,5 @@
|
||||
use dagger_core::introspection::FullType;
|
||||
use genco::{prelude::rust, quote};
|
||||
use graphql_introspection_query::introspection_response::FullType;
|
||||
|
||||
use crate::predicates::is_object_type;
|
||||
|
||||
@@ -50,7 +50,7 @@ impl Handler for Object {
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use graphql_introspection_query::introspection_response::{
|
||||
use dagger_core::introspection::{
|
||||
FullType, FullTypeFields, FullTypeFieldsType, TypeRef, __TypeKind,
|
||||
};
|
||||
use pretty_assertions::assert_eq;
|
||||
@@ -92,8 +92,14 @@ mod tests {
|
||||
|
||||
|
||||
/// A directory whose contents persists across sessions
|
||||
pub struct CacheVolume {
|
||||
pub id: Option<CacheID>
|
||||
pub struct CacheVolume {}
|
||||
|
||||
impl CacheVolume {
|
||||
pub fn id(
|
||||
&self,
|
||||
) -> Option<CacheID> {
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
impl Input for CacheVolume {}
|
||||
|
@@ -1,5 +1,5 @@
|
||||
use dagger_core::introspection::FullType;
|
||||
use genco::{prelude::rust, quote};
|
||||
use graphql_introspection_query::introspection_response::FullType;
|
||||
|
||||
use crate::predicates::is_custom_scalar_type;
|
||||
|
||||
|
@@ -1,6 +1,6 @@
|
||||
use dagger_core::introspection::TypeRef;
|
||||
use genco::prelude::rust;
|
||||
use genco::prelude::*;
|
||||
use graphql_introspection_query::introspection_response::TypeRef;
|
||||
|
||||
use crate::predicates::{
|
||||
is_custom_scalar_type_ref, is_list_type, is_required_type_ref, is_scalar_type_ref,
|
||||
|
@@ -1,5 +1,5 @@
|
||||
use dagger_core::introspection::{FullType, FullTypeFields, InputValue};
|
||||
use genco::{prelude::*, quote};
|
||||
use graphql_introspection_query::introspection_response::{FullType, FullTypeFields};
|
||||
|
||||
pub fn render_description(t: &FullType) -> Option<rust::Tokens> {
|
||||
if let Some(description) = t.description.as_ref() {
|
||||
@@ -26,3 +26,31 @@ pub fn render_description_from_field(t: &FullTypeFields) -> Option<rust::Tokens>
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
pub fn render_description_from_input_value(t: &InputValue, name: &String) -> Option<rust::Tokens> {
|
||||
if let Some(description) = t.description.as_ref() {
|
||||
if description == "" {
|
||||
return None;
|
||||
}
|
||||
let lines = description.split('\n').collect::<Vec<&str>>();
|
||||
let mut output = rust::Tokens::new();
|
||||
|
||||
if let Some(line) = lines.first() {
|
||||
output.append(quote! {
|
||||
$(format!("/// * `{name}` - {line}"))
|
||||
});
|
||||
output.push();
|
||||
}
|
||||
|
||||
for line in lines {
|
||||
output.append(quote! {
|
||||
$(format!("/// {line}"))
|
||||
});
|
||||
output.push();
|
||||
}
|
||||
|
||||
return Some(output);
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
@@ -1,32 +1,30 @@
|
||||
use graphql_introspection_query::introspection_response::{
|
||||
self, FullType, FullTypeInputFields, TypeRef, __TypeKind,
|
||||
};
|
||||
use dagger_core::introspection::{FullType, FullTypeInputFields, TypeRef, __TypeKind};
|
||||
|
||||
use crate::models::Scalars;
|
||||
|
||||
pub fn is_scalar_type(t: &FullType) -> bool {
|
||||
if let Some(introspection_response::__TypeKind::SCALAR) = t.kind {
|
||||
if let Some(__TypeKind::SCALAR) = t.kind {
|
||||
return true;
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
pub fn is_scalar_type_ref(t: &TypeRef) -> bool {
|
||||
if let Some(introspection_response::__TypeKind::SCALAR) = t.kind {
|
||||
if let Some(__TypeKind::SCALAR) = t.kind {
|
||||
return true;
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
pub fn is_enum_type(t: &FullType) -> bool {
|
||||
if let Some(introspection_response::__TypeKind::ENUM) = t.kind {
|
||||
if let Some(__TypeKind::ENUM) = t.kind {
|
||||
return true;
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
pub fn is_input_object_type(t: &FullType) -> bool {
|
||||
if let Some(introspection_response::__TypeKind::INPUT_OBJECT) = t.kind {
|
||||
if let Some(__TypeKind::INPUT_OBJECT) = t.kind {
|
||||
return true;
|
||||
}
|
||||
false
|
||||
@@ -49,14 +47,14 @@ pub fn is_required_type_ref(t: &TypeRef) -> bool {
|
||||
}
|
||||
|
||||
pub fn is_list_type(t: &TypeRef) -> bool {
|
||||
if let Some(introspection_response::__TypeKind::LIST) = t.kind {
|
||||
if let Some(__TypeKind::LIST) = t.kind {
|
||||
return true;
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
pub fn is_object_type(t: &FullType) -> bool {
|
||||
if let Some(introspection_response::__TypeKind::OBJECT) = t.kind {
|
||||
if let Some(__TypeKind::OBJECT) = t.kind {
|
||||
return true;
|
||||
}
|
||||
false
|
||||
|
Reference in New Issue
Block a user