feat(codegen): implemented oneOf unions for primitive types
This commit is contained in:
parent
549654f138
commit
70fb568230
8 changed files with 380 additions and 129 deletions
|
|
@ -1,12 +1,6 @@
|
|||
use std::{env, fs, path::Path};
|
||||
|
||||
use proc_macro2::TokenStream;
|
||||
use torn_api_codegen::{
|
||||
model::{parameter::Parameter, path::Path as ApiPath, resolve, scope::Scope},
|
||||
openapi::schema::OpenApiSchema,
|
||||
};
|
||||
|
||||
const DENY_LIST: &[&str] = &[];
|
||||
use torn_api_codegen::{model::ResolvedSchema, openapi::schema::OpenApiSchema};
|
||||
|
||||
fn main() {
|
||||
let out_dir = env::var_os("OUT_DIR").unwrap();
|
||||
|
|
@ -17,60 +11,21 @@ fn main() {
|
|||
|
||||
let s = include_str!("./openapi.json");
|
||||
let schema: OpenApiSchema = serde_json::from_str(s).unwrap();
|
||||
let resolved = ResolvedSchema::from_open_api(&schema);
|
||||
|
||||
let mut models_code = TokenStream::new();
|
||||
|
||||
for (name, model) in &schema.components.schemas {
|
||||
if DENY_LIST.contains(name) {
|
||||
continue;
|
||||
}
|
||||
let model = resolve(model, name, &schema.components.schemas);
|
||||
if let Some(new_code) = model.codegen() {
|
||||
models_code.extend(new_code);
|
||||
}
|
||||
}
|
||||
|
||||
let models_file = syn::parse2(models_code).unwrap();
|
||||
let models_file = syn::parse2(resolved.codegen_models()).unwrap();
|
||||
let models_pretty = prettyplease::unparse(&models_file);
|
||||
fs::write(&model_dest, models_pretty).unwrap();
|
||||
|
||||
let mut params_code = TokenStream::new();
|
||||
|
||||
for (name, param) in &schema.components.parameters {
|
||||
if let Some(code) = Parameter::from_schema(name, param).unwrap().codegen() {
|
||||
params_code.extend(code);
|
||||
}
|
||||
}
|
||||
|
||||
let params_file = syn::parse2(params_code).unwrap();
|
||||
let params_file = syn::parse2(resolved.codegen_parameters()).unwrap();
|
||||
let params_pretty = prettyplease::unparse(¶ms_file);
|
||||
fs::write(¶ms_dest, params_pretty).unwrap();
|
||||
|
||||
let mut requests_code = TokenStream::new();
|
||||
let mut paths = Vec::new();
|
||||
for (name, path) in &schema.paths {
|
||||
let Some(path) = ApiPath::from_schema(name, path, &schema.components.parameters) else {
|
||||
continue;
|
||||
};
|
||||
if let Some(code) = path.codegen_request() {
|
||||
requests_code.extend(code);
|
||||
}
|
||||
paths.push(path);
|
||||
}
|
||||
|
||||
let requests_file = syn::parse2(requests_code).unwrap();
|
||||
let requests_file = syn::parse2(resolved.codegen_requests()).unwrap();
|
||||
let requests_pretty = prettyplease::unparse(&requests_file);
|
||||
fs::write(&requests_dest, requests_pretty).unwrap();
|
||||
|
||||
let mut scope_code = TokenStream::new();
|
||||
let scopes = Scope::from_paths(paths);
|
||||
for scope in scopes {
|
||||
if let Some(code) = scope.codegen() {
|
||||
scope_code.extend(code);
|
||||
}
|
||||
}
|
||||
|
||||
let scopes_file = syn::parse2(scope_code).unwrap();
|
||||
let scopes_file = syn::parse2(resolved.codegen_scopes()).unwrap();
|
||||
let scopes_pretty = prettyplease::unparse(&scopes_file);
|
||||
fs::write(&scopes_dest, scopes_pretty).unwrap();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,8 +9,8 @@ pub(super) mod test {
|
|||
use crate::{
|
||||
executor::{ExecutorExt, ReqwestClient},
|
||||
models::{
|
||||
AttackCode, FactionSelectionName, PersonalStatsCategoryEnum, PersonalStatsStatName,
|
||||
UserListEnum,
|
||||
faction_selection_name::FactionSelectionNameVariant, AttackCode, FactionSelectionName,
|
||||
PersonalStatsCategoryEnum, PersonalStatsStatName, UserListEnum,
|
||||
},
|
||||
};
|
||||
|
||||
|
|
@ -67,7 +67,10 @@ pub(super) mod test {
|
|||
let r = client
|
||||
.faction()
|
||||
.for_selections(|b| {
|
||||
b.selections([FactionSelectionName::Basic, FactionSelectionName::Balance])
|
||||
b.selections([
|
||||
FactionSelectionNameVariant::Basic,
|
||||
FactionSelectionNameVariant::Balance,
|
||||
])
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
|
|
@ -424,7 +427,7 @@ pub(super) mod test {
|
|||
let forum_scope = ForumScope(&client);
|
||||
|
||||
forum_scope
|
||||
.threads_for_category_ids([2.into()].into(), |b| b)
|
||||
.threads_for_category_ids([2].into(), |b| b)
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
|
@ -495,14 +498,14 @@ pub(super) mod test {
|
|||
racing_scope.carupgrades(|b| b).await.unwrap();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
/* #[tokio::test]
|
||||
async fn racing_races() {
|
||||
let client = test_client().await;
|
||||
|
||||
let racing_scope = RacingScope(&client);
|
||||
|
||||
racing_scope.races(|b| b).await.unwrap();
|
||||
}
|
||||
} */
|
||||
|
||||
#[tokio::test]
|
||||
async fn racing_race_for_race_id() {
|
||||
|
|
@ -648,10 +651,7 @@ pub(super) mod test {
|
|||
|
||||
let torn_scope = TornScope(&client);
|
||||
|
||||
torn_scope
|
||||
.items_for_ids([1.into()].into(), |b| b)
|
||||
.await
|
||||
.unwrap();
|
||||
torn_scope.items_for_ids([1].into(), |b| b).await.unwrap();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue