Skip to content

Commit 3068946

Browse files
authored
Add registry schema (#89)
Signed-off-by: Teo Koon Peng <[email protected]>
1 parent eefce3a commit 3068946

File tree

5 files changed

+380
-24
lines changed

5 files changed

+380
-24
lines changed

registry.schema.json

Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
{
2+
"$schema": "https://json-schema.org/draft/2020-12/schema",
3+
"title": "DiagramElementRegistry",
4+
"type": "object",
5+
"properties": {
6+
"messages": {
7+
"type": "object",
8+
"additionalProperties": {
9+
"$ref": "#/$defs/MessageRegistration"
10+
}
11+
},
12+
"nodes": {
13+
"type": "object",
14+
"additionalProperties": {
15+
"$ref": "#/$defs/NodeRegistration"
16+
}
17+
},
18+
"schemas": {
19+
"type": "object",
20+
"additionalProperties": true
21+
},
22+
"sections": {
23+
"type": "object",
24+
"additionalProperties": {
25+
"$ref": "#/$defs/SectionRegistration"
26+
}
27+
}
28+
},
29+
"required": [
30+
"nodes",
31+
"sections",
32+
"messages",
33+
"schemas"
34+
],
35+
"$defs": {
36+
"MessageOperation": {
37+
"type": "object",
38+
"properties": {
39+
"deserialize": {
40+
"type": [
41+
"object",
42+
"null"
43+
]
44+
},
45+
"fork_clone": {
46+
"type": [
47+
"object",
48+
"null"
49+
]
50+
},
51+
"fork_result": {
52+
"type": [
53+
"object",
54+
"null"
55+
]
56+
},
57+
"join": {
58+
"type": [
59+
"object",
60+
"null"
61+
]
62+
},
63+
"serialize": {
64+
"type": [
65+
"object",
66+
"null"
67+
]
68+
},
69+
"split": {
70+
"type": [
71+
"object",
72+
"null"
73+
]
74+
},
75+
"unzip": {
76+
"type": [
77+
"array",
78+
"null"
79+
],
80+
"items": {
81+
"type": "string"
82+
}
83+
}
84+
}
85+
},
86+
"MessageRegistration": {
87+
"type": "object",
88+
"properties": {
89+
"operations": {
90+
"$ref": "#/$defs/MessageOperation"
91+
},
92+
"schema": {
93+
"anyOf": [
94+
{
95+
"$ref": "#/$defs/Schema"
96+
},
97+
{
98+
"type": "null"
99+
}
100+
]
101+
},
102+
"type_name": {
103+
"type": "string"
104+
}
105+
},
106+
"required": [
107+
"type_name",
108+
"operations"
109+
]
110+
},
111+
"NodeRegistration": {
112+
"type": "object",
113+
"properties": {
114+
"$key$": {
115+
"type": "string"
116+
},
117+
"config_schema": {
118+
"$ref": "#/$defs/Schema"
119+
},
120+
"name": {
121+
"type": "string"
122+
},
123+
"request": {
124+
"type": "string"
125+
},
126+
"response": {
127+
"type": "string"
128+
}
129+
},
130+
"required": [
131+
"$key$",
132+
"name",
133+
"request",
134+
"response",
135+
"config_schema"
136+
]
137+
},
138+
"Schema": {
139+
"type": [
140+
"object",
141+
"boolean"
142+
]
143+
},
144+
"SectionBuffer": {
145+
"type": "object",
146+
"properties": {
147+
"item_type": {
148+
"type": [
149+
"string",
150+
"null"
151+
]
152+
}
153+
}
154+
},
155+
"SectionInput": {
156+
"type": "object",
157+
"properties": {
158+
"message_type": {
159+
"type": "string"
160+
}
161+
},
162+
"required": [
163+
"message_type"
164+
]
165+
},
166+
"SectionMetadata": {
167+
"type": "object",
168+
"properties": {
169+
"buffers": {
170+
"type": "object",
171+
"additionalProperties": {
172+
"$ref": "#/$defs/SectionBuffer"
173+
}
174+
},
175+
"inputs": {
176+
"type": "object",
177+
"additionalProperties": {
178+
"$ref": "#/$defs/SectionInput"
179+
}
180+
},
181+
"outputs": {
182+
"type": "object",
183+
"additionalProperties": {
184+
"$ref": "#/$defs/SectionOutput"
185+
}
186+
}
187+
},
188+
"required": [
189+
"inputs",
190+
"outputs",
191+
"buffers"
192+
]
193+
},
194+
"SectionOutput": {
195+
"type": "object",
196+
"properties": {
197+
"message_type": {
198+
"type": "string"
199+
}
200+
},
201+
"required": [
202+
"message_type"
203+
]
204+
},
205+
"SectionRegistration": {
206+
"type": "object",
207+
"properties": {
208+
"config_schema": {
209+
"$ref": "#/$defs/Schema"
210+
},
211+
"metadata": {
212+
"$ref": "#/$defs/SectionMetadata"
213+
},
214+
"name": {
215+
"type": "string"
216+
}
217+
},
218+
"required": [
219+
"name",
220+
"metadata",
221+
"config_schema"
222+
]
223+
}
224+
}
225+
}

src/diagram/generate_schema.rs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use bevy_impulse::Diagram;
1+
use bevy_impulse::{Diagram, DiagramElementRegistry};
22

33
fn main() -> Result<(), Box<dyn std::error::Error>> {
44
let schema = schemars::schema_for!(Diagram);
@@ -9,6 +9,16 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
99
.open("diagram.schema.json")
1010
.unwrap();
1111
serde_json::to_writer_pretty(f, &schema)?;
12+
13+
let schema = schemars::schema_for!(DiagramElementRegistry);
14+
let f = std::fs::OpenOptions::new()
15+
.write(true)
16+
.truncate(true)
17+
.create(true)
18+
.open("registry.schema.json")
19+
.unwrap();
20+
serde_json::to_writer_pretty(f, &schema)?;
21+
1222
Ok(())
1323
}
1424

@@ -20,7 +30,7 @@ mod diagram {
2030

2131
#[cfg(not(target_os = "windows"))]
2232
#[test]
23-
fn check_schema_changes() -> Result<(), String> {
33+
fn check_diagram_schema_changes() -> Result<(), String> {
2434
let cur_schema_json = std::fs::read("diagram.schema.json").unwrap();
2535
let schema = schemars::schema_for!(Diagram);
2636
let new_schema_json = serde_json::to_vec_pretty(&schema).unwrap();
@@ -32,5 +42,20 @@ mod diagram {
3242
}
3343
Ok(())
3444
}
45+
46+
#[cfg(not(target_os = "windows"))]
47+
#[test]
48+
fn check_registry_schema_changes() -> Result<(), String> {
49+
let cur_schema_json = std::fs::read("registry.schema.json").unwrap();
50+
let schema = schemars::schema_for!(DiagramElementRegistry);
51+
let new_schema_json = serde_json::to_vec_pretty(&schema).unwrap();
52+
53+
if cur_schema_json.len() != new_schema_json.len()
54+
|| zip(cur_schema_json, new_schema_json).any(|(a, b)| a != b)
55+
{
56+
return Err(String::from("There are changes in the json schema, please run `cargo run -F=diagram generate_schema` to regenerate it"));
57+
}
58+
Ok(())
59+
}
3560
}
3661
}

0 commit comments

Comments
 (0)