Skip to content

Commit

Permalink
Feature flag snake-case-field (#176)
Browse files Browse the repository at this point in the history
  • Loading branch information
billy1624 authored Oct 8, 2024
1 parent 3d20e36 commit bc429f4
Show file tree
Hide file tree
Showing 13 changed files with 128 additions and 25 deletions.
4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ thiserror = { version = "1.0.44" }
fnv = { version = "1.0.7" }

[features]
default = []
default = ["camel-case-field"]
with-json = ["sea-orm/with-json"]
with-chrono = ["sea-orm/with-chrono", "async-graphql/chrono"]
with-time = ["sea-orm/with-time", "async-graphql/time"]
Expand All @@ -34,3 +34,5 @@ with-bigdecimal = ["sea-orm/with-bigdecimal", "async-graphql/bigdecimal"]
with-postgres-array = ["sea-orm/postgres-array"]
# with-ipnetwork = ["sea-orm/with-ipnetwork"]
# with-mac_address = ["sea-orm/with-mac_address"]
snake-case-field = []
camel-case-field = []
8 changes: 6 additions & 2 deletions src/enumerations/active_enum.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use async_graphql::dynamic::Enum;
use heck::ToUpperCamelCase;
use heck::{ToSnakeCase, ToUpperCamelCase};
use sea_orm::{ActiveEnum, DynIden, Value};

use crate::BuilderContext;
Expand All @@ -19,7 +19,11 @@ impl std::default::Default for ActiveEnumConfig {
format!("{}Enum", name.to_upper_camel_case())
}),
variant_name: Box::new(|_enum_name: &str, variant: &str| -> String {
variant.to_upper_camel_case().to_ascii_uppercase()
if cfg!(feature = "snake-case-field") {
variant.to_snake_case()
} else {
variant.to_upper_camel_case().to_ascii_uppercase()
}
}),
}
}
Expand Down
12 changes: 7 additions & 5 deletions src/inputs/active_enum_filter_input.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::BTreeSet;

use async_graphql::dynamic::ObjectAccessor;
use heck::ToUpperCamelCase;
use heck::{ToSnakeCase, ToUpperCamelCase};
use sea_orm::{ActiveEnum, ColumnTrait, ColumnType, Condition, DynIden, EntityTrait};

use crate::{ActiveEnumBuilder, BuilderContext, FilterInfo, FilterOperation, SeaResult};
Expand Down Expand Up @@ -88,10 +88,12 @@ where

