Cont. Added support for using sea-orm with #[deny(missing_docs)] (#1531)

* Added support for using sea-orm with #[deny(missing_docs)] (#1522)

* feat(macros): Added documentation tags for generated entities

* chore: Added deny(missing_docs) attribute to basic example

* chore: Fix clippy errors

* ci: test missing docs of derive macros generated types

* Try missing docs (CI should fail)

* Revert "Try missing docs (CI should fail)"

This reverts commit 83356bfca8939e7807f14bad8bb816fcabc1bf7b.

---------

Co-authored-by: Lewin Probst, M.Sc <30552361+emirror-de@users.noreply.github.com>
This commit is contained in:
Billy Chan 2023-03-10 21:49:08 +08:00 committed by GitHub
parent 27e061c99e
commit 4f8ad56cc4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 93 additions and 13 deletions

View File

@ -1,15 +1,22 @@
//! The `cake` entity.
use sea_orm::entity::prelude::*; use sea_orm::entity::prelude::*;
/// Cake entity
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)] #[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
#[sea_orm(table_name = "cake")] #[sea_orm(table_name = "cake")]
pub struct Model { pub struct Model {
#[sea_orm(primary_key)] #[sea_orm(primary_key)]
/// id field
pub id: i32, pub id: i32,
/// name field
pub name: String, pub name: String,
} }
/// Cake relation
#[derive(Copy, Clone, Debug, EnumIter)] #[derive(Copy, Clone, Debug, EnumIter)]
pub enum Relation { pub enum Relation {
/// Fruit relation
Fruit, Fruit,
} }

View File

@ -1,5 +1,8 @@
//! The `cake_filling` entity.
use sea_orm::entity::prelude::*; use sea_orm::entity::prelude::*;
/// CakeFilling entity
#[derive(Copy, Clone, Default, Debug, DeriveEntity)] #[derive(Copy, Clone, Default, Debug, DeriveEntity)]
pub struct Entity; pub struct Entity;
@ -9,21 +12,30 @@ impl EntityName for Entity {
} }
} }
/// CakeFilling model
#[derive(Clone, Debug, PartialEq, Eq, DeriveModel, DeriveActiveModel)] #[derive(Clone, Debug, PartialEq, Eq, DeriveModel, DeriveActiveModel)]
pub struct Model { pub struct Model {
/// cake_id field
pub cake_id: i32, pub cake_id: i32,
/// filling_id field
pub filling_id: i32, pub filling_id: i32,
} }
/// CakeFilling column
#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)] #[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)]
pub enum Column { pub enum Column {
/// CakeId column
CakeId, CakeId,
/// FillingId column
FillingId, FillingId,
} }
/// CakeFilling primary key
#[derive(Copy, Clone, Debug, EnumIter, DerivePrimaryKey)] #[derive(Copy, Clone, Debug, EnumIter, DerivePrimaryKey)]
pub enum PrimaryKey { pub enum PrimaryKey {
/// CakeId primary key
CakeId, CakeId,
/// FillingId primary key
FillingId, FillingId,
} }
@ -35,9 +47,12 @@ impl PrimaryKeyTrait for PrimaryKey {
} }
} }
/// CakeFilling relation
#[derive(Copy, Clone, Debug, EnumIter)] #[derive(Copy, Clone, Debug, EnumIter)]
pub enum Relation { pub enum Relation {
/// Cake relation
Cake, Cake,
/// Filling relation
Filling, Filling,
} }

View File

@ -1,5 +1,8 @@
//! The `filling` entity.
use sea_orm::entity::prelude::*; use sea_orm::entity::prelude::*;
/// Filling entity
#[derive(Copy, Clone, Default, Debug, DeriveEntity)] #[derive(Copy, Clone, Default, Debug, DeriveEntity)]
pub struct Entity; pub struct Entity;
@ -9,20 +12,28 @@ impl EntityName for Entity {
} }
} }
/// Filling model
#[derive(Clone, Debug, PartialEq, Eq, DeriveModel, DeriveActiveModel)] #[derive(Clone, Debug, PartialEq, Eq, DeriveModel, DeriveActiveModel)]
pub struct Model { pub struct Model {
/// id field
pub id: i32, pub id: i32,
/// name field
pub name: String, pub name: String,
} }
/// Filling column
#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)] #[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)]
pub enum Column { pub enum Column {
/// Id column
Id, Id,
/// Name column
Name, Name,
} }
/// Filling primary key
#[derive(Copy, Clone, Debug, EnumIter, DerivePrimaryKey)] #[derive(Copy, Clone, Debug, EnumIter, DerivePrimaryKey)]
pub enum PrimaryKey { pub enum PrimaryKey {
/// Id primary key
Id, Id,
} }
@ -34,6 +45,7 @@ impl PrimaryKeyTrait for PrimaryKey {
} }
} }
/// Filling relation
#[derive(Copy, Clone, Debug, EnumIter)] #[derive(Copy, Clone, Debug, EnumIter)]
pub enum Relation {} pub enum Relation {}

