Skip to content

Commit

Permalink
restructure code. RsSerde able to overwrite bigint and decimal types.…
Browse files Browse the repository at this point in the history
… Also convert field_name to snake for rust fields
  • Loading branch information
shuoli84 committed Jul 25, 2023
1 parent 40e28e0 commit 74cda74
Show file tree
Hide file tree
Showing 11 changed files with 604 additions and 480 deletions.
19 changes: 10 additions & 9 deletions codegen/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ struct Args {
long,
help = "root folder for all spec, will scan all specs in the folder recursively"
)]
input: Option<std::path::PathBuf>,
input: Option<PathBuf>,

#[arg(
long,
help = "root folder for all spec, will scan all specs in the folder recursively, deprecated, use --input instead"
)]
spec_folder: Option<std::path::PathBuf>,
spec_folder: Option<PathBuf>,

#[arg(short, long, default_value = "rs_serde")]
codegen: String,
Expand All @@ -32,22 +32,23 @@ struct Args {
default_value = "examples/spec/",
help = "output path, if input is folder, then output must be folder"
)]
output: std::path::PathBuf,
output: PathBuf,
}

fn main() -> anyhow::Result<()> {
let args = Args::parse();

let input = args.input.or(args.spec_folder).unwrap();

let codegen: Box<dyn Codegen> = match args.codegen.as_str() {
"rs_serde" => Box::new(RsSerde::default()),
"java_jackson" => Box::new(JavaJackson::default()),
"swift_codable" => Box::new(SwiftCodable::default()),
"py_dataclass" => Box::new(PyDataclass::default()),
"swagger" => Box::new(Swagger::default()),
"rs_serde" => Box::new(RsSerde::load_from_folder(&input)?),
"java_jackson" => Box::new(JavaJackson::load_from_folder(&input)?),
"swift_codable" => Box::new(SwiftCodable::load_from_folder(&input)?),
"py_dataclass" => Box::new(PyDataclass::load_from_folder(&input)?),
"swagger" => Box::new(Swagger::load_from_folder(&input)?),
_ => anyhow::bail!("unknown codegen name"),
};

let input = args.input.or(args.spec_folder).unwrap();
let output = absolute(&args.output);

// create output folder
Expand Down
31 changes: 30 additions & 1 deletion tot_spec/src/codegen/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,35 @@ impl Context {
&self.folder_tree
}

/// helper to load codegen specific config from spec_config.yaml
pub fn load_codegen_config<T: serde::de::DeserializeOwned>(
&self,
key_name: &str,
) -> anyhow::Result<Option<T>> {
let config_file = self.root_folder().join("spec_config.yaml");
if !config_file.exists() {
return Ok(None);
}

let config_content = std::fs::read_to_string(config_file)
.map_err(|_| anyhow::anyhow!("not able to read spec_config.yaml from folder"))?;
let config_value =
serde_yaml::from_str::<serde_json::Map<String, serde_json::Value>>(&config_content)?;
let Some(codegen_value) = config_value.get("codegen") else {
return Ok(None)
};

assert!(codegen_value.is_object());

let Some(config_value) = codegen_value.as_object().unwrap().get(key_name) else {
return Ok(None)
};

let config = serde_json::from_value::<T>(config_value.to_owned())?;

Ok(Some(config))
}

/// get a ref to definition for spec path, the spec should already loaded
/// panic if path not loaded
pub fn get_definition(&self, path: impl AsRef<Path>) -> anyhow::Result<&Definition> {
Expand Down Expand Up @@ -261,7 +290,7 @@ impl Context {
// skip example validate for virtual model
}
crate::ModelType::NewType { inner_type } => {
return self.validate_value_for_type(&value, &inner_type.0, true, spec)
return self.validate_value_for_type(&value, &inner_type.as_ref().0, true, spec)
}
crate::ModelType::Const { .. } => {
// skip validate for const
Expand Down
5 changes: 5 additions & 0 deletions tot_spec/src/codegen/fixtures/specs/spec_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ style:
- ignore_styles/**/*.yaml

codegen:
# globally overwrite type
rs_serde:
type_overwrites:
bigint: tot_spec_util::big_int::BigInt

swagger:
title: "swagger test"
description: "testing"
Expand Down
4 changes: 4 additions & 0 deletions tot_spec/src/codegen/java_jackson.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ use std::{borrow::Cow, fmt::Write, path::PathBuf};
pub struct JavaJackson {}

impl super::Codegen for JavaJackson {
fn load_from_folder(_folder: &PathBuf) -> anyhow::Result<Self> {
Ok(Self::default())
}

fn generate_for_folder(&self, folder: &PathBuf, output: &PathBuf) -> anyhow::Result<()> {
let context = Context::new_from_folder(folder)?;

Expand Down
4 changes: 4 additions & 0 deletions tot_spec/src/codegen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,9 @@ pub mod swift_codable;
pub mod utils;

pub trait Codegen {
fn load_from_folder(folder: &PathBuf) -> anyhow::Result<Self>
where
Self: Sized;

fn generate_for_folder(&self, folder: &PathBuf, output: &PathBuf) -> anyhow::Result<()>;
}
4 changes: 4 additions & 0 deletions tot_spec/src/codegen/py_dataclass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ use super::utils::{indent, multiline_prefix_with};
pub struct PyDataclass {}

impl super::Codegen for PyDataclass {
fn load_from_folder(_folder: &PathBuf) -> anyhow::Result<Self> {
Ok(Self::default())
}

fn generate_for_folder(&self, folder: &PathBuf, output: &PathBuf) -> anyhow::Result<()> {
let context = Context::new_from_folder(folder)?;

Expand Down
Loading

0 comments on commit 74cda74

Please sign in to comment.