Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Reuse matching rules and generators processing code #345

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions rust/pact_ffi/src/mock_server/bodies.rs
Expand Up @@ -148,6 +148,7 @@ fn process_matcher(
Category::HEADER => &GeneratorCategory::HEADER,
Category::PATH => &GeneratorCategory::PATH,
Category::QUERY => &GeneratorCategory::QUERY,
Category::METADATA => &GeneratorCategory::METADATA,
_ => {
warn!("invalid generator category {} provided, defaulting to body", matching_rules.name);
&GeneratorCategory::BODY
Expand Down
43 changes: 8 additions & 35 deletions rust/pact_ffi/src/mock_server/handles.rs
Expand Up @@ -2145,50 +2145,23 @@ pub extern fn pactffi_message_with_metadata(message_handle: MessageHandle, key:
pub extern fn pactffi_message_with_metadata_v2(message_handle: MessageHandle, key: *const c_char, value: *const c_char) {
if let Some(key) = convert_cstr("key", key) {
let value = convert_cstr("value", value).unwrap_or_default();
trace!("pactffi_message_with_metadata_v2(message_handle: {:?}, key: {:?}, value: {})", message_handle, key, value);

message_handle.with_message(&|_, inner, _| {
if let Some(message) = inner.as_v4_async_message_mut() {
let matching_rules = message.contents.matching_rules.add_category(Category::METADATA);
let generators = &mut message.contents.generators;
let value = match serde_json::from_str(value) {
Ok(json) => match json {
Value::Object(ref obj) => {
if let Some(matcher_type) = obj.get("pact:matcher:type") {
debug!("detected pact:matcher:type, will configure a matcher");
let matcher_type = json_to_string(matcher_type);
let attributes = Value::Object(obj.clone());
let rule = MatchingRule::create(matcher_type.as_str(), &attributes).unwrap();
matching_rules.add_rule(DocPath::new(key).unwrap(), rule.clone(), RuleLogic::And);
}
if let Some(gen) = obj.get("pact:generator:type") {
debug!("detected pact:generator:type, will configure a generators");
if let Some(generator) = Generator::from_map(&json_to_string(gen), obj) {
generators.add_generator_with_subcategory(&GeneratorCategory::METADATA, DocPath::new(key).unwrap(), generator);
}
}
match obj.get("value") {
Some(inner) => match inner {
Value::Array(_) => {
warn!("Array value is not supported");
Value::Null
}
Value::Object(_) => {
warn!("Object value is not supported");
Value::Null
}
_ => inner.clone()
},
None => Value::Null
}
},
Value::String(string) => Value::String(string.to_string()),
_ => {
warn!("Only support object or string");
Value::String(value.to_string())
}
Value::Object(ref map) => process_object(map, matching_rules, generators, DocPath::new(key).unwrap(), false),
Value::Array(ref array) => process_array(array, matching_rules, generators, DocPath::new(key).unwrap(), false, false),
Value::Null => Value::Null,
Value::String(string) => Value::String(string),
Value::Bool(bool) => Value::Bool(bool),
Value::Number(number) => Value::Number(number),
},
Err(err) => {
warn!("Failed to parse metadata from JSON - {}", err);
warn!("Failed to parse metadata value '{}' as JSON - {}. Will treat it as string", value, err);
Value::String(value.to_string())
}
};
Expand Down