Skip to content

Commit

Permalink
add clippy test to ci
Browse files Browse the repository at this point in the history
  • Loading branch information
longfangsong committed Oct 16, 2021
1 parent 430a6e1 commit d407618
Show file tree
Hide file tree
Showing 10 changed files with 58 additions and 82 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ jobs:
- uses: actions-rs/toolchain@v1
with:
toolchain: nightly
- name: Run clippy
uses: actions-rs/cargo@v1
with:
command: clippy -- -D warnings
- name: Run unit tests
uses: actions-rs/cargo@v1
with:
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ tera = "1.12.1"
convert_case = "0.4.0"
pluralize-rs = "0.1.0"
include_dir = { version = "0.6.0", features = ["search"] }
serde = {version="1.0.126", features = ["derive"]}
serde = { version = "1.0.126", features = ["derive"] }
serde_yaml = "0.8.17"
serde_json = "1.0.64"
toml = "0.5.8"
Expand Down
20 changes: 10 additions & 10 deletions src/config/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use structopt::StructOpt;
)]
pub struct Config {
/// Config file path.
#[structopt(short="i", long, alias="input", parse(from_os_str), env = "CONFIG")]
#[structopt(short = "i", long, alias = "input", parse(from_os_str), env = "CONFIG")]
pub config: PathBuf,
/// Output project path.
#[structopt(short, long, parse(from_os_str), env = "OUTPUT")]
Expand All @@ -19,23 +19,23 @@ pub struct Config {
pub force: bool,

/// Output project path.
#[structopt(alias="dbms", long, env = "DATABASE")]
#[structopt(alias = "dbms", long, env = "DATABASE")]
pub database_engine: Option<String>,
#[structopt(short, long, env = "API_NAME")]
pub name: Option<String>,

#[structopt(alias="ddl", long, env = "DDL")]
#[structopt(alias = "ddl", long, env = "DDL")]
pub load_from_ddl: Option<String>,
#[structopt(short="load-db", long, env = "LOAD_DB")]
#[structopt(short = "load-db", long, env = "LOAD_DB")]
pub load_from_db: Option<String>,

#[structopt(short="d", long, env = "DOCKER")]
pub generate_docker: Option<bool>,
#[structopt(alias="du", long, env = "DOCKER_USERNAME")]
#[structopt(short = "d", long, env = "DOCKER")]
pub generate_docker: Option<bool>,
#[structopt(alias = "du", long, env = "DOCKER_USERNAME")]
pub docker_username: Option<String>,
#[structopt(alias="dt", long, env = "DOCKER_TAG")]
pub docker_tag: Option<String>,
#[structopt(alias = "dt", long, env = "DOCKER_TAG")]
pub docker_tag: Option<String>,

#[structopt(short="k8s", long, env = "KUBERNETES")]
#[structopt(short = "k8s", long, env = "KUBERNETES")]
pub kubernetes: Option<bool>,
}
6 changes: 2 additions & 4 deletions src/config/file.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
use std::fs::OpenOptions;

use anyhow::{anyhow, Result};
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize, Debug, Clone)]
Expand All @@ -10,6 +7,7 @@ pub struct Field {
pub data_type: String,
}

#[allow(clippy::upper_case_acronyms)]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct API {
pub name: String,
Expand Down Expand Up @@ -54,6 +52,7 @@ impl Default for Docker {
}
}

#[allow(clippy::upper_case_acronyms)]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct CICD {
#[serde(default)]
Expand Down Expand Up @@ -83,4 +82,3 @@ pub struct Config {
#[serde(default)]
pub cicd: CICD,
}

22 changes: 12 additions & 10 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
mod cli;
mod file;
use anyhow::anyhow;
use std::{fs::File, io::Read, path::PathBuf};
use std::{ffi::OsStr, fs::File, io::Read, path::PathBuf};

use structopt::StructOpt;