View File

@ -1,5 +1,8 @@
//! The `fruit` entity.
use sea_orm::entity::prelude::*; use sea_orm::entity::prelude::*;
/// Fruit entity
#[derive(Copy, Clone, Default, Debug, DeriveEntity)] #[derive(Copy, Clone, Default, Debug, DeriveEntity)]
pub struct Entity; pub struct Entity;
@ -9,22 +12,32 @@ impl EntityName for Entity {
} }
} }
/// Fruit model
#[derive(Clone, Debug, PartialEq, Eq, DeriveModel, DeriveActiveModel)] #[derive(Clone, Debug, PartialEq, Eq, DeriveModel, DeriveActiveModel)]
pub struct Model { pub struct Model {
/// id field
pub id: i32, pub id: i32,
/// name field
pub name: String, pub name: String,
/// cake_id field
pub cake_id: Option<i32>, pub cake_id: Option<i32>,
} }
/// Fruit column
#[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)] #[derive(Copy, Clone, Debug, EnumIter, DeriveColumn)]
pub enum Column { pub enum Column {
/// Id column
Id, Id,
/// Name column
Name, Name,
/// CakeId column
CakeId, CakeId,
} }
/// Fruit primary key
#[derive(Copy, Clone, Debug, EnumIter, DerivePrimaryKey)] #[derive(Copy, Clone, Debug, EnumIter, DerivePrimaryKey)]
pub enum PrimaryKey { pub enum PrimaryKey {
/// Id primary key
Id, Id,
} }
@ -36,8 +49,10 @@ impl PrimaryKeyTrait for PrimaryKey {
} }
} }
/// Fruit relation
#[derive(Copy, Clone, Debug, EnumIter)] #[derive(Copy, Clone, Debug, EnumIter)]
pub enum Relation { pub enum Relation {
/// Cake relation
Cake, Cake,
} }

View File

@ -1,11 +1,16 @@
//! Basic sea-orm example.
#![deny(missing_docs)]
use sea_orm::Database; use sea_orm::Database;
mod entities; mod entities;
mod example_cake; pub mod example_cake;
mod example_cake_filling; pub mod example_cake_filling;
mod example_filling; pub mod example_filling;
mod example_fruit; pub mod example_fruit;
mod operation; mod operation;
pub mod sea_orm_active_enums;
mod select; mod select;
use entities::*; use entities::*;

View File

@ -0,0 +1,15 @@
//! SeaORM's active enums.
use sea_orm::entity::prelude::*;
/// Tea active enum
#[derive(Debug, Clone, PartialEq, Eq, EnumIter, DeriveActiveEnum)]
#[sea_orm(rs_type = "String", db_type = "Enum", enum_name = "tea")]
pub enum Tea {
/// EverydayTea variant
#[sea_orm(string_value = "EverydayTea")]
EverydayTea,
/// BreakfastTea variant
#[sea_orm(string_value = "BreakfastTea")]
BreakfastTea,
}

View File

