Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for additional model field types and arrays #1167

Closed
Closed
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ fs-err = "2.11.0"
# mailer
tera = "1.19.1"
thousands = "0.2.0"
heck = "0.4.0"
heck = { workspace = true }
cruet = "0.13.0"
lettre = { version = "0.11.4", default-features = false, features = [
"builder",
Expand Down Expand Up @@ -180,6 +180,7 @@ tower-http = { version = "0.6.1", features = [
"set-header",
"compression-full",
] }
heck = "0.4.0"

[dependencies.sea-orm-migration]
optional = true
Expand Down
1 change: 1 addition & 0 deletions loco-gen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ regex = { workspace = true }
tracing = { workspace = true }
chrono = { workspace = true }
colored = { workspace = true }
heck = { workspace = true }

clap = { version = "4.4.7", features = ["derive"] }
duct = "0.13"
Expand Down
216 changes: 195 additions & 21 deletions loco-gen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use serde_json::{json, Value};
mod controller;
use colored::Colorize;
use std::{
collections::HashMap,
fs,
path::{Path, PathBuf},
str::FromStr,
Expand Down Expand Up @@ -64,55 +65,106 @@ pub type Result<T> = std::result::Result<T, Error>;
#[derive(Serialize, Deserialize, Debug)]
struct FieldType {
name: String,
rust: Option<String>,
schema: Option<String>,
col_type: Option<String>,
rust: RustType,
schema: String,
col_type: String,
#[serde(default)]
arity: usize,
}

#[derive(Debug, Deserialize, Serialize)]
#[serde(untagged)]
enum RustType {
String(String),
Map(HashMap<String, String>),
}

#[derive(Serialize, Deserialize, Debug)]
struct Mappings {
field_types: Vec<FieldType>,
}
impl Mappings {
pub fn rust_field(&self, field: &str) -> Option<&String> {
fn error_unrecognized_default_field(&self, field: &str) -> Error {
Self::error_unrecognized(field, &self.schema_fields())
}
fn error_unrecognized(field: &str, allow_fields: &[&String]) -> Error {
Error::Message(format!(
"type: `{}` not found. try any of: `{}`",
field,
allow_fields
.iter()
.map(|&s| s.to_string())
.collect::<Vec<String>>()
.join(",")
))
}
pub fn rust_field_with_params(&self, field: &str, params: &Vec<String>) -> Result<&str> {
match field {
"array" | "array^" | "array!" => {
if let RustType::Map(ref map) = self.rust_field_kind(field)? {
if let [single] = params.as_slice() {
let keys: Vec<&String> = map.keys().collect();
Ok(map
.get(single)
.ok_or_else(|| Self::error_unrecognized(field, &keys))?)
} else {
Err(self.error_unrecognized_default_field(field))
}
} else {
panic!("array field should configured as array")
}
}

_ => self.rust_field(field),
}
}

pub fn rust_field_kind(&self, field: &str) -> Result<&RustType> {
self.field_types
.iter()
.find(|f| f.name == field)
.and_then(|f| f.rust.as_ref())
.map(|f| &f.rust)
.ok_or_else(|| self.error_unrecognized_default_field(field))
}
pub fn schema_field(&self, field: &str) -> Option<&String> {

pub fn rust_field(&self, field: &str) -> Result<&str> {
self.field_types
.iter()
.find(|f| f.name == field)
.and_then(|f| f.schema.as_ref())
.map(|f| &f.rust)
.ok_or_else(|| self.error_unrecognized_default_field(field))
.and_then(|rust_type| match rust_type {
RustType::String(s) => Ok(s),
RustType::Map(_) => Err(Error::Message(format!(
"type `{field}` need params to get the rust field type"
))),
})
.map(std::string::String::as_str)
}
pub fn col_type_field(&self, field: &str) -> Option<&String> {

pub fn schema_field(&self, field: &str) -> Result<&str> {
self.field_types
.iter()
.find(|f| f.name == field)
.and_then(|f| f.col_type.as_ref())
.map(|f| f.schema.as_str())
.ok_or_else(|| self.error_unrecognized_default_field(field))
}
pub fn col_type_arity(&self, field: &str) -> Option<usize> {
pub fn col_type_field(&self, field: &str) -> Result<&str> {
self.field_types
.iter()
.find(|f| f.name == field)
.map(|f| f.arity)
.map(|f| f.col_type.as_str())
.ok_or_else(|| self.error_unrecognized_default_field(field))
}
pub fn schema_fields(&self) -> Vec<&String> {
pub fn col_type_arity(&self, field: &str) -> Result<usize> {
self.field_types
.iter()
.filter(|f| f.schema.is_some())
.map(|f| &f.name)
.collect::<Vec<_>>()
.find(|f| f.name == field)
.map(|f| f.arity)
.ok_or_else(|| self.error_unrecognized_default_field(field))
}
pub fn rust_fields(&self) -> Vec<&String> {
self.field_types
.iter()
.filter(|f| f.rust.is_some())
.map(|f| &f.name)
.collect::<Vec<_>>()
pub fn schema_fields(&self) -> Vec<&String> {
self.field_types.iter().map(|f| &f.name).collect::<Vec<_>>()
}
}

Expand Down Expand Up @@ -482,4 +534,126 @@ mod tests {
);
}
}

fn test_mapping() -> Mappings {
Mappings {
field_types: vec![
FieldType {
name: "array".to_string(),
rust: RustType::Map(HashMap::from([
("string".to_string(), "Vec<String>".to_string()),
("chat".to_string(), "Vec<String>".to_string()),
("int".to_string(), "Vec<i32>".to_string()),
])),
schema: "array".to_string(),
col_type: "array_null".to_string(),
arity: 1,
},
FieldType {
name: "string^".to_string(),
rust: RustType::String("String".to_string()),
schema: "string_uniq".to_string(),
col_type: "StringUniq".to_string(),
arity: 0,
},
],
}
}

#[test]
fn can_get_schema_fields_from_mapping() {
let mapping = test_mapping();
assert_eq!(
mapping.schema_fields(),
Vec::from([&"array".to_string(), &"string^".to_string()])
);
}

#[test]
fn can_get_col_type_arity_from_mapping() {
let mapping = test_mapping();

assert_eq!(mapping.col_type_arity("array").expect("Get array arity"), 1);
assert_eq!(
mapping
.col_type_arity("string^")
.expect("Get string^ arity"),
0
);

assert!(mapping.col_type_arity("unknown").is_err());
}

#[test]
fn can_get_col_type_field_from_mapping() {
let mapping = test_mapping();

assert_eq!(
mapping.col_type_field("array").expect("Get array field"),
"array_null"
);

assert!(mapping.col_type_field("unknown").is_err());
}

#[test]
fn can_get_schema_field_from_mapping() {
let mapping = test_mapping();

assert_eq!(
mapping.schema_field("string^").expect("Get string^ schema"),
"string_uniq"
);

assert!(mapping.schema_field("unknown").is_err());
}

#[test]
fn can_get_rust_field_from_mapping() {
let mapping = test_mapping();

assert_eq!(
mapping
.rust_field("string^")
.expect("Get string^ rust field"),
"String"
);

assert!(mapping.rust_field("array").is_err());

assert!(mapping.rust_field("unknown").is_err(),);
}

#[test]
fn can_get_rust_field_kind_from_mapping() {
let mapping = test_mapping();

assert!(mapping.rust_field_kind("string^").is_ok());

assert!(mapping.rust_field_kind("unknown").is_err(),);
}

#[test]
fn can_get_rust_field_with_params_from_mapping() {
let mapping = test_mapping();

assert_eq!(
mapping
.rust_field_with_params("string^", &vec!["string".to_string()])
.expect("Get string^ rust field"),
"String"
);

assert_eq!(
mapping
.rust_field_with_params("array", &vec!["string".to_string()])
.expect("Get string^ rust field"),
"Vec<String>"
);
assert!(mapping
.rust_field_with_params("array", &vec!["unknown".to_string()])
.is_err());

assert!(mapping.rust_field_with_params("unknown", &vec![]).is_err());
}
}
Loading
Loading