Skip to content

Commit

Permalink
Merge pull request #21 from wslongchen/develop
Browse files Browse the repository at this point in the history
feat(develop): Upgrade Something better
  • Loading branch information
wslongchen authored Dec 21, 2021
2 parents dbab83f + a72e60e commit 3b35eb4
Show file tree
Hide file tree
Showing 23 changed files with 2,009 additions and 1,408 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "akita"
version = "0.3.4"
version = "0.3.5"
authors = ["mrpan <[email protected]>"]
edition = "2018"
description = "Akita - Mini orm for rust."
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ use akita::*;
use akita::prelude::*;

/// Annotion Support: AkitaTable、table_id、field (name, exist)
#[derive(AkitaTable, Clone, Default, ToValue, FromValue)]
#[derive(AkitaTable, Clone, Default)]
#[table(name = "t_system_user")]
pub struct User {
#[table_id(name = "id")]
Expand Down
8 changes: 6 additions & 2 deletions akita_core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "akita_core"
version = "0.3.3"
version = "0.3.4"
authors = ["mrpan <[email protected]>"]
edition = "2018"
description = "Akita - Mini orm for rust."
Expand All @@ -21,4 +21,8 @@ bigdecimal = {version = "0.3.0", features = ["serde"]}
chrono = { version = "0.4", features = ["serde"]}
uuid = {version = "0.8.2", features = ["serde", "v4"]}
base64 = "0.9"
indexmap = "1.7.0"
indexmap = "1.7.0"
syn = { version = "1.0", features = ["extra-traits"] }
quote = "1.0"
proc-macro2 = "1.0.12"
heck = "^0.3"
15 changes: 14 additions & 1 deletion akita_core/src/information.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::hash::{Hasher, Hash};
use serde::{Serialize, Deserialize};
use uuid::Uuid;

use crate::{types::SqlType, comm::keywords_safe};
use crate::{types::SqlType, comm::keywords_safe, Value};

/// Table
Expand Down Expand Up @@ -94,9 +94,18 @@ pub struct FieldName {
pub alias: Option<String>,
/// exist in actual table
pub exist: bool,
pub select: bool,
pub fill: Option<Fill>,
pub field_type: FieldType,
}

#[derive(Debug, PartialEq, Clone)]
pub struct Fill {
pub mode: String,
pub value: Option<Value>,
}


#[derive(Debug, PartialEq, Clone, Serialize, Deserialize)]
pub enum FieldType {
TableId(String),
Expand All @@ -120,6 +129,8 @@ impl FieldName {
table: Some(table),
alias: None,
exist: true,
select: true,
fill: None,
field_type: FieldType::TableField,
}
} else {
Expand All @@ -128,6 +139,8 @@ impl FieldName {
table: None,
alias: None,
exist: true,
select: true,
fill: None,
field_type: FieldType::TableField,
}
}
Expand Down
1 change: 1 addition & 0 deletions akita_core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![deny(clippy::all)]
#![feature(associated_type_bounds)]

mod information;
mod error;
Expand Down
6 changes: 0 additions & 6 deletions akita_core/src/param.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,6 @@ where
}
}

impl From<()> for Params {
fn from(_: ()) -> Params {
Params::Nil
}
}

