MCPcopy Create free account
hub / github.com/SeaQL/sea-orm / derive_active_model

Function derive_active_model

sea-orm-macros/src/derives/active_model.rs:34–169  ·  view source on GitHub ↗
(ident: &Ident, all_fields: IntoIter<Field>)

Source from the content-addressed store, hash-verified

32}
33
34fn derive_active_model(ident: &Ident, all_fields: IntoIter<Field>) -> syn::Result<TokenStream> {
35 let fields = all_fields.filter(field_not_ignored);
36
37 let field: Vec<Ident> = fields.clone().map(format_field_ident).collect();
38
39 let name: Vec<Ident> = fields
40 .clone()
41 .map(|field| {
42 let ident = field.ident.as_ref().unwrap().to_string();
43 let ident = trim_starting_raw_identifier(ident).to_upper_camel_case();
44 let ident = escape_rust_keyword(ident);
45 let mut ident = format_ident!("{}", &ident);
46 field
47 .attrs
48 .iter()
49 .filter(|attr| attr.path().is_ident("sea_orm"))
50 .try_for_each(|attr| {
51 attr.parse_nested_meta(|meta| {
52 if meta.path.is_ident("enum_name") {
53 let litstr: LitStr = meta.value()?.parse()?;
54 ident = syn::parse_str(&litstr.value()).unwrap();
55 } else {
56 // Reads the value expression to advance the parse stream.
57 // Some parameters, such as `primary_key`, do not have any value,
58 // so ignoring an error occurred here.
59 let _: Option<Expr> = meta.value().and_then(|v| v.parse()).ok();
60 }
61
62 Ok(())
63 })
64 })?;
65 Ok::<Ident, syn::Error>(ident)
66 })
67 .collect::<Result<_, _>>()?;
68
69 let ty: Vec<Type> = fields.into_iter().map(|Field { ty, .. }| ty).collect();
70
71 Ok(quote!(
72 #[doc = " Generated by sea-orm-macros"]
73 #[derive(Clone, Debug, PartialEq)]
74 pub struct ActiveModel {
75 #(
76 #[doc = " Generated by sea-orm-macros"]
77 pub #field: sea_orm::ActiveValue<#ty>
78 ),*
79 }
80
81 #[automatically_derived]
82 impl std::default::Default for ActiveModel {
83 fn default() -> Self {
84 <Self as sea_orm::ActiveModelBehavior>::new()
85 }
86 }
87
88 #[automatically_derived]
89 impl std::convert::From<#ident> for ActiveModel {
90 fn from(m: #ident) -> Self {
91 Self {

Callers 1

Calls 7

filterMethod · 0.80
unwrapMethod · 0.80
as_refMethod · 0.80
into_iterMethod · 0.80
escape_rust_keywordFunction · 0.70
parseMethod · 0.45

Tested by

no test coverage detected