Skip to content

Commit

Permalink
Rewrite deserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
andrzejressel committed Dec 17, 2024
1 parent f5a4681 commit 89d09ff
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 15 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions pulumi_wasm_generator_lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ syn.workspace = true
quote.workspace = true
proc-macro2.workspace = true
itertools.workspace = true
pulumi_wasm_provider_common.workspace = true

[dev-dependencies]
assert_cmd.workspace = true
23 changes: 23 additions & 0 deletions pulumi_wasm_generator_lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,20 @@ use std::io::{BufReader, Write};
use std::path::Path;

use crate::schema::Package;
use crate::schema_types::PulumiSchema;
use anyhow::{Context, Result};

mod code_generation;
mod description;
mod model;
mod output;
mod schema;
mod schema_types;
mod utils;

pub fn generate_rust_library(schema_json: &Path, result_path: &Path) -> Result<()> {
let schema_package: schema::Package = extract_schema_from_file(schema_json)?;
extract_schema_2_from_file(schema_json)?;
let package = schema::to_model(&schema_package)?;

fs::create_dir_all(result_path.join("wit").join("deps"))?;
Expand Down Expand Up @@ -82,6 +86,7 @@ pub fn generate_rust_library(schema_json: &Path, result_path: &Path) -> Result<(

pub fn generate_wasm_provider(schema_json: &Path, result_path: &Path) -> Result<()> {
let schema_package: schema::Package = extract_schema_from_file(schema_json)?;
extract_schema_2_from_file(schema_json)?;
let package = schema::to_model(&schema_package)?;

fs::create_dir_all(result_path.join("wit").join("deps"))?;
Expand Down Expand Up @@ -149,3 +154,21 @@ fn extract_schema_from_file(schema_json: &Path) -> anyhow::Result<Package> {
)),
}
}

fn extract_schema_2_from_file(schema_json: &Path) -> anyhow::Result<PulumiSchema> {
let file = File::open(schema_json)
.with_context(|| format!("Error opening schema file [{:?}]", schema_json))?;
let reader = BufReader::new(file);
match schema_json.extension().and_then(|s| s.to_str()) {
None => Err(anyhow::anyhow!(
"No extensions for schema file: {}.",
schema_json.display()
)),
Some("yml" | "yaml") => serde_yaml::from_reader(reader).map_err(anyhow::Error::new),
Some("json") => serde_json::from_reader(reader).map_err(anyhow::Error::new),
Some(ext) => Err(anyhow::anyhow!(
"Unsupported schema file extension: {}.",
ext
)),
}
}
20 changes: 5 additions & 15 deletions pulumi_wasm_generator_lib/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use convert_case::{Case, Casing};
use serde::Deserialize;
use std::collections::{BTreeMap, BTreeSet, HashSet};

type PulumiMap<T> = BTreeMap<String, T>;
pub(crate) type PulumiMap<T> = BTreeMap<String, T>;

