Skip to content

Commit

Permalink
fairly significant refactor. aimed at supporting deepl
Browse files Browse the repository at this point in the history
  • Loading branch information
kurtbuilds committed Apr 12, 2024
1 parent 2c762c2 commit e18380f
Show file tree
Hide file tree
Showing 33 changed files with 3,355 additions and 890 deletions.
13 changes: 11 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 6 additions & 11 deletions Justfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
set positional-arguments
set dotenv-load := true
set dotenv-load
set export

help:
@just --list --unsorted
Expand Down Expand Up @@ -81,9 +82,6 @@ go:
checkexec ${CARGO_TARGET_DIR:-target}/debug/ocg $(fd . ocg/template) -- cargo clean --package ocg
cargo run -- gen --name PetStore --output-dir gen/petstore-go --generator go spec/petstore.yaml --github libninjacom/petstore-go --version 0.1.0

create:
bash ocg/script/create.sh

generate:
#!/bin/bash -euxo pipefail
if [ -n "${LIBRARY:-}" ]; then
Expand All @@ -97,22 +95,22 @@ generate:

test *ARGS:
checkexec commercial -- just dummy_commercial
cargo test
cargo test -- "$ARGS"
alias t := test

integration *ARGS:
cd libninja && cargo test -F integration -- "$@"
alias int := integration

# Test the library we just generated
test-lib:
test_lib:
#!/bin/bash -euxo pipefail
REPO_DIR=$DIR/$(basename $REPO)
cd $REPO_DIR
just bootstrap
just check
just test
alias tt := test-lib
alias tt := test_lib

clean-gen:
#!/bin/bash -euxo pipefail
Expand All @@ -125,14 +123,11 @@ clean-gen:
delete *ARG:
gh repo delete $REPO {{ARG}}

push:
bash ocg/script/push.sh

commercial:
rm -rf commercial
git clone https://github.com/kurtbuilds/libninja-commercial commercial

# Create a dummy commercial repo that lets the workspace work
# without the commericial code
dummy_commercial:
cargo new --lib commercial --name libninja_commercial
cargo new --lib commercial --name libninja_commercial
4 changes: 3 additions & 1 deletion core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[lib]
name = "ln_core"
name = "libninja_core"

[dependencies]
anyhow = "1.0.71"
Expand All @@ -24,6 +24,8 @@ tera = "1.19.0"
libninja_hir = { path = "../hir" }
tracing = "0.1.40"
tracing-ez = "0.3.0"
regex-lite = "0.1.5"

[dev-dependencies]
serde_yaml = "0.9.25"
http = "1.1.0"
12 changes: 12 additions & 0 deletions core/Justfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

run:
cargo run

test *ARGS:
cargo test -- $(ARGS)

build:
cargo build

install:
cargo install --path .
234 changes: 132 additions & 102 deletions core/src/child_schemas.rs
Original file line number Diff line number Diff line change
@@ -1,107 +1,137 @@
use std::collections::HashMap;
use std::sync::atomic::AtomicBool;
use openapiv3::{OpenAPI, Operation, RequestBody, Response, Schema, SchemaKind, Type};
// pub trait ChildSchemas {
// fn add_child_schemas<'a>(&'a self, acc: &mut HashMap<String, &'a Schema>);
// }

pub trait ChildSchemas {
fn add_child_schemas<'a>(&'a self, acc: &mut HashMap<String, &'a Schema>);
}
// impl ChildSchemas for Schema {
// fn add_child_schemas<'a>(&'a self, acc: &mut HashMap<String, &'a Schema>) {
// match &self.kind {
// SchemaKind::Type(Type::Array(a)) => {
// let Some(items) = &a.items else {
// return;
// };
// let Some(item) = items.as_item() else {
// return;
// };
// if let Some(title) = &item.title {
// let title = sanitize(title).to_string();
// acc.entry(title).or_insert(item);
// }
// item.add_child_schemas(acc);
// }
// SchemaKind::Type(Type::Object(o)) => {
// if let Some(title) = &self.title {
// let title = sanitize(title).to_string();
// acc.entry(title).or_insert(self);
// }
// for (_name, prop) in &o.properties {
// let Some(prop) = prop.as_item() else {
// continue;
// };
// if let Some(title) = &prop.title {
// let title = sanitize(title).to_string();
// acc.entry(title).or_insert(prop);
// }
// prop.add_child_schemas(acc);
// }
// }
// SchemaKind::Type(_) => {}
// SchemaKind::OneOf { one_of: schemas }
// | SchemaKind::AllOf { all_of: schemas }
// | SchemaKind::AnyOf { any_of: schemas } => {
// for schema in schemas {
// let Some(schema) = schema.as_item() else {
// continue;
// };
// if let Some(title) = &schema.title {
// let title = sanitize(title).to_string();
// acc.entry(title).or_insert(schema);
// }
// schema.add_child_schemas(acc);
// }
// }
// SchemaKind::Not { .. } => {}
// SchemaKind::Any(_) => {}
// }
// }
// }

