Skip to content

Commit

Permalink
add checks for json schema changes (#19)
Browse files Browse the repository at this point in the history
Signed-off-by: Teo Koon Peng <teokoonpeng@gmail.com>
  • Loading branch information
koonpeng authored Aug 1, 2024
1 parent 9c2e096 commit 6002465
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ jobs:
rosdep update
rosdep resolve -q ament_cmake std_msgs example_interfaces | sed '/^#/d' | xargs sudo apt install -y
- name: cargo test
run: . /ros_entrypoint.sh && cargo test
run: . /ros_entrypoint.sh && cargo test --all-features
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ name = "redf"
version = "0.1.0"
edition = "2021"
default-run = "redf"
license = "Apache-2.0"
license-file = "LICENSE"

[[bin]]
Expand Down
4 changes: 2 additions & 2 deletions redf.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
"description",
"message_type",
"title",
"topic",
"topic_name",
"type"
],
"properties": {
Expand All @@ -101,7 +101,7 @@
"title": {
"type": "string"
},
"topic": {
"topic_name": {
"type": "string"
},
"type": {
Expand Down
28 changes: 27 additions & 1 deletion src/generate-schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,32 @@ use schemars::schema_for;

fn main() -> Result<(), Box<dyn std::error::Error>> {
let schema = schema_for!(Redf);
std::fs::write("redf.schema.json", serde_json::to_string_pretty(&schema)?)?;
let f = std::fs::OpenOptions::new()
.write(true)
.truncate(true)
.create(true)
.open("redf.schema.json")
.unwrap();
serde_json::to_writer_pretty(f, &schema).unwrap();
Ok(())
}

#[cfg(test)]
mod test {
use super::*;
use std::iter::zip;

#[test]
fn check_schema_changes() -> Result<(), String> {
let cur_schema_json = std::fs::read("redf.schema.json").unwrap();
let schema = schema_for!(Redf);
let new_schema_json = serde_json::to_vec_pretty(&schema).unwrap();

if cur_schema_json.len() != new_schema_json.len()
|| zip(cur_schema_json, new_schema_json).any(|(a, b)| a != b)
{
return Err(String::from("There are changes in the json schema, please run `cargo run -F json_schema --bin generate-schema` to regenerate it"));
}
Ok(())
}
}

0 comments on commit 6002465

Please sign in to comment.