Skip to content

Commit

Permalink
feat(core): handle JoinType serde using SCREAMING_SNAKE_CASE and `s…
Browse files Browse the repository at this point in the history
…nake_case` style (#760)

* use `SCREAMING_SNAKE_CASE` to handle the serde of join type

* modifiy for real case tests

* cargo fmt and clippy
  • Loading branch information
goldmedal committed Sep 4, 2024
1 parent 8be4f00 commit 8a29eee
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
32 changes: 32 additions & 0 deletions wren-modeling-rs/core/src/mdl/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,38 @@ mod test {
assert_eq!(actual, expected)
}

#[test]
fn test_join_type_case_insensitive() {
let case = ["one_to_one", "ONE_TO_ONE"];
let expected = JoinType::OneToOne;
for case in case.iter() {
assert_serde(&format!("\"{}\"", case), expected);
}

let case = ["one_to_many", "ONE_TO_MANY"];
let expected = JoinType::OneToMany;
for case in case.iter() {
assert_serde(&format!("\"{}\"", case), expected);
}

let case = ["many_to_one", "MANY_TO_ONE"];
let expected = JoinType::ManyToOne;
for case in case.iter() {
assert_serde(&format!("\"{}\"", case), expected);
}

let case = ["many_to_many", "MANY_TO_MANY"];
let expected = JoinType::ManyToMany;
for case in case.iter() {
assert_serde(&format!("\"{}\"", case), expected);
}
}

fn assert_serde(json_str: &str, expected: JoinType) {
let actual: JoinType = serde_json::from_str(json_str).unwrap();
assert_eq!(actual, expected);
}

#[test]
fn test_metric_roundtrip() {
let model = MetricBuilder::new("test")
Expand Down
7 changes: 5 additions & 2 deletions wren-modeling-rs/core/src/mdl/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,16 @@ pub struct Relationship {
pub properties: BTreeMap<String, String>,
}

// handle case insensitive
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Hash, Clone, Copy)]
#[serde(rename_all = "snake_case")]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum JoinType {
#[serde(alias = "one_to_one")]
OneToOne,
#[serde(alias = "one_to_many")]
OneToMany,
#[serde(alias = "many_to_one")]
ManyToOne,
#[serde(alias = "many_to_many")]
ManyToMany,
}

Expand Down
4 changes: 2 additions & 2 deletions wren-modeling-rs/core/tests/data/mdl.json
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,13 @@
{
"name": "CustomerOrders",
"models": ["customer", "orders"],
"joinType": "one_to_many",
"joinType": "ONE_TO_MANY",
"condition": "customer.custkey = orders.custkey"
},
{
"name" : "CustomerProfile",
"models": ["customer", "profile"],
"joinType": "one_to_one",
"joinType": "ONE_TO_ONE",
"condition": "customer.custkey = profile.custkey"
}
],
Expand Down

0 comments on commit 8a29eee

Please sign in to comment.