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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
use json_eval_rs::JSONEval;
use serde_json::json;
#[test]
fn test_selective_evaluation_basic() {
let schema = json!({
"$params": {
"type": "illustration",
"accessList": {
"$evaluation": {
"if": [
{
"and": [
{
"in": [
{
"$ref": "$context.agentProfile.sob"
},
[
"AG",
"AP"
]
]
},
{
"==": [
{
"$ref": "$context.agentProfile.agentFlag"
},
"true"
]
}
]
},
{
"return": [
"AG",
"AP"
]
},
{
"return": []
}
]
}
},
"constants": {
"POL_DURATION": 8
},
"others": {
"MIN_SA": {
"$evaluation": {
"/": [
{
"*": [
{
"/": [
4000000,
{
"$ref": "#/illustration/properties/product_benefit/properties/benefit_type/properties/prem_freq"
}
]
},
1000
]
},
{
"*": [
{
"$ref": "#/$params/others/OTHER_SA"
},
{
"$ref": "#/$params/others/MODAL_FACTOR_CALC"
}
]
}
]
}
}
}
}
});
let schema_str = serde_json::to_string(&schema).unwrap();
let data = json!({});
let data_str = serde_json::to_string(&data).unwrap();
let ctx = json!({
"agentProfile": {
"agentFlag": "true",
"sob": "AP"
}
});
let ctx_str = serde_json::to_string(&ctx).unwrap();
let mut eval = JSONEval::new(&schema_str, Some(&ctx_str), Some(&data_str)).unwrap();
// 1. Full evaluation
eval.evaluate(&data_str, Some(&ctx_str), None, None)
.unwrap();
// Check results
let evaluated = eval.get_evaluated_schema();
assert_eq!(
*evaluated.pointer("/$params/accessList").unwrap(),
json!(["AG", "AP"])
);
// 2. Selective evaluation, not target the value must be persists
let nctx = json!({
"agentProfile": {
"agentFlag": "false",
"sob": "AP"
}
});
let nctx_str = serde_json::to_string(&nctx).unwrap();
eval.evaluate(
&data_str,
Some(&nctx_str),
Some(&["$params.others.MIN_SA".to_string()]),
None,
)
.unwrap();
// Check results
let evaluated = eval.get_evaluated_schema();
assert_eq!(
*evaluated.pointer("/$params/accessList").unwrap(),
json!(["AG", "AP"])
);
// 3. Selective evaluation, target the value must be re-evaluated
let nctx = json!({
"agentProfile": {
"agentFlag": "false",
"sob": "AP"
}
});
let nctx_str = serde_json::to_string(&nctx).unwrap();
eval.evaluate(
&data_str,
Some(&nctx_str),
Some(&["$params.accessList".to_string()]),
None,
)
.unwrap();
// Check results
let evaluated = eval.get_evaluated_schema();
assert_eq!(
*evaluated.pointer("/$params/accessList").unwrap(),
json!([])
);
}