Skip to content

Commit

Permalink
Add some TypeScript snapshots
Browse files Browse the repository at this point in the history
  • Loading branch information
aumetra committed Nov 12, 2024
1 parent 08d06c4 commit 460850a
Show file tree
Hide file tree
Showing 18 changed files with 278 additions and 10 deletions.
1 change: 1 addition & 0 deletions packages/cw-schema-codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ authors = ["Aumetra Weisman <aumetra@confio.gmbh>"]
edition = "2021"
version.workspace = true
publish = false
build = "build.rs"

[dependencies]
anyhow = "1.0.89"
Expand Down
3 changes: 3 additions & 0 deletions packages/cw-schema-codegen/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
println!("cargo:rerun-if-changed=templates");
}
36 changes: 34 additions & 2 deletions packages/cw-schema-codegen/src/go/template.rs
Original file line number Diff line number Diff line change
@@ -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>,
}
2 changes: 2 additions & 0 deletions packages/cw-schema-codegen/templates/go/enum.tpl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// This code is @generated by cw-schema-codegen. Do not modify this manually.

21 changes: 21 additions & 0 deletions packages/cw-schema-codegen/templates/go/struct.tpl.go
Original file line number Diff line number Diff line change
@@ -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 %}
}
4 changes: 4 additions & 0 deletions packages/cw-schema-codegen/templates/typescript/enum.tpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ type {{ name }} =
} }
{% endmatch %}
{% endfor %}

{% if variants.len() == 0 %}
never;
{% endif %}
;

export { {{ name }} };
Original file line number Diff line number Diff line change
Expand Up @@ -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"]


Expand All @@ -18,9 +21,7 @@ pub enum Complex {
One

(

u64,

u64
)

,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"]


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"]


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"]


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"]


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"]


Expand All @@ -10,9 +13,5 @@ pub struct Tuple


(

u64,

String,

u64, String
);
Original file line number Diff line number Diff line change
@@ -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 };
Original file line number Diff line number Diff line change
@@ -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ó };
Original file line number Diff line number Diff line change
@@ -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 };
Original file line number Diff line number Diff line change
@@ -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 };
Original file line number Diff line number Diff line change
@@ -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 };
54 changes: 54 additions & 0 deletions packages/cw-schema-codegen/tests/typescript.rs
Original file line number Diff line number Diff line change
@@ -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() {
// generate the schemas for each of the above types
let schemas = [
cw_schema::schema_of::<Owo>(),
cw_schema::schema_of::<Uwu>(),
cw_schema::schema_of::<Òwó>(),
cw_schema::schema_of::<Empty>(),
cw_schema::schema_of::<Hehehe>(),
];

// 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::<String>();

insta::assert_snapshot!(output);
}
}

0 comments on commit 460850a

Please sign in to comment.