add fields

This commit is contained in:
Kasper Juul Hermansen 2023-01-30 22:21:16 +01:00
parent d2cddff365
commit 496a687bc3
Signed by: kjuulh
GPG Key ID: 57B6E1465221F912
20 changed files with 1133 additions and 634 deletions

5
Cargo.lock generated
View File

@ -190,7 +190,6 @@ dependencies = [
"dagger-core",
"eyre",
"genco",
"graphql-introspection-query",
"itertools",
"pretty_assertions",
"serde",
@ -200,6 +199,9 @@ dependencies = [
[[package]]
name = "dagger-core"
version = "0.1.0"
dependencies = [
"serde",
]
[[package]]
name = "dagger-rs"
@ -207,6 +209,7 @@ version = "0.1.2"
dependencies = [
"clap",
"dagger-codegen",
"dagger-core",
"dirs",
"eyre",
"flate2",

View File

@ -13,6 +13,7 @@ members = ["crates/dagger-codegen", "crates/dagger-sdk", "crates/dagger-core"]
[dependencies]
dagger-codegen = { path = "crates/dagger-codegen" }
dagger-core = { path = "crates/dagger-core" }
clap = "4.1.4"
dirs = "4.0.0"

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -6,3 +6,4 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
serde = { version = "1.0.152", features = ["derive"] }

View File

@ -0,0 +1,299 @@
#![allow(non_camel_case_types)]
use serde::{Deserialize, Deserializer, Serialize};
#[derive(Clone, Debug)]
pub enum __DirectiveLocation {
QUERY,
MUTATION,
SUBSCRIPTION,
FIELD,
FRAGMENT_DEFINITION,
FRAGMENT_SPREAD,
INLINE_FRAGMENT,
SCHEMA,
SCALAR,
OBJECT,
FIELD_DEFINITION,
ARGUMENT_DEFINITION,
INTERFACE,
UNION,
ENUM,
ENUM_VALUE,
INPUT_OBJECT,
INPUT_FIELD_DEFINITION,
Other(String),
}
impl Serialize for __DirectiveLocation {
fn serialize<S: serde::Serializer>(&self, ser: S) -> Result<S::Ok, S::Error> {
ser.serialize_str(match *self {
__DirectiveLocation::QUERY => "QUERY",
__DirectiveLocation::MUTATION => "MUTATION",
__DirectiveLocation::SUBSCRIPTION => "SUBSCRIPTION",
__DirectiveLocation::FIELD => "FIELD",
__DirectiveLocation::FRAGMENT_DEFINITION => "FRAGMENT_DEFINITION",
__DirectiveLocation::FRAGMENT_SPREAD => "FRAGMENT_SPREAD",
__DirectiveLocation::INLINE_FRAGMENT => "INLINE_FRAGMENT",
__DirectiveLocation::SCHEMA => "SCHEMA",
__DirectiveLocation::SCALAR => "SCALAR",
__DirectiveLocation::OBJECT => "OBJECT",
__DirectiveLocation::FIELD_DEFINITION => "FIELD_DEFINITION",
__DirectiveLocation::ARGUMENT_DEFINITION => "ARGUMENT_DEFINITION",
__DirectiveLocation::INTERFACE => "INTERFACE",
__DirectiveLocation::UNION => "UNION",
__DirectiveLocation::ENUM => "ENUM",
__DirectiveLocation::ENUM_VALUE => "ENUM_VALUE",
__DirectiveLocation::INPUT_OBJECT => "INPUT_OBJECT",
__DirectiveLocation::INPUT_FIELD_DEFINITION => "INPUT_FIELD_DEFINITION",
__DirectiveLocation::Other(ref s) => s.as_str(),
})
}
}
impl<'de> Deserialize<'de> for __DirectiveLocation {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let s = <&'de str>::deserialize(deserializer)?;
match s {
"QUERY" => Ok(__DirectiveLocation::QUERY),
"MUTATION" => Ok(__DirectiveLocation::MUTATION),
"SUBSCRIPTION" => Ok(__DirectiveLocation::SUBSCRIPTION),
"FIELD" => Ok(__DirectiveLocation::FIELD),
"FRAGMENT_DEFINITION" => Ok(__DirectiveLocation::FRAGMENT_DEFINITION),
"FRAGMENT_SPREAD" => Ok(__DirectiveLocation::FRAGMENT_SPREAD),
"INLINE_FRAGMENT" => Ok(__DirectiveLocation::INLINE_FRAGMENT),
"SCHEMA" => Ok(__DirectiveLocation::SCHEMA),
"SCALAR" => Ok(__DirectiveLocation::SCALAR),
"OBJECT" => Ok(__DirectiveLocation::OBJECT),
"FIELD_DEFINITION" => Ok(__DirectiveLocation::FIELD_DEFINITION),
"ARGUMENT_DEFINITION" => Ok(__DirectiveLocation::ARGUMENT_DEFINITION),
"INTERFACE" => Ok(__DirectiveLocation::INTERFACE),
"UNION" => Ok(__DirectiveLocation::UNION),
"ENUM" => Ok(__DirectiveLocation::ENUM),
"ENUM_VALUE" => Ok(__DirectiveLocation::ENUM_VALUE),
"INPUT_OBJECT" => Ok(__DirectiveLocation::INPUT_OBJECT),
"INPUT_FIELD_DEFINITION" => Ok(__DirectiveLocation::INPUT_FIELD_DEFINITION),
_ => Ok(__DirectiveLocation::Other(s.to_string())),
}
}
}
#[derive(Clone, Debug, PartialEq)]
pub enum __TypeKind {
SCALAR,
OBJECT,
INTERFACE,
UNION,
ENUM,
INPUT_OBJECT,
LIST,
NON_NULL,
Other(String),
}
impl Serialize for __TypeKind {
fn serialize<S: serde::Serializer>(&self, ser: S) -> Result<S::Ok, S::Error> {
ser.serialize_str(match *self {
__TypeKind::SCALAR => "SCALAR",
__TypeKind::OBJECT => "OBJECT",
__TypeKind::INTERFACE => "INTERFACE",
__TypeKind::UNION => "UNION",
__TypeKind::ENUM => "ENUM",
__TypeKind::INPUT_OBJECT => "INPUT_OBJECT",
__TypeKind::LIST => "LIST",
__TypeKind::NON_NULL => "NON_NULL",
__TypeKind::Other(ref s) => s.as_str(),
})
}
}
impl<'de> Deserialize<'de> for __TypeKind {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let s = <&'de str>::deserialize(deserializer)?;
match s {
"SCALAR" => Ok(__TypeKind::SCALAR),
"OBJECT" => Ok(__TypeKind::OBJECT),
"INTERFACE" => Ok(__TypeKind::INTERFACE),
"UNION" => Ok(__TypeKind::UNION),
"ENUM" => Ok(__TypeKind::ENUM),
"INPUT_OBJECT" => Ok(__TypeKind::INPUT_OBJECT),
"LIST" => Ok(__TypeKind::LIST),
"NON_NULL" => Ok(__TypeKind::NON_NULL),
_ => Ok(__TypeKind::Other(s.to_string())),
}
}
}
#[derive(Clone, Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct FullType {
pub kind: Option<__TypeKind>,
pub name: Option<String>,
pub description: Option<String>,
pub fields: Option<Vec<FullTypeFields>>,
pub input_fields: Option<Vec<FullTypeInputFields>>,
pub interfaces: Option<Vec<FullTypeInterfaces>>,
pub enum_values: Option<Vec<FullTypeEnumValues>>,
pub possible_types: Option<Vec<FullTypePossibleTypes>>,
}
#[derive(Clone, Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct FullTypeFieldsArgs {
#[serde(flatten)]
pub input_value: InputValue,
}
#[derive(Clone, Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct FullTypeFieldsType {
#[serde(flatten)]
pub type_ref: TypeRef,
}
#[derive(Clone, Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct FullTypeFields {
pub name: Option<String>,
pub description: Option<String>,
pub args: Option<Vec<Option<FullTypeFieldsArgs>>>,
#[serde(rename = "type")]
pub type_: Option<FullTypeFieldsType>,
pub is_deprecated: Option<bool>,
pub deprecation_reason: Option<String>,
}
#[derive(Clone, Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct FullTypeInputFields {
#[serde(flatten)]
pub input_value: InputValue,
}
#[derive(Clone, Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct FullTypeInterfaces {
#[serde(flatten)]
pub type_ref: TypeRef,
}
#[derive(Clone, Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct FullTypeEnumValues {
pub name: Option<String>,
pub description: Option<String>,
pub is_deprecated: Option<bool>,
pub deprecation_reason: Option<String>,
}
#[derive(Clone, Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct FullTypePossibleTypes {
#[serde(flatten)]
pub type_ref: TypeRef,
}
#[derive(Clone, Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct InputValue {
pub name: String,
pub description: Option<String>,
#[serde(rename = "type")]
pub type_: InputValueType,
pub default_value: Option<String>,
}
type InputValueType = TypeRef;
#[derive(Clone, Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TypeRef {
pub kind: Option<__TypeKind>,
pub name: Option<String>,
pub of_type: Option<Box<TypeRef>>,
}
#[derive(Clone, Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SchemaQueryType {
pub name: Option<String>,
}
#[derive(Clone, Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SchemaMutationType {
pub name: Option<String>,
}
#[derive(Clone, Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SchemaSubscriptionType {
pub name: Option<String>,
}
#[derive(Clone, Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SchemaTypes {
#[serde(flatten)]
pub full_type: FullType,
}
#[derive(Clone, Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SchemaDirectivesArgs {
#[serde(flatten)]
input_value: InputValue,
}
#[derive(Clone, Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SchemaDirectives {
pub name: Option<String>,
pub description: Option<String>,
pub locations: Option<Vec<Option<__DirectiveLocation>>>,
pub args: Option<Vec<Option<SchemaDirectivesArgs>>>,
}
#[derive(Clone, Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Schema {
pub query_type: Option<SchemaQueryType>,
pub mutation_type: Option<SchemaMutationType>,
pub subscription_type: Option<SchemaSubscriptionType>,
pub types: Option<Vec<Option<SchemaTypes>>>,
directives: Option<Vec<Option<SchemaDirectives>>>,
}
#[derive(Clone, Debug, Deserialize)]
pub struct SchemaContainer {
#[serde(rename = "__schema")]
pub schema: Option<Schema>,
}
#[derive(Deserialize, Debug)]
pub struct FullResponse<T> {
data: T,
}
#[derive(Debug, Deserialize)]
#[serde(untagged)]
pub enum IntrospectionResponse {
FullResponse(FullResponse<SchemaContainer>),
Schema(SchemaContainer),
}
impl IntrospectionResponse {
pub fn as_schema(&self) -> &SchemaContainer {
match self {
IntrospectionResponse::FullResponse(full_response) => &full_response.data,
IntrospectionResponse::Schema(schema) => schema,
}
}
pub fn into_schema(self) -> SchemaContainer {
match self {
IntrospectionResponse::FullResponse(full_response) => full_response.data,
IntrospectionResponse::Schema(schema) => schema,
}
}
}

View File

@ -1,3 +1,5 @@
pub mod introspection;
pub struct Scalar(String);
pub struct Boolean(bool);

File diff suppressed because it is too large Load Diff

View File

@ -1,4 +1,4 @@
use graphql_introspection_query::introspection_response::IntrospectionResponse;
use dagger_core::introspection::IntrospectionResponse;
use crate::{config::Config, engine::Engine, session::Session};

View File

@ -1,5 +1,5 @@
use dagger_core::introspection::{self, IntrospectionResponse};
use graphql_client::GraphQLQuery;
use graphql_introspection_query::introspection_response::IntrospectionResponse;
use reqwest::{
blocking::{Client, RequestBuilder},
header::{HeaderMap, HeaderValue, ACCEPT, CONTENT_TYPE},
@ -65,7 +65,7 @@ impl Session {
return Err(eyre::anyhow!(error_message));
}
let json: IntrospectionResponse = res.json()?;
let json: introspection::IntrospectionResponse = res.json()?;
Ok(json)
}