-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
83861f2
commit df71d3f
Showing
19 changed files
with
662 additions
and
1,018 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
use crate::code_generation::rust_generation::generate_code; | ||
use crate::code_generation::yaml::model::yaml_to_model; | ||
use crate::code_generation::yaml::yaml_model::YamlFile; | ||
use anyhow::anyhow; | ||
use anyhow::Context; | ||
use std::panic; | ||
pub(crate) mod rust_generation; | ||
mod tests; | ||
pub(crate) mod yaml; | ||
|
||
pub fn generate_code_from_string( | ||
yaml: String, | ||
package: &crate::model::Package, | ||
) -> anyhow::Result<String> { | ||
let yaml_file = | ||
YamlFile::from_yaml(yaml.as_str()).context(format!("Failed to parse YAML: {}", yaml))?; | ||
let example = panic::catch_unwind(|| yaml_to_model(yaml_file, package.name.clone(), package)) | ||
.map_err(|_| anyhow!("Failed to convert YAML to model")) | ||
.context(format!("Failed to convert YAML {} to model", yaml))?; | ||
generate_code(example) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#[cfg(test)] | ||
mod tests { | ||
use crate::code_generation::yaml::model::yaml_to_model; | ||
use crate::code_generation::yaml::tests::*; | ||
use crate::code_generation::YamlFile; | ||
use crate::{extract_schema_from_file, schema}; | ||
|
||
use crate::code_generation::rust_generation::generate_code; | ||
use crate::code_generation::yaml::tests::{ | ||
example_array, example_empty_properties, example_escape_string, example_numbers, | ||
}; | ||
|
||
macro_rules! yaml_deserialization_test { | ||
($test_name:ident, $test_module:ident) => { | ||
#[test] | ||
fn $test_name() { | ||
let yaml_file = YamlFile::from_yaml($test_module::YAML).unwrap(); | ||
let expected_yaml_file = $test_module::get_yaml_file(); | ||
assert_eq!(yaml_file, expected_yaml_file); | ||
} | ||
}; | ||
} | ||
|
||
yaml_deserialization_test!(generate_yaml_complex_yaml, complex_yaml); | ||
|
||
macro_rules! full_pipeline_test { | ||
($test_name:ident, $package_name:expr, $test_module:ident) => { | ||
#[test] | ||
fn $test_name() { | ||
let yaml_file = YamlFile::from_yaml($test_module::YAML).unwrap(); | ||
let expected_yaml_file = $test_module::get_yaml_file(); | ||
assert_eq!(yaml_file, expected_yaml_file); | ||
|
||
let schema_package: schema::Package = extract_schema_from_file( | ||
concat!("test_cases/", $package_name, ".json").as_ref(), | ||
) | ||
.unwrap(); | ||
let package = schema::to_model(&schema_package).unwrap(); | ||
let yaml_file = $test_module::get_yaml_file(); | ||
let result = yaml_to_model(yaml_file, $package_name.to_string(), &package); | ||
assert_eq!(result, $test_module::get_model()); | ||
|
||
let model = $test_module::get_model(); | ||
let code = generate_code(model).unwrap(); | ||
assert_eq!($test_module::get_rust_code(), code); | ||
} | ||
}; | ||
} | ||
|
||
full_pipeline_test!(full_pipeline_example_array, "cloudflare", example_array); | ||
full_pipeline_test!( | ||
full_pipeline_example_empty_properties, | ||
"cloudflare", | ||
example_empty_properties | ||
); | ||
full_pipeline_test!( | ||
full_pipeline_example_escape_string, | ||
"cloudflare", | ||
example_escape_string | ||
); | ||
full_pipeline_test!(full_pipeline_example_numbers, "cloudflare", example_numbers); | ||
full_pipeline_test!( | ||
full_pipeline_example_variable, | ||
"cloudflare", | ||
example_variable | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
pub(crate) mod model; | ||
#[cfg(test)] | ||
pub(crate) mod tests; | ||
pub(crate) mod yaml_model; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
pulumi_wasm_generator_lib/src/code_generation/yaml/tests/complex_yaml.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
use crate::code_generation::YamlFile; | ||
|
||
//language=YAML | ||
pub const YAML: &str = r#" | ||
resources: | ||
petstoreSchema: | ||
type: cloudflare:ApiShieldSchema | ||
name: petstore_schema | ||
properties: | ||
zoneId: 0da42c8d2132a9ddaf714f9e7c920711 | ||
name: myschema | ||
kind: openapi_v3 | ||
validationEnabled: true | ||
source: | ||
fn::invoke: | ||
Function: std:file | ||
Arguments: | ||
input: ./schemas/petstore.json | ||
Return: result | ||
"#; | ||
|
||
pub fn get_yaml_file() -> YamlFile { | ||
use crate::code_generation::yaml::yaml_model::{YamlExpression, YamlFile, YamlResource}; | ||
use std::collections::BTreeMap; | ||
|
||
YamlFile { | ||
resources: { | ||
let mut resources = BTreeMap::new(); | ||
resources.insert( | ||
"petstoreSchema".to_string(), | ||
YamlResource { | ||
type_: "cloudflare:ApiShieldSchema".to_string(), | ||
name: Some("petstore_schema".to_string()), | ||
properties: { | ||
let mut properties = BTreeMap::new(); | ||
properties.insert( | ||
"zoneId".to_string(), | ||
YamlExpression::String("0da42c8d2132a9ddaf714f9e7c920711".to_string()), | ||
); | ||
properties.insert( | ||
"name".to_string(), | ||
YamlExpression::String("myschema".to_string()), | ||
); | ||
properties.insert( | ||
"kind".to_string(), | ||
YamlExpression::String("openapi_v3".to_string()), | ||
); | ||
properties.insert( | ||
"validationEnabled".to_string(), | ||
YamlExpression::Boolean(true), | ||
); | ||
properties.insert( | ||
"source".to_string(), | ||
YamlExpression::Object({ | ||
let mut source = BTreeMap::new(); | ||
source.insert( | ||
"fn::invoke".to_string(), | ||
YamlExpression::Object({ | ||
let mut fn_invoke = BTreeMap::new(); | ||
fn_invoke.insert( | ||
"Function".to_string(), | ||
YamlExpression::String("std:file".to_string()), | ||
); | ||
fn_invoke.insert( | ||
"Arguments".to_string(), | ||
YamlExpression::Object({ | ||
let mut arguments = BTreeMap::new(); | ||
arguments.insert( | ||
"input".to_string(), | ||
YamlExpression::String( | ||
"./schemas/petstore.json".to_string(), | ||
), | ||
); | ||
arguments | ||
}), | ||
); | ||
fn_invoke.insert( | ||
"Return".to_string(), | ||
YamlExpression::String("result".to_string()), | ||
); | ||
fn_invoke | ||
}), | ||
); | ||
source | ||
}), | ||
); | ||
properties | ||
}, | ||
}, | ||
); | ||
resources | ||
}, | ||
} | ||
} |
Oops, something went wrong.