Skip to content

Commit

Permalink
add feature : graphql(name =..) for enums (#34)
Browse files Browse the repository at this point in the history
* add feature : graphql(name =..) for enums

* bump version

* bump nix

* udpate cargo.lock

* allow empty enum.value
  • Loading branch information
tacogips authored Jul 12, 2024
1 parent f9f9942 commit 41b1b77
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "async-graphql-reverse"
version = "0.6.0"
version = "0.6.1"
authors = ["tacogips <[email protected]>"]
edition = "2018"

Expand Down
8 changes: 8 additions & 0 deletions examples/simple/input/reverse.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ data_source = "use crate::datasource::DataSource"
target_enum = "UserType"
rename_items = "UPPERCASE"

[[enum.value]]
value = "Other"
rename = "OTHER"

[[enum]]
target_enum = "Sex"
rename_items = "snake_case"

[[resolver]]
target_type = "Query"
target_field = "type"
Expand Down
1 change: 1 addition & 0 deletions examples/simple/input/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ enum Sex {
enum UserType {
NotPayed
Payed
Other
}

interface User {
Expand Down
4 changes: 3 additions & 1 deletion examples/simple/output/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub enum Status {
EmailVerified,
}
#[derive(Enum, Copy, Clone, Debug, Eq, PartialEq)]
#[graphql(rename_items = "camelCase")]
#[graphql(rename_items = "snake_case")]
pub enum Sex {
Male,
Female,
Expand All @@ -19,6 +19,8 @@ pub enum Sex {
pub enum UserType {
NotPayed,
Payed,
#[graphql(name = "OTHER")]
Other,
}
#[derive(Enum, Copy, Clone, Debug, Eq, PartialEq)]
#[graphql(rename_items = "camelCase")]
Expand Down
8 changes: 4 additions & 4 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

inputs =
{
nixpkgs.url = "github:NixOS/nixpkgs/release-23.11";
nixpkgs.url = "github:NixOS/nixpkgs/release-24.05";
flake-utils.url = "github:numtide/flake-utils";
fenix =
{
Expand Down
8 changes: 8 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ pub struct HiddenFields {
pub struct EnumSetting {
pub target_enum: String,
pub rename_items: Option<String>,
pub value: Option<Vec<EnumValueSetting>>,
}

#[derive(Deserialize, Debug, Clone)]
pub struct EnumValueSetting {
pub value: String,
pub rename: Option<String>,
}

#[derive(Deserialize, Debug)]
Expand Down Expand Up @@ -101,6 +108,7 @@ pub type DefinedFieldName = String;

pub type FieldsResolverSetting<'a> = HashMap<DefinedFieldName, &'a ResolverSetting>;
pub type FieldsSetting<'a> = HashMap<DefinedFieldName, &'a FieldSetting>;
pub type EnumFieldsSetting<'a> = HashMap<DefinedEnumName, &'a FieldSetting>;

#[derive(Deserialize, Debug, PartialEq)]
#[serde(rename_all = "snake_case")]
Expand Down
56 changes: 40 additions & 16 deletions src/render/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use super::comment::*;
use super::files::{fmt_file, pathbuf_to_str};
use super::sorter::sort_by_line_pos_and_name;
use super::tokens::*;
use crate::config::{EnumSetting, RendererConfig};
use crate::config::{EnumSetting, EnumValueSetting, RendererConfig};
use anyhow::Result;
use heck::CamelCase;
use proc_macro2::TokenStream;
Expand Down Expand Up @@ -75,32 +75,55 @@ fn enum_token(
config: &RendererConfig,
enum_settings: &HashMap<String, EnumSetting>,
) -> Result<TokenStream> {
let enum_name = format_ident!("{}", enm.name.to_camel_case());
let enum_name = enm.name.to_camel_case();
let mut graphql_derive = quote! {};

// TODO(tacogips) using there_is_specific_rename_item is naive implementation. make this concise with macro or something
let mut there_is_specific_rename_item = false;
let mut enum_value_settings = HashMap::<String, &EnumValueSetting>::default();
if let Some(specific_enum_setting) = enum_settings.get(&enum_name) {
if let Some(specifig_rename_items) = &specific_enum_setting.rename_items {
there_is_specific_rename_item = true;
graphql_derive = quote! {
#[graphql(rename_items = #specifig_rename_items)]
}
}

if let Some(specific_enum_setting_value) = specific_enum_setting.value.as_ref() {
enum_value_settings = specific_enum_setting_value
.iter()
.map(|each| (each.value.to_string(), each))
.collect();
}
}

let enums_members: Vec<TokenStream> = enm
.values
.iter()
.map(|each_enum| {
.map(|each_enum_value| {
//each_enum.value_name.parse::<TokenStream>().unwrap()}
let each_enum = format_ident!("{}", each_enum.value_name.to_camel_case());
let enum_value_name = each_enum_value.value_name.to_camel_case();
let each_enum = format_ident!("{}", enum_value_name);

let enum_attribute = match enum_value_settings.get(&enum_value_name) {
Some(each_enum_setting) => match &each_enum_setting.rename {
Some(rename) => {
quote! {
#[graphql(name = #rename)]
}
}
None => quote! {},
},
None => quote! {},
};

quote! {
#enum_attribute
#each_enum
}
})
.collect();

let mut graphql_derive = quote! {};

// TODO(tacogips) using there_is_specific_rename_item is naive implementation. make this concise with macro or something
let mut there_is_specific_rename_item = false;
if let Some(specific_enum_setting) = enum_settings.get(&enm.name.to_camel_case()) {
if let Some(specifig_rename_item) = &specific_enum_setting.rename_items {
there_is_specific_rename_item = true;
graphql_derive = quote! {
#[graphql(rename_items = #specifig_rename_item)]
}
}
}
if !there_is_specific_rename_item {
if let Some(enum_rename_items) = config.enum_rename_items.as_ref() {
graphql_derive = quote! {
Expand All @@ -109,6 +132,7 @@ fn enum_token(
}
}

let enum_name = format_ident!("{}", enum_name);
let enum_members = separate_by_comma(enums_members);

let enum_def = quote! {
Expand Down
3 changes: 3 additions & 0 deletions src/render/tokens.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
use proc_macro2::TokenStream;
use quote::*;

//TODO(tacogips) rename to join_with_space
pub fn separate_by_space(tokens: Vec<TokenStream>) -> TokenStream {
separate_tokens_by(tokens, " ")
}

//TODO(tacogips) rename to join_with_comma
pub fn separate_by_comma(tokens: Vec<TokenStream>) -> TokenStream {
separate_tokens_by(tokens, ",")
}

//TODO(tacogips) rename to join_tokens_with
fn separate_tokens_by(mut tokens: Vec<TokenStream>, by: &'static str) -> TokenStream {
if tokens.is_empty() {
quote! {}
Expand Down

0 comments on commit 41b1b77

Please sign in to comment.