From 5b7caf0c068c7e176411bd2ae3a45771a55b9a01 Mon Sep 17 00:00:00 2001 From: Aumetra Weisman Date: Tue, 12 Nov 2024 15:12:00 +0100 Subject: [PATCH] Add some TypeScript snapshots --- packages/cw-schema-codegen/Cargo.toml | 1 + packages/cw-schema-codegen/build.rs | 3 + packages/cw-schema-codegen/src/go/template.rs | 36 +++++++++++- .../templates/go/enum.tpl.go | 2 + .../templates/go/struct.tpl.go | 21 +++++++ .../templates/typescript/enum.tpl.ts | 4 ++ .../snapshots/rust_tpl__complex_enum.snap | 7 ++- .../tests/snapshots/rust_tpl__empty_enum.snap | 3 + .../snapshots/rust_tpl__empty_struct.snap | 3 + .../snapshots/rust_tpl__named_struct.snap | 3 + .../snapshots/rust_tpl__simple_enum.snap | 3 + .../snapshots/rust_tpl__tuple_struct.snap | 9 ++- .../snapshots/typescript__codegen_snap-2.snap | 17 ++++++ .../snapshots/typescript__codegen_snap-3.snap | 17 ++++++ .../snapshots/typescript__codegen_snap-4.snap | 19 +++++++ .../snapshots/typescript__codegen_snap-5.snap | 55 +++++++++++++++++++ .../snapshots/typescript__codegen_snap.snap | 31 +++++++++++ .../cw-schema-codegen/tests/typescript.rs | 54 ++++++++++++++++++ 18 files changed, 278 insertions(+), 10 deletions(-) create mode 100644 packages/cw-schema-codegen/build.rs create mode 100644 packages/cw-schema-codegen/tests/snapshots/typescript__codegen_snap-2.snap create mode 100644 packages/cw-schema-codegen/tests/snapshots/typescript__codegen_snap-3.snap create mode 100644 packages/cw-schema-codegen/tests/snapshots/typescript__codegen_snap-4.snap create mode 100644 packages/cw-schema-codegen/tests/snapshots/typescript__codegen_snap-5.snap create mode 100644 packages/cw-schema-codegen/tests/snapshots/typescript__codegen_snap.snap create mode 100644 packages/cw-schema-codegen/tests/typescript.rs diff --git a/packages/cw-schema-codegen/Cargo.toml b/packages/cw-schema-codegen/Cargo.toml index 5acc60be60..afc6d6987f 100644 --- a/packages/cw-schema-codegen/Cargo.toml +++ b/packages/cw-schema-codegen/Cargo.toml @@ -4,6 +4,7 @@ authors = ["Aumetra Weisman "] edition = "2021" version.workspace = true publish = false +build = "build.rs" [dependencies] anyhow = "1.0.89" diff --git a/packages/cw-schema-codegen/build.rs b/packages/cw-schema-codegen/build.rs new file mode 100644 index 0000000000..73f98cda43 --- /dev/null +++ b/packages/cw-schema-codegen/build.rs @@ -0,0 +1,3 @@ +fn main() { + println!("cargo:rerun-if-changed=templates"); +} diff --git a/packages/cw-schema-codegen/src/go/template.rs b/packages/cw-schema-codegen/src/go/template.rs index ea807e3a2a..90c02e1b2d 100644 --- a/packages/cw-schema-codegen/src/go/template.rs +++ b/packages/cw-schema-codegen/src/go/template.rs @@ -1,9 +1,41 @@ use askama::Template; +use std::borrow::Cow; + +#[derive(Clone)] +pub struct EnumVariantTemplate<'a> { + pub name: Cow<'a, str>, + pub docs: Cow<'a, [Cow<'a, str>]>, + pub ty: TypeTemplate<'a>, +} #[derive(Template)] #[template(escape = "none", path = "go/enum.tpl.go")] -pub struct EnumTemplate {} +pub struct EnumTemplate<'a> { + pub name: Cow<'a, str>, + pub docs: Cow<'a, [Cow<'a, str>]>, + pub variants: Cow<'a, [EnumVariantTemplate<'a>]>, +} + +#[derive(Clone)] +pub struct FieldTemplate<'a> { + pub name: Cow<'a, str>, + pub docs: Cow<'a, [Cow<'a, str>]>, + pub ty: Cow<'a, str>, +} + +#[derive(Clone)] +pub enum TypeTemplate<'a> { + Unit, + Tuple(Cow<'a, [Cow<'a, str>]>), + Named { + fields: Cow<'a, [FieldTemplate<'a>]>, + }, +} #[derive(Template)] #[template(escape = "none", path = "go/struct.tpl.go")] -pub struct StructTemplate {} +pub struct StructTemplate<'a> { + pub name: Cow<'a, str>, + pub docs: Cow<'a, [Cow<'a, str>]>, + pub ty: TypeTemplate<'a>, +} diff --git a/packages/cw-schema-codegen/templates/go/enum.tpl.go b/packages/cw-schema-codegen/templates/go/enum.tpl.go index e69de29bb2..ec0d54cc88 100644 --- a/packages/cw-schema-codegen/templates/go/enum.tpl.go +++ b/packages/cw-schema-codegen/templates/go/enum.tpl.go @@ -0,0 +1,2 @@ +// This code is @generated by cw-schema-codegen. Do not modify this manually. + diff --git a/packages/cw-schema-codegen/templates/go/struct.tpl.go b/packages/cw-schema-codegen/templates/go/struct.tpl.go index e69de29bb2..48b420e7f3 100644 --- a/packages/cw-schema-codegen/templates/go/struct.tpl.go +++ b/packages/cw-schema-codegen/templates/go/struct.tpl.go @@ -0,0 +1,21 @@ +// This code is @generated by cw-schema-codegen. Do not modify this manually. + +package cwcodegen + +{% for doc in docs %} + // {{ doc }} +{% endfor %} +type {{ name }} struct { + {% match ty %} + {% when TypeTemplate::Unit %} + {% when TypeTemplate::Tuple with (types) %} + {{ todo!() }} + {% when TypeTemplate::Named with { fields } %} + {% for field in fields %} + {% for doc in docs %} + // {{ doc }} + {% endfor %} + {{ field.name|capitalize }} {{ field.ty }} `json:"{{ field.name }}"` + {% endfor %} + {% endmatch %} +} diff --git a/packages/cw-schema-codegen/templates/typescript/enum.tpl.ts b/packages/cw-schema-codegen/templates/typescript/enum.tpl.ts index f2b897ccf7..70c675385a 100644 --- a/packages/cw-schema-codegen/templates/typescript/enum.tpl.ts +++ b/packages/cw-schema-codegen/templates/typescript/enum.tpl.ts @@ -35,6 +35,10 @@ type {{ name }} = } } {% endmatch %} {% endfor %} + +{% if variants.len() == 0 %} + never; +{% endif %} ; export { {{ name }} }; diff --git a/packages/cw-schema-codegen/tests/snapshots/rust_tpl__complex_enum.snap b/packages/cw-schema-codegen/tests/snapshots/rust_tpl__complex_enum.snap index d219e94379..00c3725ff8 100644 --- a/packages/cw-schema-codegen/tests/snapshots/rust_tpl__complex_enum.snap +++ b/packages/cw-schema-codegen/tests/snapshots/rust_tpl__complex_enum.snap @@ -2,6 +2,9 @@ source: packages/cw-schema-codegen/tests/rust_tpl.rs expression: rendered --- +// This code is @generated by cw-schema-codegen. Do not modify this manually. + + #[doc = "Complex enum"] @@ -18,9 +21,7 @@ pub enum Complex { One ( - - u64, - + u64 ) , diff --git a/packages/cw-schema-codegen/tests/snapshots/rust_tpl__empty_enum.snap b/packages/cw-schema-codegen/tests/snapshots/rust_tpl__empty_enum.snap index 876c882b06..8e97ca0d4b 100644 --- a/packages/cw-schema-codegen/tests/snapshots/rust_tpl__empty_enum.snap +++ b/packages/cw-schema-codegen/tests/snapshots/rust_tpl__empty_enum.snap @@ -2,6 +2,9 @@ source: packages/cw-schema-codegen/tests/rust_tpl.rs expression: rendered --- +// This code is @generated by cw-schema-codegen. Do not modify this manually. + + #[doc = "Empty enum"] diff --git a/packages/cw-schema-codegen/tests/snapshots/rust_tpl__empty_struct.snap b/packages/cw-schema-codegen/tests/snapshots/rust_tpl__empty_struct.snap index 6766ff832b..be4cd9a49d 100644 --- a/packages/cw-schema-codegen/tests/snapshots/rust_tpl__empty_struct.snap +++ b/packages/cw-schema-codegen/tests/snapshots/rust_tpl__empty_struct.snap @@ -2,6 +2,9 @@ source: packages/cw-schema-codegen/tests/rust_tpl.rs expression: rendered --- +// This code is @generated by cw-schema-codegen. Do not modify this manually. + + #[doc = "Empty struct"] diff --git a/packages/cw-schema-codegen/tests/snapshots/rust_tpl__named_struct.snap b/packages/cw-schema-codegen/tests/snapshots/rust_tpl__named_struct.snap index f37ab2f6be..b6d3ba9548 100644 --- a/packages/cw-schema-codegen/tests/snapshots/rust_tpl__named_struct.snap +++ b/packages/cw-schema-codegen/tests/snapshots/rust_tpl__named_struct.snap @@ -2,6 +2,9 @@ source: packages/cw-schema-codegen/tests/rust_tpl.rs expression: rendered --- +// This code is @generated by cw-schema-codegen. Do not modify this manually. + + #[doc = "Named struct"] diff --git a/packages/cw-schema-codegen/tests/snapshots/rust_tpl__simple_enum.snap b/packages/cw-schema-codegen/tests/snapshots/rust_tpl__simple_enum.snap index c5d5dd0f9e..7d5b52b2e2 100644 --- a/packages/cw-schema-codegen/tests/snapshots/rust_tpl__simple_enum.snap +++ b/packages/cw-schema-codegen/tests/snapshots/rust_tpl__simple_enum.snap @@ -2,6 +2,9 @@ source: packages/cw-schema-codegen/tests/rust_tpl.rs expression: rendered --- +// This code is @generated by cw-schema-codegen. Do not modify this manually. + + #[doc = "Simple enum"] diff --git a/packages/cw-schema-codegen/tests/snapshots/rust_tpl__tuple_struct.snap b/packages/cw-schema-codegen/tests/snapshots/rust_tpl__tuple_struct.snap index 54d55450b7..90eff5f1c5 100644 --- a/packages/cw-schema-codegen/tests/snapshots/rust_tpl__tuple_struct.snap +++ b/packages/cw-schema-codegen/tests/snapshots/rust_tpl__tuple_struct.snap @@ -2,6 +2,9 @@ source: packages/cw-schema-codegen/tests/rust_tpl.rs expression: rendered --- +// This code is @generated by cw-schema-codegen. Do not modify this manually. + + #[doc = "Tuple struct"] @@ -10,9 +13,5 @@ pub struct Tuple ( - - u64, - - String, - + u64, String ); diff --git a/packages/cw-schema-codegen/tests/snapshots/typescript__codegen_snap-2.snap b/packages/cw-schema-codegen/tests/snapshots/typescript__codegen_snap-2.snap new file mode 100644 index 0000000000..7cfb7b4a73 --- /dev/null +++ b/packages/cw-schema-codegen/tests/snapshots/typescript__codegen_snap-2.snap @@ -0,0 +1,17 @@ +--- +source: packages/cw-schema-codegen/tests/typescript.rs +expression: output +--- +// This code is @generated by cw-schema-codegen. Do not modify this manually. + +/** + + */ + +type Uwu = + + [string, string] + +; + +export { Uwu }; diff --git a/packages/cw-schema-codegen/tests/snapshots/typescript__codegen_snap-3.snap b/packages/cw-schema-codegen/tests/snapshots/typescript__codegen_snap-3.snap new file mode 100644 index 0000000000..c13999ac21 --- /dev/null +++ b/packages/cw-schema-codegen/tests/snapshots/typescript__codegen_snap-3.snap @@ -0,0 +1,17 @@ +--- +source: packages/cw-schema-codegen/tests/typescript.rs +expression: output +--- +// This code is @generated by cw-schema-codegen. Do not modify this manually. + +/** + + */ + +type Òwó = + + void + +; + +export { Òwó }; diff --git a/packages/cw-schema-codegen/tests/snapshots/typescript__codegen_snap-4.snap b/packages/cw-schema-codegen/tests/snapshots/typescript__codegen_snap-4.snap new file mode 100644 index 0000000000..9f0cb760fa --- /dev/null +++ b/packages/cw-schema-codegen/tests/snapshots/typescript__codegen_snap-4.snap @@ -0,0 +1,19 @@ +--- +source: packages/cw-schema-codegen/tests/typescript.rs +expression: output +--- +// This code is @generated by cw-schema-codegen. Do not modify this manually. + +/** + + */ + +type Empty = + + + + never; + +; + +export { Empty }; diff --git a/packages/cw-schema-codegen/tests/snapshots/typescript__codegen_snap-5.snap b/packages/cw-schema-codegen/tests/snapshots/typescript__codegen_snap-5.snap new file mode 100644 index 0000000000..5bd7258ecc --- /dev/null +++ b/packages/cw-schema-codegen/tests/snapshots/typescript__codegen_snap-5.snap @@ -0,0 +1,55 @@ +--- +source: packages/cw-schema-codegen/tests/typescript.rs +expression: output +--- +// This code is @generated by cw-schema-codegen. Do not modify this manually. + +/** + + */ + +type Hehehe = + + | + + /** + + */ + + + { "A": {} } + + + | + + /** + + */ + + + { "B": [string] } + + + | + + /** + + */ + + + { "C": { + + /** + + */ + + field: string; + + } } + + + + +; + +export { Hehehe }; diff --git a/packages/cw-schema-codegen/tests/snapshots/typescript__codegen_snap.snap b/packages/cw-schema-codegen/tests/snapshots/typescript__codegen_snap.snap new file mode 100644 index 0000000000..b36d30ea35 --- /dev/null +++ b/packages/cw-schema-codegen/tests/snapshots/typescript__codegen_snap.snap @@ -0,0 +1,31 @@ +--- +source: packages/cw-schema-codegen/tests/typescript.rs +expression: output +--- +// This code is @generated by cw-schema-codegen. Do not modify this manually. + +/** + + */ + +type Owo = + + { + + /** + + */ + + field_1: string; + + /** + + */ + + field_2: string; + + } + +; + +export { Owo }; diff --git a/packages/cw-schema-codegen/tests/typescript.rs b/packages/cw-schema-codegen/tests/typescript.rs new file mode 100644 index 0000000000..11cb4fff48 --- /dev/null +++ b/packages/cw-schema-codegen/tests/typescript.rs @@ -0,0 +1,54 @@ +use cw_schema::Schemaifier; + +#[derive(Schemaifier)] +struct Owo { + field_1: u32, + field_2: String, +} + +#[derive(Schemaifier)] +struct Uwu(String, u32); + +#[derive(Schemaifier)] +struct Òwó; + +#[derive(Schemaifier)] +enum Empty {} + +#[derive(Schemaifier)] +enum Hehehe { + A, + B(u32), + C { field: String }, +} + +#[test] +fn codegen_snap() { + // generate the schemas for each of the above types + let schemas = [ + cw_schema::schema_of::(), + cw_schema::schema_of::(), + cw_schema::schema_of::<Òwó>(), + cw_schema::schema_of::(), + cw_schema::schema_of::(), + ]; + + // run the codegen to typescript + for schema in schemas { + let cw_schema::Schema::V1(schema) = schema else { + panic!(); + }; + + let output = schema + .definitions + .iter() + .map(|node| { + let mut buf = Vec::new(); + cw_schema_codegen::typescript::process_node(&mut buf, &schema, node).unwrap(); + String::from_utf8(buf).unwrap() + }) + .collect::(); + + insta::assert_snapshot!(output); + } +}