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
use pact_matching::models::matchingrules::{Category, MatchingRule, RuleLogic};
use pact_matching::models::generators::{Generators, Generator, GeneratorCategory};
use serde_json::{Value, Map};
use pact_matching::models::json_utils::json_to_string;
fn process_array(array: &Vec<Value>, matching_rules: &mut Category, generators: &mut Generators, path: &String, type_matcher: bool) -> Value {
Value::Array(array.iter().enumerate().map(|(index, val)| {
let updated_path = if type_matcher {
path.to_owned() + "[*]"
} else {
path.to_owned() + "[" + &index.to_string() + "]"
};
match val {
Value::Object(ref map) => process_object(map, matching_rules, generators, &updated_path, false),
Value::Array(ref array) => process_array(array, matching_rules, generators, &updated_path, false),
_ => val.clone()
}
}).collect())
}
fn process_object(obj: &Map<String, Value>, matching_rules: &mut Category, generators: &mut Generators, path: &String, type_matcher: bool) -> Value {
if obj.contains_key("pact:matcher:type") {
if let Some(rule) = MatchingRule::from_integration_json(obj) {
matching_rules.add_rule(path, rule, &RuleLogic::And);
}
if let Some(gen) = obj.get("pact:generator:type") {
match Generator::from_map(&json_to_string(gen), obj) {
Some(generator) => generators.add_generator_with_subcategory(&GeneratorCategory::BODY, path, generator),
_ => ()
};
}
match obj.get("value") {
Some(val) => match val {
Value::Object(ref map) => process_object(map, matching_rules, generators, path, true),
Value::Array(array) => process_array(array, matching_rules, generators, path, true),
_ => val.clone()
},
None => Value::Null
}
} else {
Value::Object(obj.iter().map(|(key, val)| {
let updated_path = if type_matcher {
path.to_owned() + ".*"
} else {
path.to_owned() + "." + key
};
(key.clone(), match val {
Value::Object(ref map) => process_object(map, matching_rules, generators, &updated_path, false),
Value::Array(ref array) => process_array(array, matching_rules, generators, &updated_path, false),
_ => val.clone()
})
}).collect())
}
}
pub fn process_json(body: String, matching_rules: &mut Category, generators: &mut Generators) -> String {
match serde_json::from_str(&body) {
Ok(json) => match json {
Value::Object(ref map) => process_object(map, matching_rules, generators, &"$".to_string(), false).to_string(),
Value::Array(ref array) => process_array(array, matching_rules, generators, &"$".to_string(), false).to_string(),
_ => body
},
Err(_) => body
}
}