impl ChildSchemas for Schema {
fn add_child_schemas<'a>(&'a self, acc: &mut HashMap<String, &'a Schema>) {
match &self.kind {
SchemaKind::Type(Type::Array(a)) => {
let Some(items) = &a.items else { return; };
let Some(item) = items.as_item() else { return; };
if let Some(title) = &item.title {
acc.entry(title.clone()).or_insert(item);
}
item.add_child_schemas(acc);
}
SchemaKind::Type(Type::Object(o)) => {
if let Some(title) = &self.title {
acc.entry(title.clone()).or_insert(self);
}
for (_name, prop) in &o.properties {
let Some(prop) = prop.as_item() else { continue; };
if let Some(title) = &prop.title {
acc.entry(title.clone()).or_insert(prop);
}
prop.add_child_schemas(acc);
}
}
SchemaKind::Type(_) => {}
| SchemaKind::OneOf { one_of: schemas }
| SchemaKind::AllOf { all_of: schemas }
| SchemaKind::AnyOf { any_of: schemas} => {
for schema in schemas {
let Some(schema) = schema.as_item() else { continue; };
if let Some(title) = &schema.title {
acc.entry(title.clone()).or_insert(schema);
}
schema.add_child_schemas(acc);
}
}
SchemaKind::Not { .. } => {}
SchemaKind::Any(_) => {}
}
}
}
// impl ChildSchemas for Operation {
// fn add_child_schemas<'a>(&'a self, acc: &mut HashMap<String, &'a Schema>) {
// 'body: {
// let Some(body) = &self.request_body else {
// break 'body;
// };
// let Some(body) = body.as_item() else {
// break 'body;
// };
// body.add_child_schemas(acc);
// }
// for par in &self.parameters {
// let Some(par) = par.as_item() else {
// continue;
// };
// let Some(schema) = par.data.schema() else {
// continue;
// };
// let Some(schema) = schema.as_item() else {
// continue;
// };
// schema.add_child_schemas(acc);
// }
// for (_code, response) in &self.responses.responses {
// let Some(response) = response.as_item() else {
// continue;
// };
// response.add_child_schemas(acc);
// }
// }
// }

impl ChildSchemas for Operation {
fn add_child_schemas<'a>(&'a self, acc: &mut HashMap<String, &'a Schema>) {
'body: {
let Some(body) = &self.request_body else { break 'body; };
let Some(body) = body.as_item() else { break 'body; };
body.add_child_schemas(acc);
}
for par in &self.parameters {
let Some(par) = par.as_item() else { continue; };
let Some(schema) = par.data.schema() else { continue; };
let Some(schema) = schema.as_item() else { continue; };
schema.add_child_schemas(acc);
}
for (_code, response) in &self.responses.responses {
let Some(response) = response.as_item() else { continue; };
response.add_child_schemas(acc);
}
}
}
// impl ChildSchemas for RequestBody {
// fn add_child_schemas<'a>(&'a self, acc: &mut HashMap<String, &'a Schema>) {
// for (_key, content) in &self.content {
// let Some(schema) = &content.schema else {
// continue;
// };
// let Some(schema) = schema.as_item() else {
// continue;
// };
// if let Some(title) = &schema.title {
// acc.entry(title.clone()).or_insert(schema);
// }
// schema.add_child_schemas(acc);
// }
// }
// }

impl ChildSchemas for RequestBody {
fn add_child_schemas<'a>(&'a self, acc: &mut HashMap<String, &'a Schema>) {
for (_key, content) in &self.content {
let Some(schema) = &content.schema else { continue; };
let Some(schema) = schema.as_item() else { continue; };
if let Some(title) = &schema.title {
acc.entry(title.clone()).or_insert(schema);
}
schema.add_child_schemas(acc);
}
}
}
// impl ChildSchemas for Response {
// fn add_child_schemas<'a>(&'a self, acc: &mut HashMap<String, &'a Schema>) {
// for (k, content) in &self.content {
// let Some(schema) = &content.schema else {
// continue;
// };
// let Some(schema) = schema.as_item() else {
// continue;
// };
// if let Some(title) = &schema.title {
// acc.entry(title.clone()).or_insert(schema);
// }
// schema.add_child_schemas(acc);
// }
// }
// }

impl ChildSchemas for Response {
fn add_child_schemas<'a>(&'a self, acc: &mut HashMap<String, &'a Schema>) {
for (k, content) in &self.content {
let Some(schema) = &content.schema else { continue; };
let Some(schema) = schema.as_item() else { continue; };
if let Some(title) = &schema.title {
acc.entry(title.clone()).or_insert(schema);
}
schema.add_child_schemas(acc);
}
}
}

impl ChildSchemas for OpenAPI {
fn add_child_schemas<'a>(&'a self, acc: &mut HashMap<String, &'a Schema>) {
for (_path, _method, op, _item) in self.operations() {
op.add_child_schemas(acc);
}
for (name, schema) in &self.schemas {
let Some(schema) = schema.as_item() else { continue; };
acc.entry(name.clone()).or_insert(schema);
schema.add_child_schemas(acc);
}
}
}
// impl ChildSchemas for OpenAPI {
// fn add_child_schemas<'a>(&'a self, acc: &mut HashMap<String, &'a Schema>) {
// for (_path, _method, op, _item) in self.operations() {
// op.add_child_schemas(acc);
// }
// for (name, schema) in &self.schemas {
// let Some(schema) = schema.as_item() else {
// continue;
// };
// acc.entry(name.clone()).or_insert(schema);
// schema.add_child_schemas(acc);
// }
// }
// }
Loading

0 comments on commit e18380f

Please sign in to comment.