@ -256,9 +256,11 @@ impl ActiveEnum {
.collect(); .collect();
quote!( quote!(
#[doc = " Generated by sea-orm-macros"]
#[derive(Debug, Clone, PartialEq, Eq, sea_orm::EnumIter)] #[derive(Debug, Clone, PartialEq, Eq, sea_orm::EnumIter)]
pub enum #enum_variant_iden { pub enum #enum_variant_iden {
#( #(
#[doc = " Generated by sea-orm-macros"]
#enum_variants, #enum_variants,
)* )*
} }
@ -276,6 +278,7 @@ impl ActiveEnum {
#[automatically_derived] #[automatically_derived]
impl #ident { impl #ident {
#[doc = " Generated by sea-orm-macros"]
pub fn iden_values() -> Vec<sea_orm::sea_query::DynIden> { pub fn iden_values() -> Vec<sea_orm::sea_query::DynIden> {
<#enum_variant_iden as sea_orm::strum::IntoEnumIterator>::iter() <#enum_variant_iden as sea_orm::strum::IntoEnumIterator>::iter()
.map(|v| sea_orm::sea_query::SeaRc::new(v) as sea_orm::sea_query::DynIden) .map(|v| sea_orm::sea_query::SeaRc::new(v) as sea_orm::sea_query::DynIden)
@ -297,6 +300,7 @@ impl ActiveEnum {
}; };
quote!( quote!(
#[doc = " Generated by sea-orm-macros"]
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
pub struct #enum_name_iden; pub struct #enum_name_iden;

View File

@ -38,11 +38,10 @@ pub fn expand_derive_active_model(ident: Ident, data: Data) -> syn::Result<Token
fn derive_active_model(all_fields: IntoIter<Field>) -> syn::Result<TokenStream> { fn derive_active_model(all_fields: IntoIter<Field>) -> syn::Result<TokenStream> {
let fields = all_fields.filter(field_not_ignored); let fields = all_fields.filter(field_not_ignored);
let field: Vec<Ident> = fields.clone().into_iter().map(format_field_ident).collect(); let field: Vec<Ident> = fields.clone().map(format_field_ident).collect();
let name: Vec<Ident> = fields let name: Vec<Ident> = fields
.clone() .clone()
.into_iter()
.map(|field| { .map(|field| {
let ident = field.ident.as_ref().unwrap().to_string(); let ident = field.ident.as_ref().unwrap().to_string();
let ident = trim_starting_raw_identifier(ident).to_camel_case(); let ident = trim_starting_raw_identifier(ident).to_camel_case();
@ -78,9 +77,14 @@ fn derive_active_model(all_fields: IntoIter<Field>) -> syn::Result<TokenStream>
let ty: Vec<Type> = fields.into_iter().map(|Field { ty, .. }| ty).collect(); let ty: Vec<Type> = fields.into_iter().map(|Field { ty, .. }| ty).collect();
Ok(quote!( Ok(quote!(
#[doc = " Generated by sea-orm-macros"]
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
pub struct ActiveModel { pub struct ActiveModel {
#(pub #field: sea_orm::ActiveValue<#ty>),*
#(
#[doc = " Generated by sea-orm-macros"]
pub #field: sea_orm::ActiveValue<#ty>
),*
} }
#[automatically_derived] #[automatically_derived]
@ -172,11 +176,7 @@ fn derive_into_model(model_fields: IntoIter<Field>) -> syn::Result<TokenStream>
.into_iter() .into_iter()
.map(format_field_ident) .map(format_field_ident)
.collect(); .collect();
let model_field: Vec<Ident> = model_fields let model_field: Vec<Ident> = model_fields.clone().map(format_field_ident).collect();
.clone()
.into_iter()
.map(format_field_ident)
.collect();
let ignore_attr: Vec<bool> = model_fields let ignore_attr: Vec<bool> = model_fields
.map(|field| !field_not_ignored(&field)) .map(|field| !field_not_ignored(&field))

View File

@ -43,6 +43,7 @@ pub fn expand_derive_entity_model(data: Data, attrs: Vec<Attribute>) -> syn::Res
.as_ref() .as_ref()
.map(|table_name| { .map(|table_name| {
quote! { quote! {
#[doc = " Generated by sea-orm-macros"]
#[derive(Copy, Clone, Default, Debug, sea_orm::prelude::DeriveEntity)] #[derive(Copy, Clone, Default, Debug, sea_orm::prelude::DeriveEntity)]
pub struct Entity; pub struct Entity;
@ -72,6 +73,7 @@ pub fn expand_derive_entity_model(data: Data, attrs: Vec<Attribute>) -> syn::Res
if let Some(table_name) = table_name { if let Some(table_name) = table_name {
let table_field_name = Ident::new("Table", Span::call_site()); let table_field_name = Ident::new("Table", Span::call_site());
columns_enum.push(quote! { columns_enum.push(quote! {
#[doc = " Generated by sea-orm-macros"]
#[sea_orm(table_name=#table_name)] #[sea_orm(table_name=#table_name)]
#[strum(disabled)] #[strum(disabled)]
#table_field_name #table_field_name
@ -226,8 +228,11 @@ pub fn expand_derive_entity_model(data: Data, attrs: Vec<Attribute>) -> syn::Res
let variant_attrs = match &column_name { let variant_attrs = match &column_name {
Some(column_name) => quote! { Some(column_name) => quote! {
#[sea_orm(column_name = #column_name)] #[sea_orm(column_name = #column_name)]
#[doc = " Generated by sea-orm-macros"]
},
None => quote! {
#[doc = " Generated by sea-orm-macros"]
}, },
None => quote! {},
}; };
if ignore { if ignore {
@ -360,6 +365,7 @@ pub fn expand_derive_entity_model(data: Data, attrs: Vec<Attribute>) -> syn::Res
quote! { (#primary_key_types) } quote! { (#primary_key_types) }
}; };
quote! { quote! {
#[doc = " Generated by sea-orm-macros"]
#[derive(Copy, Clone, Debug, EnumIter, DerivePrimaryKey)] #[derive(Copy, Clone, Debug, EnumIter, DerivePrimaryKey)]
pub enum PrimaryKey { pub enum PrimaryKey {
#primary_keys #primary_keys
@ -377,6 +383,7 @@ pub fn expand_derive_entity_model(data: Data, attrs: Vec<Attribute>) -> syn::Res
}; };
Ok(quote! { Ok(quote! {
#[doc = " Generated by sea-orm-macros"]
#[derive(Copy, Clone, Debug, sea_orm::prelude::EnumIter, sea_orm::prelude::DeriveColumn)] #[derive(Copy, Clone, Debug, sea_orm::prelude::EnumIter, sea_orm::prelude::DeriveColumn)]
pub enum Column { pub enum Column {
#columns_enum #columns_enum