Expand All @@ -18,8 +18,8 @@ pub fn from_cli_config() -> anyhow::Result<Config> {
let mut file_config: file::Config = match cli_config
.config
.extension()
.and_then(|it| it.to_str())
.ok_or(anyhow!("Cannot open config file"))?
.and_then(OsStr::to_str)
.ok_or_else(|| anyhow!("Cannot open config file"))?
{
"toml" => {
let mut content = String::new();
Expand Down Expand Up @@ -64,17 +64,15 @@ pub fn from_cli_config() -> anyhow::Result<Config> {
}
let try_load_from_db = if let Some(addr) = cli_config.load_from_db {
Some(addr)
} else if let Some(addr) = file_config.implementation.database.url {
Some(addr)
} else {
None
file_config.implementation.database.url.clone()
};
if try_load_from_db.is_some() {
if cli_config.name.is_none() {
return Err(anyhow!("I need to know API name before load it from db!"));
}
todo!("load api config from database");
}
todo!("load api config from database");
}

if let Some(generate_docker) = cli_config.generate_docker {
Expand All @@ -92,15 +90,19 @@ pub fn from_cli_config() -> anyhow::Result<Config> {
file_config.cicd.kubernetes = match file_config.cicd.kubernetes {
Some(false) => Some(false),
Some(true) if file_config.implementation.database.url.is_none() => {
return Err(anyhow!("generating kubernetes yaml file requires database connection string"));
return Err(anyhow!(
"generating kubernetes yaml file requires database connection string"
));
}
Some(true) if file_config.cicd.docker.username.is_none() => {
// todo: support other docker image registry than dockerhub
return Err(anyhow!("generating kubernetes yaml file requires docker username"));
return Err(anyhow!(
"generating kubernetes yaml file requires docker username"
));
}
Some(true) => Some(true),
None if file_config.implementation.database.url.is_none() => Some(false),
None => Some(true)
None => Some(true),
};
Ok(Config {
output: cli_config.output,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

use serde::{Deserialize, Serialize};

pub mod postgres;

/// The database the user wants to use.
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[serde(rename_all = "lowercase")]
Expand Down
35 changes: 0 additions & 35 deletions src/implementation/database/postgres.rs

This file was deleted.

20 changes: 10 additions & 10 deletions src/implementation/framework/golang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ use std::{collections::HashMap, path::Path};
use tera::{Result, Tera, Value};

use crate::{
implementation::Implementation,
model::{data_type::DataType, Model},
render::render_simple,
implementation::Implementation,
};

/// "imports" for different golang files
Expand All @@ -29,17 +29,17 @@ pub struct Golang {

// TODO: It is highly possible that each language needs `data_type`, `register` and `render`, maybe we can add a trait.
/// Get the string representing of a data_type in Golang.
fn data_type(data_type: DataType) -> String {
fn data_type(data_type: &DataType) -> String {
match data_type {
DataType::Int(x) if x <= 8 => "int8".to_string(),
DataType::Int(x) if x <= 16 => "int16".to_string(),
DataType::Int(x) if x <= 32 => "int32".to_string(),
DataType::Int(x) if *x <= 8 => "int8".to_string(),
DataType::Int(x) if *x <= 16 => "int16".to_string(),
DataType::Int(x) if *x <= 32 => "int32".to_string(),
DataType::Int(_) => "int64".to_string(),
DataType::UInt(x) if x <= 8 => "uint8".to_string(),
DataType::UInt(x) if x <= 16 => "uint16".to_string(),
DataType::UInt(x) if x <= 32 => "uint32".to_string(),
DataType::UInt(x) if *x <= 8 => "uint8".to_string(),
DataType::UInt(x) if *x <= 16 => "uint16".to_string(),
DataType::UInt(x) if *x <= 32 => "uint32".to_string(),
DataType::UInt(_) => "uint64".to_string(),
DataType::Float(x) if x <= 32 => "float32".to_string(),
DataType::Float(x) if *x <= 32 => "float32".to_string(),
DataType::Float(_) => "float64".to_string(),
DataType::String(_) => "string".to_string(),
DataType::DateTime => "time".to_string(),
Expand All @@ -48,7 +48,7 @@ fn data_type(data_type: DataType) -> String {

fn data_type_in_template(args: &HashMap<String, Value>) -> Result<Value> {
let data_type_value: DataType = serde_json::from_value(args.get("data_type").unwrap().clone())?;
Ok(Value::String(data_type(data_type_value)))
Ok(Value::String(data_type(&data_type_value)))
}

/// A function which can be used in the template for judging whether the datatype is a string
Expand Down
16 changes: 11 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use implementation::Implementation;

use crate::{
model::{data_type::DataType, Field, Model},
implementation::framework::golang,
model::{data_type::DataType, Field, Model},
};

mod config;
mod implementation;
mod model;
mod render;
mod implementation;
mod config;

fn main() {
let mut tera = render::load_templates();
Expand All @@ -23,11 +23,17 @@ fn main() {
name: "id".to_string(),
data_type: DataType::UInt(64),
},
fields: config.generate_config.api.unwrap().fields.into_iter()
fields: config
.generate_config
.api
.unwrap()
.fields
.into_iter()
.map(|it| Field {
name: it.name,
data_type: it.data_type.into(),
}).collect(),
})
.collect(),
};
let mut context = tera::Context::new();
context.insert("model", &model);
Expand Down
13 changes: 8 additions & 5 deletions src/model/data_type.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use convert_case::{Case, Casing};
use serde::{Deserialize, Serialize};
use toml::value::Datetime;

/// Meta information about a string.
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
Expand All @@ -27,11 +26,15 @@ impl From<String> for DataType {
match s.as_str().to_case(Case::Snake).as_str() {
"tiny_int" | "tinyint" | "tiny_integer" | "int8" => DataType::Int(8),
"small_int" | "smallint" | "small_integer" | "int16" => DataType::Int(16),
"int" | "integer" | "medium_int" | "mediumint" | "medium_integer" | "int32" => DataType::Int(32),
"int" | "integer" | "medium_int" | "mediumint" | "medium_integer" | "int32" => {
DataType::Int(32)
}
"big_int" | "bigint" | "big_integer" | "int64" => DataType::Int(64),
"unsigned_tiny_int" | "unsigned_tiny_integer" | "uint8" => DataType::UInt(8),
"unsigned_small_int" | "unsigned_small_integer" | "uint16" => DataType::UInt(16),
"uint" | "unsigned" | "unsigned_medium_int" | "unsigned_medium_integer" | "uint32" => DataType::UInt(32),
"uint" | "unsigned" | "unsigned_medium_int" | "unsigned_medium_integer" | "uint32" => {
DataType::UInt(32)
}
"unsigned_big_int" | "unsigned_big_integer" | "uint64" => DataType::UInt(64),
"float" | "float32" => DataType::Float(32),
"double" | "float64" => DataType::Float(64),
Expand All @@ -40,7 +43,7 @@ impl From<String> for DataType {
if s.starts_with("char(") {
let length: usize = s
.trim_start_matches("char(")
.trim_end_matches(")")
.trim_end_matches(')')
.parse()
.unwrap();
DataType::String(Some(StringMeta {
Expand All @@ -50,7 +53,7 @@ impl From<String> for DataType {
} else if s.starts_with("varchar(") {
let length: usize = s
.trim_start_matches("varchar(")
.trim_end_matches(")")
.trim_end_matches(')')
.parse()
.unwrap();
DataType::String(Some(StringMeta {
Expand Down

0 comments on commit d407618

Please sign in to comment.