#[derive(Deserialize, Debug)]
pub(crate) enum TypeEnum {
Expand Down Expand Up @@ -67,7 +67,7 @@ enum OneOfTypePrimitiveType {
}

#[derive(Deserialize, Debug)]
struct Property {
pub(crate) struct Property {
#[serde(flatten)]
r#type: Type,
}
Expand Down Expand Up @@ -103,21 +103,21 @@ enum ObjectTypeEnum {
}

#[derive(Deserialize, Debug)]
struct IntegerEnumValue {
pub(crate) struct IntegerEnumValue {
name: String,
description: Option<String>,
value: i64,
}

#[derive(Deserialize, Debug)]
struct NumberEnumValue {
pub(crate) struct NumberEnumValue {
name: String,
description: Option<String>,
value: f64,
}

#[derive(Deserialize, Debug)]
struct StringEnumValue {
pub(crate) struct StringEnumValue {
name: Option<String>,
description: Option<String>,
value: Option<String>,
Expand Down Expand Up @@ -150,16 +150,6 @@ pub(crate) struct Package {
functions: PulumiMap<Function>,
}

// fn complex_type_mapper(complex_type: ComplexType) -> Result<crate::model::Type> {
// //TODO: Enums
// object_type_mapper(complex_type.object_type)
// }
//
// fn object_type_mapper(object_type: ObjectType) -> Result<crate::model::Type> {
//
//
// }

//TODO: Fix formatting
fn new_type_mapper(type_: &Type) -> Result<crate::model::Type> {
(match type_ {
Expand Down
129 changes: 129 additions & 0 deletions pulumi_wasm_generator_lib/src/schema_types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
use crate::schema::IntegerEnumValue;
use crate::schema::NumberEnumValue;
use crate::schema::{PulumiMap, StringEnumValue};
use pulumi_wasm_provider_common;

Check warning

Code scanning / clippy

this import is redundant Warning

this import is redundant
use serde::Deserialize;
use std::collections::BTreeSet;

pulumi_wasm_provider_common::generate_string_const!(StringConstant, "string");
pulumi_wasm_provider_common::generate_string_const!(IntegerConstant, "integer");
pulumi_wasm_provider_common::generate_string_const!(NumberConstant, "number");
pulumi_wasm_provider_common::generate_string_const!(BooleanConstant, "boolean");
pulumi_wasm_provider_common::generate_string_const!(ArrayConstant, "array");
pulumi_wasm_provider_common::generate_string_const!(ObjectConstant, "object");

#[derive(Deserialize)]
pub(crate) struct PulumiSchema {

Check warning

Code scanning / clippy

field types is never read Warning

field types is never read
#[serde(default)]
types: PulumiMap<TopLevelType>,

Check warning

Code scanning / clippy

field types is never read Warning

field types is never read
}

#[derive(Deserialize)]
#[serde(untagged)]
enum TopLevelType {
Object(MyTypeObject),

Check warning

Code scanning / clippy

field 0 is never read Warning

field 0 is never read

Check warning

Code scanning / clippy

field 0 is never read Warning

field 0 is never read
StringEnum(StringEnumType),

Check warning

Code scanning / clippy

field 0 is never read Warning

field 0 is never read

Check warning

Code scanning / clippy

field 0 is never read Warning

field 0 is never read
IntegerEnum(IntegerEnumType),

Check warning

Code scanning / clippy

field 0 is never read Warning

field 0 is never read

Check warning

Code scanning / clippy

field 0 is never read Warning

field 0 is never read
NumberEnum(NumberEnumType),

Check warning

Code scanning / clippy

field 0 is never read Warning

field 0 is never read

Check warning

Code scanning / clippy

field 0 is never read Warning

field 0 is never read
}

#[derive(Deserialize)]
#[serde(untagged)]
enum InnerType {
StringEnum(StringEnumType),

Check warning

Code scanning / clippy

field 0 is never read Warning

field 0 is never read

Check warning

Code scanning / clippy

field 0 is never read Warning

field 0 is never read
IntegerEnum(IntegerEnumType),

Check warning

Code scanning / clippy

field 0 is never read Warning

field 0 is never read

Check warning

Code scanning / clippy

field 0 is never read Warning

field 0 is never read
NumberEnum(NumberEnumType),

Check warning

Code scanning / clippy

field 0 is never read Warning

field 0 is never read

Check warning

Code scanning / clippy

field 0 is never read Warning

field 0 is never read
Boolean(BooleanType),
AnyString(AnyStringType),
AnyNumber(AnyNumberType),
AnyInteger(AnyIntegerType),
HashMap(HashMapType),

Check warning

Code scanning / clippy

field 0 is never read Warning

field 0 is never read

Check warning

Code scanning / clippy

field 0 is never read Warning

field 0 is never read
Ref(RefType),

Check warning

Code scanning / clippy

field 0 is never read Warning

field 0 is never read

Check warning

Code scanning / clippy

field 0 is never read Warning

field 0 is never read
OneOf(OneOfType),

Check warning

Code scanning / clippy

field 0 is never read Warning

field 0 is never read

Check warning

Code scanning / clippy

field 0 is never read Warning

field 0 is never read
Array(ArrayType),

Check warning

Code scanning / clippy

field 0 is never read Warning

field 0 is never read

Check warning

Code scanning / clippy

field 0 is never read Warning

field 0 is never read
}

#[derive(Deserialize)]
struct MyTypeObject {

Check warning

Code scanning / clippy

fields type_, description, properties, and required are never read Warning

fields type\_, description, properties, and required are never read
#[serde(rename = "type")]
type_: ObjectConstant,

Check warning

Code scanning / clippy

fields type_, description, properties, and required are never read Warning

fields type\_, description, properties, and required are never read
description: Option<String>,

Check warning

Code scanning / clippy

fields type_, description, properties, and required are never read Warning

fields type\_, description, properties, and required are never read
properties: PulumiMap<InnerType>,

Check warning

Code scanning / clippy

fields type_, description, properties, and required are never read Warning

fields type\_, description, properties, and required are never read
#[serde(default)]
required: BTreeSet<String>,

Check warning

Code scanning / clippy

fields type_, description, properties, and required are never read Warning

fields type\_, description, properties, and required are never read
}

#[derive(Deserialize)]
struct StringEnumType {

Check warning

Code scanning / clippy

fields type_ and enum_ are never read Warning

fields type\_ and enum\_ are never read
#[serde(rename = "type")]
type_: StringConstant,

Check warning

Code scanning / clippy

fields type_ and enum_ are never read Warning

fields type\_ and enum\_ are never read
#[serde(rename = "enum")]
enum_: Vec<StringEnumValue>,

Check warning

Code scanning / clippy

fields type_ and enum_ are never read Warning

fields type\_ and enum\_ are never read
}

#[derive(Deserialize)]
struct IntegerEnumType {

Check warning

Code scanning / clippy

fields type_ and enum_ are never read Warning

fields type\_ and enum\_ are never read
#[serde(rename = "type")]
type_: IntegerConstant,

Check warning

Code scanning / clippy

fields type_ and enum_ are never read Warning

fields type\_ and enum\_ are never read
#[serde(rename = "enum")]
enum_: Vec<IntegerEnumValue>,

Check warning

Code scanning / clippy

fields type_ and enum_ are never read Warning

fields type\_ and enum\_ are never read
}

#[derive(Deserialize)]
struct NumberEnumType {

Check warning

Code scanning / clippy

fields type_ and enum_ are never read Warning

fields type\_ and enum\_ are never read
#[serde(rename = "type")]
type_: NumberConstant,

Check warning

Code scanning / clippy

fields type_ and enum_ are never read Warning

fields type\_ and enum\_ are never read
#[serde(rename = "enum")]
enum_: Vec<NumberEnumValue>,

Check warning

Code scanning / clippy

fields type_ and enum_ are never read Warning

fields type\_ and enum\_ are never read
}

#[derive(Deserialize)]
struct AnyStringType {

Check warning

Code scanning / clippy

field type_ is never read Warning

field type\_ is never read
#[serde(rename = "type")]
type_: StringConstant,

Check warning

Code scanning / clippy

field type_ is never read Warning

field type\_ is never read
}

#[derive(Deserialize)]
struct BooleanType {

Check warning

Code scanning / clippy

field type_ is never read Warning

field type\_ is never read
#[serde(rename = "type")]
type_: BooleanConstant,

Check warning

Code scanning / clippy

field type_ is never read Warning

field type\_ is never read
}

#[derive(Deserialize)]
struct AnyIntegerType {

Check warning

Code scanning / clippy

field type_ is never read Warning

field type\_ is never read
#[serde(rename = "type")]
type_: IntegerConstant,

Check warning

Code scanning / clippy

field type_ is never read Warning

field type\_ is never read
}

#[derive(Deserialize)]
struct AnyNumberType {

Check warning

Code scanning / clippy

field type_ is never read Warning

field type\_ is never read
#[serde(rename = "type")]
type_: NumberConstant,

Check warning

Code scanning / clippy

field type_ is never read Warning

field type\_ is never read
}

#[derive(Deserialize)]
struct RefType {

Check warning

Code scanning / clippy

field ref_ is never read Warning

field ref\_ is never read
#[serde(rename = "$ref")]
ref_: String,

Check warning

Code scanning / clippy

field ref_ is never read Warning

field ref\_ is never read
}

#[derive(Deserialize)]
struct OneOfType {

Check warning

Code scanning / clippy

field one_of is never read Warning

field one\_of is never read
#[serde(rename = "oneOf")]
one_of: Vec<InnerType>,

Check warning

Code scanning / clippy

field one_of is never read Warning

field one\_of is never read
}

#[derive(Deserialize)]
struct ArrayType {

Check warning

Code scanning / clippy

fields type_ and items are never read Warning

fields type\_ and items are never read
#[serde(rename = "type")]
type_: ArrayConstant,

Check warning

Code scanning / clippy

fields type_ and items are never read Warning

fields type\_ and items are never read
items: Box<InnerType>,

Check warning

Code scanning / clippy

fields type_ and items are never read Warning

fields type\_ and items are never read
}

#[derive(Deserialize)]
struct HashMapType {

Check warning

Code scanning / clippy

fields type_ and additional_properties are never read Warning

fields type\_ and additional\_properties are never read
#[serde(rename = "type")]
type_: ObjectConstant,

Check warning

Code scanning / clippy

fields type_ and additional_properties are never read Warning

fields type\_ and additional\_properties are never read
#[serde(rename = "additionalProperties")]
additional_properties: Box<InnerType>,

Check warning

Code scanning / clippy

fields type_ and additional_properties are never read Warning

fields type\_ and additional\_properties are never read
}

0 comments on commit 89d09ff

Please sign in to comment.