Skip to content

Commit dcbc887

Browse files
committed
Fix TypeScript codegen
1 parent 126b2b7 commit dcbc887

File tree

9 files changed

+43
-32
lines changed

9 files changed

+43
-32
lines changed

packages/cw-schema-codegen/src/typescript/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ fn expand_node_name<'a>(
1818
cw_schema::NodeType::Double => "z.number()".into(),
1919
cw_schema::NodeType::Boolean => "z.boolean()".into(),
2020
cw_schema::NodeType::String => "z.string()".into(),
21-
cw_schema::NodeType::Integer { .. } => "z.string()".into(),
21+
cw_schema::NodeType::Integer { .. } => "z.string().or(z.number())".into(),
2222
cw_schema::NodeType::Binary => "z.instanceof(Uint8Array)".into(),
2323
cw_schema::NodeType::Optional { inner } => {
2424
let inner = &schema.definitions[inner];

packages/cw-schema-codegen/templates/typescript/enum.tpl.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const {{ name }}Schema = z.union([
2020

2121
{% match variant.ty %}
2222
{% when TypeTemplate::Unit %}
23-
z.object({ "{{ variant.name }}": z.void() }),
23+
z.object({ "{{ variant.name }}": z.null() }).or(z.literal("{{ variant.name }}")),
2424
{% when TypeTemplate::Tuple with (types) %}
2525
z.object({ "{{ variant.name }}": z.tuple([{{ types|join(", ") }}]) }),
2626
{% when TypeTemplate::Named with { fields } %}

packages/cw-schema-codegen/templates/typescript/struct.tpl.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { z } from 'zod';
1313
const {{ name }}Schema =
1414
{% match ty %}
1515
{% when TypeTemplate::Unit %}
16-
z.void()
16+
z.null()
1717
{% when TypeTemplate::Tuple with (types) %}
1818
z.tuple([{{ types|join(", ") }}])
1919
{% when TypeTemplate::Named with { fields } %}

packages/cw-schema-codegen/tests/snapshots/typescript__codegen_snap-2.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { z } from 'zod';
1414

1515
const UwuSchema =
1616

17-
z.tuple([z.string(), z.string()])
17+
z.tuple([z.string(), z.string().or(z.number())])
1818

1919
;
2020

packages/cw-schema-codegen/tests/snapshots/typescript__codegen_snap-3.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { z } from 'zod';
1414

1515
const ÒwóSchema =
1616

17-
z.void()
17+
z.null()
1818

1919
;
2020

packages/cw-schema-codegen/tests/snapshots/typescript__codegen_snap-5.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ const HeheheSchema = z.union([
1919
*/
2020

2121

22-
z.object({ "A": z.void() }),
22+
z.object({ "A": z.null() }).or(z.literal("A")),
2323

2424

2525
/**
2626
2727
*/
2828

2929

30-
z.object({ "B": z.tuple([z.string()]) }),
30+
z.object({ "B": z.tuple([z.string().or(z.number())]) }),
3131

3232

3333
/**

packages/cw-schema-codegen/tests/snapshots/typescript__codegen_snap.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const OwoSchema =
2020
2121
*/
2222

23-
field_1: z.string(),
23+
field_1: z.string().or(z.number()),
2424

2525
/**
2626

packages/cw-schema-codegen/tests/ts-e2e/src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ const typeName = process.argv[2];
1414
const deserialized = JSON.parse(stdinString);
1515

1616
let validated = gen[typeName].parse(deserialized);
17+
console.error(stdinString);
18+
console.error(deserialized);
19+
console.error(validated);
1720

1821
const outputStream = process.stdout;
1922
outputStream.write(JSON.stringify(validated));

packages/cw-schema-codegen/tests/typescript.rs

Lines changed: 32 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use arbitrary::Arbitrary;
2+
use core::str;
23
use cw_schema::Schemaifier;
34
use serde::{Deserialize, Serialize};
45
use std::{
@@ -19,15 +20,8 @@ struct Uwu(String, u32);
1920
#[derive(Arbitrary, Schemaifier, Debug, Deserialize, PartialEq, Serialize)]
2021
struct Òwó;
2122

22-
mod empty {
23-
#![allow(unreachable_code)]
24-
use super::*;
25-
26-
#[derive(Arbitrary, Schemaifier, Debug, Deserialize, PartialEq, Serialize)]
27-
pub enum Empty {}
28-
}
29-
30-
use self::empty::Empty;
23+
#[derive(Schemaifier, Debug, Deserialize, PartialEq, Serialize)]
24+
pub enum Empty {}
3125

3226
#[derive(Arbitrary, Schemaifier, Debug, Deserialize, PartialEq, Serialize)]
3327
enum Hehehe {
@@ -84,8 +78,7 @@ fn codegen_snap() {
8478
.iter()
8579
.map(|node| {
8680
let mut buf = Vec::new();
87-
cw_schema_codegen::typescript::process_node(&mut buf, &schema, node, true)
88-
.unwrap();
81+
cw_schema_codegen::typescript::process_node(&mut buf, &schema, node, true).unwrap();
8982
String::from_utf8(buf).unwrap()
9083
})
9184
.collect::<String>();
@@ -124,18 +117,30 @@ fn assert_validity() {
124117
wrap::<Òwó>,
125118
type_name::<Òwó>(),
126119
),
127-
(
120+
// `Empty` is a non-constructable type
121+
/*(
128122
cw_schema::schema_of::<Empty>(),
129123
wrap::<Empty>,
130124
type_name::<Empty>(),
131-
),
125+
),*/
132126
(
133127
cw_schema::schema_of::<Hehehe>(),
134128
wrap::<Hehehe>,
135129
type_name::<Hehehe>(),
136130
),
137131
];
138132

133+
let e2e_dir = format!("{}/tests/ts-e2e", env!("CARGO_MANIFEST_DIR"));
134+
let gen_file_path = format!("{}/src/gen.ts", e2e_dir);
135+
136+
// make sure the dependencies are installed
137+
let install_status = Command::new("npm")
138+
.arg("i")
139+
.current_dir(&e2e_dir)
140+
.status()
141+
.unwrap();
142+
assert!(install_status.success());
143+
139144
let random_data: [u8; 255] = rand::random();
140145
let mut unstructured = arbitrary::Unstructured::new(&random_data);
141146
for (schema, arbitrary_gen, type_name) in schemas {
@@ -148,26 +153,20 @@ fn assert_validity() {
148153
.iter()
149154
.map(|node| {
150155
let mut buf = Vec::new();
151-
cw_schema_codegen::typescript::process_node(&mut buf, schema, node, true)
152-
.unwrap();
156+
cw_schema_codegen::typescript::process_node(&mut buf, schema, node, true).unwrap();
153157
String::from_utf8(buf).unwrap()
154158
})
155159
.collect::<String>();
156160

157-
let e2e_dir = format!("{}/tests/ts-e2e", env!("CARGO_MANIFEST_DIR"));
158-
let gen_file_path = format!("{}/src/gen.ts", e2e_dir);
159-
let mut gen_file = File::create(gen_file_path).unwrap();
161+
let mut gen_file = File::create(&gen_file_path).unwrap();
160162
gen_file.write_all(output.as_bytes()).unwrap();
161163

162164
let data = arbitrary_gen(&mut unstructured);
163165
let serialized = serde_json::to_string(&data).unwrap();
164166

165-
let install_status = Command::new("npm").arg("i").current_dir(&e2e_dir).status().unwrap();
166-
assert!(install_status.success());
167-
168167
let mut child = Command::new("npm")
169168
.args(["test", type_name])
170-
.current_dir(e2e_dir)
169+
.current_dir(&e2e_dir)
171170
.stdin(Stdio::piped())
172171
.stdout(Stdio::piped())
173172
.spawn()
@@ -178,8 +177,17 @@ fn assert_validity() {
178177
stdin.write_all(serialized.as_bytes()).unwrap();
179178
}
180179

181-
let output = child.wait_with_output().unwrap();
182-
let deserialized: Combined = serde_json::from_slice(&output.stdout).unwrap();
180+
let proc_output = child.wait_with_output().unwrap();
181+
assert!(
182+
proc_output.status.success(),
183+
"failed with object: {data:#?}; json: {serialized}; schema: {output}"
184+
);
185+
186+
let stdout = str::from_utf8(&proc_output.stdout).unwrap();
187+
let stdout = stdout.lines().last().unwrap();
188+
let deserialized: Combined = serde_json::from_str(stdout).unwrap_or_else(|err| {
189+
panic!("{err:?}; input: {serialized}, output: {stdout}");
190+
});
183191

184192
assert_eq!(data, deserialized);
185193
}

0 commit comments

Comments
 (0)