let extract_variant = move |input: &str| -> String {
let variant = variants.iter().find(|variant| {
let variant = variant
.to_string()
.to_upper_camel_case()
.to_ascii_uppercase();
let variant = variant.to_string();
let variant = if cfg!(feature = "snake-case-field") {
variant.to_snake_case()
} else {
variant.to_upper_camel_case().to_ascii_uppercase()
};
variant.eq(input)
});
variant.unwrap().to_string()
Expand Down
9 changes: 8 additions & 1 deletion src/mutation/entity_create_batch_mutation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,14 @@ pub struct EntityCreateBatchMutationConfig {
impl std::default::Default for EntityCreateBatchMutationConfig {
fn default() -> Self {
EntityCreateBatchMutationConfig {
mutation_suffix: "CreateBatch".into(),
mutation_suffix: {
if cfg!(feature = "snake-case-field") {
"_create_batch"
} else {
"CreateBatch"
}
.into()
},
data_field: "data".into(),
}
}
Expand Down
9 changes: 8 additions & 1 deletion src/mutation/entity_create_one_mutation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,14 @@ pub struct EntityCreateOneMutationConfig {
impl std::default::Default for EntityCreateOneMutationConfig {
fn default() -> Self {
EntityCreateOneMutationConfig {
mutation_suffix: "CreateOne".into(),
mutation_suffix: {
if cfg!(feature = "snake-case-field") {
"_create_one"
} else {
"CreateOne"
}
.into()
},
data_field: "data".into(),
}
}
Expand Down
9 changes: 8 additions & 1 deletion src/mutation/entity_delete_mutation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,14 @@ pub struct EntityDeleteMutationConfig {
impl std::default::Default for EntityDeleteMutationConfig {
fn default() -> Self {
Self {
mutation_suffix: "Delete".into(),
mutation_suffix: {
if cfg!(feature = "snake-case-field") {
"_delete"
} else {
"Delete"
}
.into()
},
filter_field: "filter".into(),
}
}
Expand Down
9 changes: 8 additions & 1 deletion src/mutation/entity_update_mutation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,14 @@ pub struct EntityUpdateMutationConfig {
impl std::default::Default for EntityUpdateMutationConfig {
fn default() -> Self {
Self {
mutation_suffix: "Update".into(),
mutation_suffix: {
if cfg!(feature = "snake-case-field") {
"_update"
} else {
"Update"
}
.into()
},
data_field: "data".into(),
filter_field: "filter".into(),
}
Expand Down
18 changes: 16 additions & 2 deletions src/outputs/connection_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,22 @@ impl std::default::Default for ConnectionObjectConfig {
type_name: Box::new(|object_name: &str| -> String {
format!("{object_name}Connection")
}),
page_info: "pageInfo".into(),
pagination_info: "paginationInfo".into(),
page_info: {
if cfg!(feature = "snake-case-field") {
"page_info"
} else {
"pageInfo"
}
.into()
},
pagination_info: {
if cfg!(feature = "snake-case-field") {
"pagination_info"
} else {
"paginationInfo"
}
.into()
},
edges: "edges".into(),
nodes: "nodes".into(),
}
Expand Down
8 changes: 6 additions & 2 deletions src/outputs/entity_object.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use async_graphql::dynamic::{Field, FieldFuture, Object};
use async_graphql::{Error, Value};
use heck::{ToLowerCamelCase, ToUpperCamelCase};
use heck::{ToLowerCamelCase, ToSnakeCase, ToUpperCamelCase};
use sea_orm::{ColumnTrait, ColumnType, EntityName, EntityTrait, IdenStatic, Iterable, ModelTrait};

/// The configuration structure for EntityObjectBuilder
Expand All @@ -20,7 +20,11 @@ impl std::default::Default for EntityObjectConfig {
entity_name.to_upper_camel_case()
}),
column_name: Box::new(|_entity_name: &str, column_name: &str| -> String {
column_name.to_lower_camel_case()
if cfg!(feature = "snake-case-field") {
column_name.to_snake_case()
} else {
column_name.to_lower_camel_case()
}
}),
basic_type_suffix: "Basic".into(),
}
Expand Down
36 changes: 32 additions & 4 deletions src/outputs/page_info_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,38 @@ impl std::default::Default for PageInfoObjectConfig {
fn default() -> Self {
PageInfoObjectConfig {
type_name: "PageInfo".into(),
has_previous_page: "hasPreviousPage".into(),
has_next_page: "hasNextPage".into(),
start_cursor: "startCursor".into(),
end_cursor: "endCursor".into(),
has_previous_page: {
if cfg!(feature = "snake-case-field") {
"has_previous_page"
} else {
"hasPreviousPage"
}
.into()
},
has_next_page: {
if cfg!(feature = "snake-case-field") {
"has_next_page"
} else {
"hasNextPage"
}
.into()
},
start_cursor: {
if cfg!(feature = "snake-case-field") {
"start_cursor"
} else {
"startCursor"
}
.into()
},
end_cursor: {
if cfg!(feature = "snake-case-field") {
"end_cursor"
} else {
"endCursor"
}
.into()
},
}
}
}
Expand Down
7 changes: 6 additions & 1 deletion src/query/entity_object_relation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use async_graphql::{
dynamic::{Field, FieldFuture, FieldValue, InputValue, TypeRef},
Error,
};
use heck::ToSnakeCase;
use heck::{ToLowerCamelCase, ToSnakeCase};
use sea_orm::{EntityTrait, Iden, ModelTrait, RelationDef};

use crate::{
Expand All @@ -30,6 +30,11 @@ impl EntityObjectRelationBuilder {
<R as sea_orm::EntityTrait>::Model: Sync,
<<R as sea_orm::EntityTrait>::Column as std::str::FromStr>::Err: core::fmt::Debug,
{
let name = if cfg!(feature = "snake-case-field") {
name.to_snake_case()
} else {
name.to_lower_camel_case()
};
let context: &'static BuilderContext = self.context;
let entity_object_builder = EntityObjectBuilder { context };
let connection_object_builder = ConnectionObjectBuilder { context };
Expand Down
7 changes: 6 additions & 1 deletion src/query/entity_object_via_relation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use async_graphql::{
dynamic::{Field, FieldFuture, FieldValue, InputValue, TypeRef},
Error,
};
use heck::ToSnakeCase;
use heck::{ToLowerCamelCase, ToSnakeCase};
use sea_orm::{
ColumnTrait, Condition, DatabaseConnection, EntityTrait, Iden, ModelTrait, QueryFilter, Related,
};
Expand Down Expand Up @@ -33,6 +33,11 @@ impl EntityObjectViaRelationBuilder {
<<T as sea_orm::EntityTrait>::Column as std::str::FromStr>::Err: core::fmt::Debug,
<<R as sea_orm::EntityTrait>::Column as std::str::FromStr>::Err: core::fmt::Debug,
{
let name = if cfg!(feature = "snake-case-field") {
name.to_snake_case()
} else {
name.to_lower_camel_case()
};
let context: &'static BuilderContext = self.context;
let to_relation_definition = <T as Related<R>>::to();
let (via_relation_definition, is_via_relation) = match <T as Related<R>>::via() {
Expand Down
17 changes: 14 additions & 3 deletions src/query/entity_query_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use async_graphql::{
dynamic::{Field, FieldFuture, FieldValue, InputValue, TypeRef},
Error,
};
use heck::ToLowerCamelCase;
use heck::{ToLowerCamelCase, ToSnakeCase};
use sea_orm::{DatabaseConnection, EntityTrait, QueryFilter};

use crate::{
Expand All @@ -27,10 +27,21 @@ impl std::default::Default for EntityQueryFieldConfig {
fn default() -> Self {
EntityQueryFieldConfig {
type_name: Box::new(|object_name: &str| -> String {
object_name.to_lower_camel_case()
if cfg!(feature = "snake-case-field") {
object_name.to_snake_case()
} else {
object_name.to_lower_camel_case()
}
}),
filters: "filters".into(),
order_by: "orderBy".into(),
order_by: {
if cfg!(feature = "snake-case-field") {
"order_by"
} else {
"orderBy"
}
.into()
},
pagination: "pagination".into(),
}
}
Expand Down

0 comments on commit bc429f4

Please sign in to comment.