impl From<Value> for Params {
fn from(x: Value) -> Params {
Params::Vector(vec![x])
Expand Down
154 changes: 154 additions & 0 deletions akita_core/src/printer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
use crate::entity::Entity;
use crate::TableDef;
use quote::quote;
use proc_macro::{TokenStream};

#[derive(Clone, Debug)]
pub struct EntityPrinter {
pub(crate) entities: Vec<TableDef>,
}

pub struct PrinterOutput {
pub files: Vec<OutputFile>,
}

pub struct OutputFile {
pub name: String,
pub content: String,
}

impl EntityPrinter {
pub fn generate(self, ) {

}

pub fn write_entities(&self, expanded_format: bool) -> Vec<OutputFile> {
self.entities
.iter()
.map(|entity| {
let mut lines = Vec::new();
Self::write_doc_comment(&mut lines);
let code_blocks = if expanded_format {
Self::gen_expanded_code_blocks(entity)
} else {
Self::gen_compact_code_blocks(entity)
};
Self::write(&mut lines, code_blocks);
OutputFile {
name: format!("{}.rs", entity.get_table_name_snake_case()),
content: lines.join("\n\n"),
}
})
.collect()
}

pub fn write_doc_comment(lines: &mut Vec<String>) {
let ver = env!("CARGO_PKG_VERSION");
let comments = vec![format!(
"//! Akita Entity. Generated by akita_codegen {}",
ver
)];
lines.extend(comments);
lines.push("".to_owned());
}

pub fn gen_compact_code_blocks(entity: &Entity) -> Vec<TokenStream> {
let mut imports = Self::gen_import();
imports.extend(Self::gen_import_active_enum(entity));
let mut code_blocks = vec![imports, Self::gen_compact_model_struct(entity)];
code_blocks.extend(Self::gen_impl_conjunct_related(entity));
code_blocks.extend(vec![Self::gen_impl_active_model_behavior()]);
code_blocks
}

pub fn gen_compact_model_struct(entity: &Entity) -> TokenStream {
let table_name = entity.table_name().as_str();
let column_names_snake_case = entity.get_column_names_snake_case();
let column_rs_types = entity.get_column_rs_types();
let primary_keys: Vec<String> = entity
.primary_keys
.iter()
.map(|pk| pk.name.clone())
.collect();
let attrs: Vec<TokenStream> = entity
.columns
.iter()
.map(|col| {
let mut attrs: Punctuated<_, Comma> = Punctuated::new();
if primary_keys.contains(&col.name) {
attrs.push(quote! { primary_key });
if !col.auto_increment {
attrs.push(quote! { auto_increment = false });
}
}
if let Some(ts) = col.get_col_type_attrs() {
attrs.extend(vec![ts]);
if !col.not_null {
attrs.push(quote! { nullable });
}
};
if col.unique {
attrs.push(quote! { unique });
}
if !attrs.is_empty() {
let mut ts = TokenStream::new();
for (i, attr) in attrs.into_iter().enumerate() {
if i > 0 {
ts = quote! { #ts, };
}
ts = quote! { #ts #attr };
}
quote! {
#[sea_orm(#ts)]
}
} else {
TokenStream::new()
}
})
.collect();

let extra_derive = with_serde.extra_derive();

quote! {
#[derive(Clone, Debug, PartialEq, DeriveEntityModel #extra_derive)]
#[sea_orm(table_name = #table_name)]
pub struct Model {
#(
#attrs
pub #column_names_snake_case: #column_rs_types,
)*
}
}
}

pub fn gen_import() -> TokenStream {
let prelude_import = quote!(
use akiata::*;
);
prelude_import
}

pub fn gen_expanded_code_blocks(entity: &Entity) -> Vec<TokenStream> {
let mut imports = Self::gen_import();
let mut code_blocks = vec![
imports,
Self::gen_entity_struct(),
// Self::gen_impl_entity_name(entity),
// Self::gen_model_struct(entity, with_serde),
// Self::gen_column_enum(entity),
// Self::gen_primary_key_enum(entity),
// Self::gen_impl_primary_key(entity),
// Self::gen_relation_enum(entity),
// Self::gen_impl_column_trait(entity),
// Self::gen_impl_relation_trait(entity),
];
code_blocks
}

pub fn gen_entity_struct() -> TokenStream {
quote! {
#[derive(Copy, Clone, Default, Debug, AkitaTable, ToAkita, FromAkita)]
pub struct Entity;
}
}
}
7 changes: 6 additions & 1 deletion akita_core/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,8 +433,13 @@ where
}
}

impl ToValue for () {
fn to_value(&self) -> Value {
Value::Nil
}
}

impl<T> ToValue for &T
impl <T> ToValue for &T
where
T: ToValue,
{
Expand Down
8 changes: 6 additions & 2 deletions akita_derive/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "akita_derive"
version = "0.3.2"
version = "0.3.3"
authors = ["mrpan <[email protected]>"]
edition = "2018"
description = "Akita - Mini orm for rust."
Expand All @@ -17,4 +17,8 @@ proc-macro = true
[dependencies]
syn = { version = "1.0", features = ["extra-traits"] }
quote = "1.0"
proc-macro2 = "1.0.12"
proc-macro2 = "1.0.12"
proc-macro-error = "1"
lazy_static = "1"
if_chain = "1"
regex = "1"
Loading

0 comments on commit 3b35eb4

Please sign in to comment.