Skip to content

Commit

Permalink
Merge pull request #15 from Jkutkut/2-a-way-to-check-if-a-ctx-variabl…
Browse files Browse the repository at this point in the history
…e-exists

feat: exists now available for objects by null checking
  • Loading branch information
Jkutkut authored Jun 10, 2024
2 parents a7fc685 + c483b33 commit 16b448a
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "osmia"
version = "1.12.1"
version = "1.13.0"
edition = "2021"
authors = ["Jkutkut"]

Expand Down
18 changes: 9 additions & 9 deletions src/model/ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,13 @@ impl Ctx {
let mut current_key = keys.next().unwrap();
loop {
match var {
JsonTree::Object(ref map) =>
var = Self::visit_obj(current_key, map)?,
JsonTree::Object(ref map) => var = match Self::visit_obj(current_key, map)? {
Some(v) => v,
None => return match keys.next() {
Some(_) => Err(format!("Key {} not found in object", current_key)),
None => Ok(&JsonTree::Null)
}
},
JsonTree::Array(ref array) =>
var = Self::visit_arr(current_key, array)?,
_ => return Err(
Expand Down Expand Up @@ -129,19 +134,14 @@ impl Ctx {
fn visit_obj<'a>(
key: &'a VariableKey,
map: &'a HashMap<String, Box<JsonTree>>
) -> Result<&'a JsonTree, String> {
) -> Result<Option<&'a Box<JsonTree>>, String> {
let key = match key {
VariableKey::Key(k) => k,
VariableKey::Index(i) => return Err(
format!("Attempted to use index {} in object", i)
)
};
match map.get(key) {
None => Err(
format!("Key {} not found in object", key)
),
Some(v) => Ok(v)
}
Ok(map.get(key))
}

fn visit_mut_obj<'a>(
Expand Down
9 changes: 9 additions & 0 deletions src/model/variable_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,12 @@ pub enum VariableKey {
Key(String),
Index(usize),
}

impl std::fmt::Display for VariableKey {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
VariableKey::Key(k) => write!(f, "{}", k),
VariableKey::Index(i) => write!(f, "{}", i),
}
}
}
28 changes: 25 additions & 3 deletions src/tests/interpreter/get_ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,27 @@ macro_tests!(
"{{ arr[0].name }} {{ arr[1].surname }}",
r#"{"arr": [{"name": "foo"}, {"name": "bar", "surname": "baz"}]}"#,
"foo baz"
),
(
null_variable01,
"{{ foo }}",
"{}",
"null"
),
(
null_variable02,
"{{ foo.bar }}",
r#"{"foo": {}}"#,
"null"
)
);

macro_tests!(
expect_error,
(
invalid_variable01,
"{{ foo }}",
r#"{}"#
"{{ foo.bar }}",
r#"{"foo": "bar"}"#
),
(
invalid_variable02,
Expand All @@ -50,7 +62,7 @@ macro_tests!(
(
invalid_variable03,
"{{ foo.bar }}",
r#"{"foo": {"other": 42}}"#
r#"{"foo": ["other"]}"#
),
(
invalid_variable_index01,
Expand All @@ -61,6 +73,16 @@ macro_tests!(
invalid_variable_index02,
"{{ arr[12] }}",
r#"{"arr": [1, 2, 3]}"#
),
(
invalid_variable_index03,
"{{ arr[0] }}",
r#"{"arr": {"foo": "bar"}}"#
),
(
invalid_variable_index04,
"{{ arr[0] }}",
r#"{"arr": "bar"}"#
)
);

Expand Down
4 changes: 2 additions & 2 deletions src/tests/model/ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,13 +253,13 @@ macro_tests!(
(
get_invalid_key01,
r#"{"foo": "bar"}"#,
"other",
"foo.bar",
Err(())
),
(
get_invalid_key02,
r#"{"foo": {"bar": "baz"}}"#,
"foo.other",
"foo.bar.baz",
Err(())
),
(
Expand Down
18 changes: 13 additions & 5 deletions src/tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,14 +251,24 @@ macro_tests!(
test_execution_error,
(
not_found_key01,
"Hello, {{name}}!",
Some(r#"{ "name2": "world" }"#)
"Hello, {{usr.name}}!",
Some(r#"{ "user": { "name": "John" } }"#)
),
(
not_found_key02,
"Hello, {{lst[4]}}!",
Some(r#"{ "lst": [1, 2, 3] }"#)
),
(
not_found_key03,
"Hello, {{lst[4]}}!",
Some(r#"{ "lst": { "a": 1, "b": 2 } }"#)
),
(
not_found_key04,
"Hello, {{obj[0]}}!",
Some(r#"{ "obj": {}}"#)
),
(
foreach_fail01,
"Hello, {{name}}!
Expand All @@ -273,9 +283,7 @@ macro_tests!(
"url": "https://example01.com",
"name": "Example01"
},
{
"url": "https://example02.org"
}
123
]
}
"#)
Expand Down

0 comments on commit 16b448a

Please sign in to comment.