tera/builtins/filters/
object.rs

1/// Filters operating on numbers
2use std::collections::HashMap;
3
4use serde_json::value::Value;
5
6use crate::errors::{Error, Result};
7
8/// Returns a value by a `key` argument from a given object
9pub fn get(value: &Value, args: &HashMap<String, Value>) -> Result<Value> {
10    let default = args.get("default");
11    let key = match args.get("key") {
12        Some(val) => try_get_value!("get", "key", String, val),
13        None => return Err(Error::msg("The `get` filter has to have an `key` argument")),
14    };
15
16    match value.as_object() {
17        Some(o) => match o.get(&key) {
18            Some(val) => Ok(val.clone()),
19            // If the value is not present, allow for an optional default value
20            None => match default {
21                Some(def) => Ok(def.clone()),
22                None => Err(Error::msg(format!(
23                    "Filter `get` tried to get key `{}` but it wasn't found",
24                    &key
25                ))),
26            },
27        },
28        None => Err(Error::msg("Filter `get` was used on a value that isn't an object")),
29    }
30}
31
32#[cfg(test)]
33mod tests {
34    use super::*;
35    use serde_json::value::to_value;
36    use std::collections::HashMap;
37
38    #[test]
39    fn test_get_filter_exists() {
40        let mut obj = HashMap::new();
41        obj.insert("1".to_string(), "first".to_string());
42        obj.insert("2".to_string(), "second".to_string());
43
44        let mut args = HashMap::new();
45        args.insert("key".to_string(), to_value("1").unwrap());
46        let result = get(&to_value(&obj).unwrap(), &args);
47        assert!(result.is_ok());
48        assert_eq!(result.unwrap(), to_value("first").unwrap());
49    }
50
51    #[test]
52    fn test_get_filter_doesnt_exist() {
53        let mut obj = HashMap::new();
54        obj.insert("1".to_string(), "first".to_string());
55        obj.insert("2".to_string(), "second".to_string());
56
57        let mut args = HashMap::new();
58        args.insert("key".to_string(), to_value("3").unwrap());
59        let result = get(&to_value(&obj).unwrap(), &args);
60        assert!(result.is_err());
61    }
62
63    #[test]
64    fn test_get_filter_with_default_exists() {
65        let mut obj = HashMap::new();
66        obj.insert("1".to_string(), "first".to_string());
67        obj.insert("2".to_string(), "second".to_string());
68
69        let mut args = HashMap::new();
70        args.insert("key".to_string(), to_value("1").unwrap());
71        args.insert("default".to_string(), to_value("default").unwrap());
72        let result = get(&to_value(&obj).unwrap(), &args);
73        assert!(result.is_ok());
74        assert_eq!(result.unwrap(), to_value("first").unwrap());
75    }
76
77    #[test]
78    fn test_get_filter_with_default_doesnt_exist() {
79        let mut obj = HashMap::new();
80        obj.insert("1".to_string(), "first".to_string());
81        obj.insert("2".to_string(), "second".to_string());
82
83        let mut args = HashMap::new();
84        args.insert("key".to_string(), to_value("3").unwrap());
85        args.insert("default".to_string(), to_value("default").unwrap());
86        let result = get(&to_value(&obj).unwrap(), &args);
87        assert!(result.is_ok());
88        assert_eq!(result.unwrap(), to_value("default").unwrap());
89    }
90}