Skip to content
Open
Show file tree
Hide file tree
Changes from 6 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
8 changes: 1 addition & 7 deletions cot-cli/tests/migration_generator/keywords.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use cot::db::{Auto, model};
use cot::db::{Auto, ForeignKey, model};
Comment thread
xelab04 marked this conversation as resolved.
Outdated

#[model]
struct r#const {
Expand All @@ -7,9 +7,3 @@ struct r#const {
r#abstract: String,
r#type: i32,
}

#[model(table_name = "use")]
struct TestModel {
#[model(primary_key)]
id: Auto<i32>,
}
27 changes: 22 additions & 5 deletions 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>,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, also please add this field to the docs here

/// Implement the [`Model`] trait for a struct.
with an example of how to use this

}

impl FieldOpts {
Expand Down Expand Up @@ -205,11 +206,13 @@ impl FieldOpts {
symbol_resolver: &SymbolResolver,
self_reference: Option<&String>,
) -> Result<Field, syn::Error> {
let name = self
.ident
.clone()
.expect("Only named struct fields are supported");
let column_name = name.unraw().to_string();
let name = self.ident.clone().expect("Only structs are supported");

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