Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion cot-codegen/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ pub struct FieldOpts {
pub ty: syn::Type,
pub primary_key: darling::util::Flag,
pub unique: darling::util::Flag,
pub field_name: Option<String>,
}

impl FieldOpts {
Expand Down Expand Up @@ -206,7 +207,12 @@ impl FieldOpts {
self_reference: Option<&String>,
) -> Result<Field, syn::Error> {
let name = self.ident.clone().expect("Only structs are supported");
let column_name = name.unraw().to_string();

let column_name = if let Some(specified_field_name) = &self.field_name {
specified_field_name.clone()
} else {
name.unraw().to_string()
};

let (auto_value, foreign_key) = (
self.find_type("cot::db::Auto", symbol_resolver).is_some(),
Expand Down Expand Up @@ -489,6 +495,20 @@ mod tests {
assert_eq!(field.column_name, "abstract");
}

#[test]
fn field_opts_specified_field_name() {
let input: syn::Field = parse_quote! {
#[model(field_name="test_field")]
test: String
};
let field_opts = FieldOpts::from_field(&input).unwrap();
let field = field_opts
.as_field(&SymbolResolver::new(vec![]), Some(&"TestModel".to_string()))
.unwrap();
assert_eq!(field.name.to_string(), "test");
assert_eq!(field.column_name, "test_field");
}

#[test]
fn find_type_resolved() {
let input: syn::Type =
Expand Down