Compare commits

...

4 Commits

Author SHA1 Message Date
e0db105cba
add context to unwrap 2023-02-15 22:07:54 +01:00
b6caf27854
fix vec 2023-02-15 22:06:55 +01:00
61eb9f3228
with opts 2023-02-15 21:59:04 +01:00
427e47c675
add format function 2023-02-15 21:15:11 +01:00
8 changed files with 429 additions and 16 deletions

View File

@ -1,6 +1,7 @@
use std::sync::Arc;
use dagger_core::introspection::{FullType, InputValue, TypeRef, __TypeKind};
use eyre::ContextCompat;
use crate::utility::OptionExt;
@ -81,10 +82,19 @@ impl CommonFunctions {
)
}
__TypeKind::LIST => {
let mut inner_type = rf
.of_type
.as_ref()
.map(|t| t.clone())
.map(|t| *t)
.map(|t| self.format_type(&t, input))
.context("could not get inner type of list")
.unwrap();
representation =
self.format_type_funcs.format_kind_list(&mut representation);
r = rf.of_type.as_ref().map(|t| t.clone()).map(|t| *t);
continue;
self.format_type_funcs.format_kind_list(&mut inner_type);
return representation;
}
__TypeKind::NON_NULL => {
r = rf.of_type.as_ref().map(|t| t.clone()).map(|t| *t);

View File

@ -8,6 +8,18 @@ use dagger_core::introspection::Schema;
use self::generator::DynGenerator;
fn set_schema_parents(mut schema: Schema) -> Schema {
for t in schema.types.as_mut().into_iter().flatten().flatten() {
let t_parent = t.full_type.clone();
for mut field in t.full_type.fields.as_mut().into_iter().flatten() {
field.parent_type = Some(t_parent.clone());
}
}
return schema;
}
pub fn generate(schema: Schema, generator: DynGenerator) -> eyre::Result<String> {
let schema = set_schema_parents(schema);
generator.generate(schema)
}

View File

@ -1,5 +1,20 @@
use convert_case::{Case, Casing};
use dagger_core::introspection::FullTypeFields;
pub fn format_name(s: &str) -> String {
s.to_case(Case::Pascal)
}
pub fn format_struct_name(s: &str) -> String {
s.to_case(Case::Snake)
}
pub fn field_options_struct_name(field: &FullTypeFields) -> Option<String> {
field
.parent_type
.as_ref()
.map(|p| p.name.as_ref().map(|n| format_name(n)))
.flatten()
.zip(field.name.as_ref().map(|n| format_name(n)))
.map(|(parent_name, field_name)| format!("{parent_name}{field_name}Opts"))
}

View File

@ -23,7 +23,7 @@ pub struct RustGenerator {}
impl Generator for RustGenerator {
fn generate(&self, schema: Schema) -> eyre::Result<String> {
let render = Arc::new(Mutex::new(rust::Tokens::new()));
let common_funcs = CommonFunctions::new(Arc::new(FormatTypeFunc {}));
let common_funcs = Arc::new(CommonFunctions::new(Arc::new(FormatTypeFunc {})));
println!("generating dagger for rust");
let visitor = Visitor {
@ -31,6 +31,8 @@ impl Generator for RustGenerator {
handlers: VisitHandlers {
visit_scalar: Arc::new({
let render = render.clone();
let common_funcs = common_funcs.clone();
move |t| {
println!("generating scalar");
let rendered_scalar = render_scalar(t)?;
@ -47,10 +49,11 @@ impl Generator for RustGenerator {
}),
visit_object: Arc::new({
let render = render.clone();
let common_funcs = common_funcs.clone();
move |t| {
println!("generating object");
let rendered_scalar = render_object(t)?;
let rendered_scalar = render_object(&common_funcs, t)?;
let mut render = render.lock().unwrap();
@ -63,6 +66,7 @@ impl Generator for RustGenerator {
}),
visit_input: Arc::new({
let render = render.clone();
let common_funcs = common_funcs.clone();
move |t| {
println!("generating input");
@ -79,6 +83,7 @@ impl Generator for RustGenerator {
}),
visit_enum: Arc::new({
let render = render.clone();
let common_funcs = common_funcs.clone();
move |t| {
println!("generating enum");

View File

@ -3,7 +3,7 @@ use genco::prelude::rust;
use genco::quote;
use crate::functions::CommonFunctions;
use crate::rust::functions::format_name;
use crate::rust::functions::{format_name, format_struct_name};
pub fn render_input(funcs: &CommonFunctions, t: &FullType) -> eyre::Result<rust::Tokens> {
Ok(quote! {
@ -30,6 +30,6 @@ pub fn render_input_fields(
pub fn render_input_field(funcs: &CommonFunctions, field: &FullTypeInputFields) -> rust::Tokens {
quote! {
$(&field.input_value.name): $(funcs.format_input_type(&field.input_value.type_))
pub $(format_struct_name(&field.input_value.name)): $(funcs.format_input_type(&field.input_value.type_)),
}
}

View File

@ -1,17 +1,18 @@
use dagger_core::introspection::{FullType, FullTypeFields};
use dagger_core::introspection::{FullType, FullTypeFields, FullTypeFieldsArgs};
use genco::prelude::rust;
use genco::quote;
use crate::rust::functions::format_name;
use crate::functions::CommonFunctions;
use crate::rust::functions::{field_options_struct_name, format_name, format_struct_name};
use crate::utility::OptionExt;
pub fn render_object(t: &FullType) -> eyre::Result<rust::Tokens> {
pub fn render_object(funcs: &CommonFunctions, t: &FullType) -> eyre::Result<rust::Tokens> {
Ok(quote! {
pub struct $(t.name.pipe(|s| format_name(s))) {
}
$(t.fields.pipe(render_optional_args))
$(t.fields.pipe(|f| render_optional_args(funcs, f)))
impl $(t.name.pipe(|s| format_name(s))) {
@ -19,10 +20,13 @@ pub fn render_object(t: &FullType) -> eyre::Result<rust::Tokens> {
})
}
fn render_optional_args(fields: &Vec<FullTypeFields>) -> Option<rust::Tokens> {
fn render_optional_args(
funcs: &CommonFunctions,
fields: &Vec<FullTypeFields>,
) -> Option<rust::Tokens> {
let rendered_fields = fields
.iter()
.map(render_optional_arg)
.map(|f| render_optional_arg(funcs, f))
.flatten()
.collect::<Vec<_>>();
@ -35,6 +39,39 @@ fn render_optional_args(fields: &Vec<FullTypeFields>) -> Option<rust::Tokens> {
}
}
fn render_optional_arg(field: &FullTypeFields) -> Option<rust::Tokens> {
todo!()
fn render_optional_arg(funcs: &CommonFunctions, field: &FullTypeFields) -> Option<rust::Tokens> {
let output_type = field_options_struct_name(field);
let fields = field
.args
.pipe(|t| t.into_iter().flatten().collect::<Vec<_>>())
.pipe(|t| render_optional_field_args(funcs, t))
.flatten();
if let Some(fields) = fields {
Some(quote! {
pub struct $output_type {
$fields
}
})
} else {
None
}
}
fn render_optional_field_args(
funcs: &CommonFunctions,
args: &Vec<&FullTypeFieldsArgs>,
) -> Option<rust::Tokens> {
if args.len() == 0 {
return None;
}
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_)),
}
});
Some(quote! {
$(for arg in rendered_args join ($['\r']) => $arg)
})
}

View File

@ -161,6 +161,9 @@ pub struct FullTypeFields {
pub type_: Option<FullTypeFieldsType>,
pub is_deprecated: Option<bool>,
pub deprecation_reason: Option<String>,
#[serde(skip)]
pub parent_type: Option<FullType>,
}
#[derive(Clone, Debug, Deserialize)]

View File

@ -6,33 +6,364 @@ pub struct Platform(String);
pub struct SecretId(String);
pub struct SocketId(String);
pub struct BuildArg {
pub name: String,
pub value: String,
}
pub struct CacheVolume {
}
impl CacheVolume {
}
pub struct Container {
}
pub struct ContainerBuildOpts {
pub context: DirectoryID,
pub dockerfile: String,
pub build_args: Vec<BuildArg>,
pub target: String,
}
pub struct ContainerDirectoryOpts {
pub path: String,
}
pub struct ContainerEnvVariableOpts {
pub name: String,
}
pub struct ContainerExecOpts {
pub args: Vec<String>,
pub stdin: String,
pub redirect_stdout: String,
pub redirect_stderr: String,
pub experimental_privileged_nesting: bool,
}
pub struct ContainerExportOpts {
pub path: String,
pub platform_variants: Vec<ContainerID>,
}
pub struct ContainerFileOpts {
pub path: String,
}
pub struct ContainerFromOpts {
pub address: String,
}
pub struct ContainerLabelOpts {
pub name: String,
}
pub struct ContainerPipelineOpts {
pub name: String,
pub description: String,
}
pub struct ContainerPublishOpts {
pub address: String,
pub platform_variants: Vec<ContainerID>,
}
pub struct ContainerWithDefaultArgsOpts {
pub args: Vec<String>,
}
pub struct ContainerWithDirectoryOpts {
pub path: String,
pub directory: DirectoryID,
pub exclude: Vec<String>,
pub include: Vec<String>,
}
pub struct ContainerWithEntrypointOpts {
pub args: Vec<String>,
}
pub struct ContainerWithEnvVariableOpts {
pub name: String,
pub value: String,
}
pub struct ContainerWithExecOpts {
pub args: Vec<String>,
pub stdin: String,
pub redirect_stdout: String,
pub redirect_stderr: String,
pub experimental_privileged_nesting: bool,
}
pub struct ContainerWithFsOpts {
pub id: DirectoryID,
}
pub struct ContainerWithFileOpts {
pub path: String,
pub source: FileID,
pub permissions: isize,
}
pub struct ContainerWithLabelOpts {
pub name: String,
pub value: String,
}
pub struct ContainerWithMountedCacheOpts {
pub path: String,
pub cache: CacheID,
pub source: DirectoryID,
}
pub struct ContainerWithMountedDirectoryOpts {
pub path: String,
pub source: DirectoryID,
}
pub struct ContainerWithMountedFileOpts {
pub path: String,
pub source: FileID,
}
pub struct ContainerWithMountedSecretOpts {
pub path: String,
pub source: SecretID,
}
pub struct ContainerWithMountedTempOpts {
pub path: String,
}
pub struct ContainerWithNewFileOpts {
pub path: String,
pub contents: String,
pub permissions: isize,
}
pub struct ContainerWithRootfsOpts {
pub id: DirectoryID,
}
pub struct ContainerWithSecretVariableOpts {
pub name: String,
pub secret: SecretID,
}
pub struct ContainerWithUnixSocketOpts {
pub path: String,
pub source: SocketID,
}
pub struct ContainerWithUserOpts {
pub name: String,
}
pub struct ContainerWithWorkdirOpts {
pub path: String,
}
pub struct ContainerWithoutEnvVariableOpts {
pub name: String,
}
pub struct ContainerWithoutLabelOpts {
pub name: String,
}
pub struct ContainerWithoutMountOpts {
pub path: String,
}
pub struct ContainerWithoutUnixSocketOpts {
pub path: String,
}
impl Container {
}
pub struct Directory {
}
pub struct DirectoryDiffOpts {
pub other: DirectoryID,
}
pub struct DirectoryDirectoryOpts {
pub path: String,
}
pub struct DirectoryDockerBuildOpts {
pub dockerfile: String,
pub platform: Platform,
pub build_args: Vec<BuildArg>,
pub target: String,
}
pub struct DirectoryEntriesOpts {
pub path: String,
}
pub struct DirectoryExportOpts {
pub path: String,
}
pub struct DirectoryFileOpts {
pub path: String,
}
pub struct DirectoryLoadProjectOpts {
pub config_path: String,
}
pub struct DirectoryPipelineOpts {
pub name: String,
pub description: String,
}
pub struct DirectoryWithDirectoryOpts {
pub path: String,
pub directory: DirectoryID,
pub exclude: Vec<String>,
pub include: Vec<String>,
}
pub struct DirectoryWithFileOpts {
pub path: String,
pub source: FileID,
pub permissions: isize,
}
pub struct DirectoryWithNewDirectoryOpts {
pub path: String,
pub permissions: isize,
}
pub struct DirectoryWithNewFileOpts {
pub path: String,
pub contents: String,
pub permissions: isize,
}
pub struct DirectoryWithTimestampsOpts {
pub timestamp: isize,
}
pub struct DirectoryWithoutDirectoryOpts {
pub path: String,
}
pub struct DirectoryWithoutFileOpts {
pub path: String,
}
impl Directory {
}
pub struct EnvVariable {
}
impl EnvVariable {
}
pub struct File {
}
pub struct FileExportOpts {
pub path: String,
}
pub struct FileWithTimestampsOpts {
pub timestamp: isize,
}
impl File {
}
pub struct GitRef {
}
pub struct GitRefTreeOpts {
pub ssh_known_hosts: String,
pub ssh_auth_socket: SocketID,
}
impl GitRef {
}
pub struct GitRepository {
}
pub struct GitRepositoryBranchOpts {
pub name: String,
}
pub struct GitRepositoryCommitOpts {
pub id: String,
}
pub struct GitRepositoryTagOpts {
pub name: String,
}
impl GitRepository {
}
pub struct Host {
}
pub struct HostDirectoryOpts {
pub path: String,
pub exclude: Vec<String>,
pub include: Vec<String>,
}
pub struct HostEnvVariableOpts {
pub name: String,
}
pub struct HostUnixSocketOpts {
pub path: String,
}
pub struct HostWorkdirOpts {
pub exclude: Vec<String>,
pub include: Vec<String>,
}
impl Host {
}
pub struct HostVariable {
}
impl HostVariable {
}
pub struct Label {
}
impl Label {
}
pub struct Project {
}
impl Project {
}
pub struct Query {
}
pub struct QueryCacheVolumeOpts {
pub key: String,
}
pub struct QueryContainerOpts {
pub id: ContainerID,
pub platform: Platform,
}
pub struct QueryDirectoryOpts {
pub id: DirectoryID,
}
pub struct QueryFileOpts {
pub id: FileID,
}
pub struct QueryGitOpts {
pub url: String,
pub keep_git_dir: bool,
}
pub struct QueryHttpOpts {
pub url: String,
}
pub struct QueryPipelineOpts {
pub name: String,
pub description: String,
}
pub struct QueryProjectOpts {
pub name: String,
}
pub struct QuerySecretOpts {
pub id: SecretID,
}
pub struct QuerySocketOpts {
pub id: SocketID,
}
impl Query {
}
pub struct Secret {
}
impl Secret {
}
pub struct Socket {
}
impl Socket {
}