1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
use crate::conversion::JSArg;
use serde_json::value::Value as JsonValue;

pub fn value_to_js_arg(value: &quaint::Value) -> serde_json::Result<JSArg> {
    let res = match &value.typed {
        quaint::ValueType::Numeric(Some(bd)) => match bd.to_string().parse::<f64>() {
            Ok(double) => JSArg::from(JsonValue::from(double)),
            Err(_) => JSArg::from(JsonValue::from(value.clone())),
        },
        quaint::ValueType::Json(Some(s)) => JSArg::Value(s.to_owned()),
        quaint::ValueType::Bytes(Some(bytes)) => JSArg::Buffer(bytes.to_vec()),
        quaint::ValueType::Array(Some(ref items)) => JSArg::Array(
            items
                .iter()
                .map(value_to_js_arg)
                .collect::<serde_json::Result<Vec<JSArg>>>()?,
        ),
        quaint_value => JSArg::from(JsonValue::from(quaint_value.clone())),
    };

    Ok(res)
}

// unit tests for value_to_js_arg
#[cfg(test)]
mod test {
    use super::*;
    use quaint::bigdecimal::BigDecimal;
    use quaint::chrono::*;
    use quaint::ValueType;
    use serde_json::Value;
    use std::str::FromStr;

    #[test]
    #[rustfmt::skip]
    fn test_value_to_js_arg() {
        let test_cases = vec![
           (
                // This is different than how mysql or postgres processes integral BigInt values.
                ValueType::Numeric(Some(1.into())),
                JSArg::Value(Value::Number("1.0".parse().unwrap()))
            ),
            (
                ValueType::Numeric(Some(BigDecimal::from_str("-1.1").unwrap())),
                JSArg::Value(Value::Number("-1.1".parse().unwrap())),
            ),
            (
                ValueType::Numeric(None),
                JSArg::Value(Value::Null)
            ),
            (
                ValueType::Json(Some(serde_json::json!({"a": 1}))),
                JSArg::Value(serde_json::json!({"a": 1})),
            ),
            (
                ValueType::Json(None),
                JSArg::Value(Value::Null)
            ),
            (
                ValueType::Date(Some(NaiveDate::from_ymd_opt(2020, 2, 29).unwrap())),
                JSArg::Value(Value::String("2020-02-29".to_string())),
            ),
            (
                ValueType::Date(None),
                JSArg::Value(Value::Null)
            ),
            (
                ValueType::DateTime(Some(Utc.with_ymd_and_hms(2020, 1, 1, 23, 13, 1).unwrap())),
                JSArg::Value(Value::String("2020-01-01T23:13:01+00:00".to_string())),
            ),
            (
                ValueType::DateTime(None),
                JSArg::Value(Value::Null)
            ),
            (
                ValueType::Time(Some(NaiveTime::from_hms_opt(23, 13, 1).unwrap())),
                JSArg::Value(Value::String("23:13:01".to_string())),
            ),
            (
                ValueType::Time(None),
                JSArg::Value(Value::Null)
            ),
            (
                ValueType::Array(Some(vec!(
                    ValueType::Numeric(Some(1.into())).into_value(),
                    ValueType::Numeric(None).into_value(),
                    ValueType::Time(Some(NaiveTime::from_hms_opt(23, 13, 1).unwrap())).into_value(),
                    ValueType::Time(None).into_value(),
                ))),
                JSArg::Array(vec!(
                    JSArg::Value(Value::Number("1.0".parse().unwrap())),
                    JSArg::Value(Value::Null),
                    JSArg::Value(Value::String("23:13:01".to_string())),
                    JSArg::Value(Value::Null),
                ))
            ),
            (
                ValueType::Bytes(Some("hello".as_bytes().into())),
                JSArg::Buffer("hello".as_bytes().to_vec())
            ),
        ];

        let mut errors: Vec<String> = vec![];
        for (val, expected) in test_cases {
            let actual = value_to_js_arg(&val.clone().into_value()).unwrap();
            if actual != expected {
                errors.push(format!("transforming: {:?}, expected: {:?}, actual: {:?}", &val, expected, actual));
            }
        }
        assert_eq!(errors.len(), 0, "{}", errors.join("\n"));
    }
}