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

fix(merge transform): Support nested fields and arrays #1936

Merged
6 commits merged into from
Mar 16, 2020
90 changes: 90 additions & 0 deletions src/event/discriminant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,11 @@ fn value_eq(this: &Value, other: &Value) -> bool {
(Value::Boolean(this), Value::Boolean(other)) => this.eq(other),
(Value::Integer(this), Value::Integer(other)) => this.eq(other),
(Value::Timestamp(this), Value::Timestamp(other)) => this.eq(other),
(Value::Null, Value::Null) => true,
// Non-trivial.
(Value::Float(this), Value::Float(other)) => f64_eq(this, other),
(Value::Array(this), Value::Array(other)) => array_eq(this, other),
(Value::Map(this), Value::Map(other)) => map_eq(this, other),
// Type mismatch.
_ => false,
}
Expand All @@ -79,6 +82,26 @@ fn f64_eq(this: &f64, other: &f64) -> bool {
true
}

fn array_eq(this: &Vec<Value>, other: &Vec<Value>) -> bool {
if this.len() != other.len() {
return false;
}

this.iter()
.zip(other.iter())
.all(|(first, second)| value_eq(first, second))
}

fn map_eq(this: &BTreeMap<Atom, Value>, other: &BTreeMap<Atom, Value>) -> bool {
if this.len() != other.len() {
return false;
}

this.iter()
.zip(other.iter())
.all(|((key1, value1), (key2, value2))| key1 == key2 && value_eq(value1, value2))
}

impl Hash for Discriminant {
fn hash<H: Hasher>(&self, state: &mut H) {
for value in &self.values {
Expand Down Expand Up @@ -199,6 +222,73 @@ mod tests {
assert_eq!(hash(discriminant_1), hash(discriminant_2));
}

#[test]
fn map_values_key_order() {
let mut event_1 = new_log_event();
event_1.insert("nested.a", "a");
event_1.insert("nested.b", "b");
let mut event_2 = new_log_event();
event_2.insert("nested.b", "b");
event_2.insert("nested.a", "a");

let discriminant_fields = vec![Atom::from("nested")];

let discriminant_1 = Discriminant::from_log_event(&event_1, &discriminant_fields);
let discriminant_2 = Discriminant::from_log_event(&event_2, &discriminant_fields);

assert_eq!(discriminant_1, discriminant_2);
assert_eq!(hash(discriminant_1), hash(discriminant_2));
}

#[test]
fn map_values_array() {
let mut event_1 = new_log_event();
event_1.insert("array[0]", "a");
event_1.insert("array[1]", "b");
let mut event_2 = new_log_event();
event_2.insert("array[1]", "b");
event_2.insert("array[0]", "a");

let discriminant_fields = vec![Atom::from("array")];

let discriminant_1 = Discriminant::from_log_event(&event_1, &discriminant_fields);
let discriminant_2 = Discriminant::from_log_event(&event_2, &discriminant_fields);

assert_eq!(discriminant_1, discriminant_2);
assert_eq!(hash(discriminant_1), hash(discriminant_2));
}

#[test]
fn map_values_matter_1() {
let mut event_1 = new_log_event();
event_1.insert("nested.a", "a"); // `nested` is a `Value::Map`
let event_2 = new_log_event(); // empty event

let discriminant_fields = vec![Atom::from("nested")];

let discriminant_1 = Discriminant::from_log_event(&event_1, &discriminant_fields);
let discriminant_2 = Discriminant::from_log_event(&event_2, &discriminant_fields);

assert_ne!(discriminant_1, discriminant_2);
assert_ne!(hash(discriminant_1), hash(discriminant_2));
}

#[test]
fn map_values_matter_2() {
let mut event_1 = new_log_event();
event_1.insert("nested.a", "a"); // `nested` is a `Value::Map`
let mut event_2 = new_log_event();
event_2.insert("nested", "x"); // `nested` is a `Value::String`

let discriminant_fields = vec![Atom::from("nested")];

let discriminant_1 = Discriminant::from_log_event(&event_1, &discriminant_fields);
let discriminant_2 = Discriminant::from_log_event(&event_2, &discriminant_fields);

assert_ne!(discriminant_1, discriminant_2);
assert_ne!(hash(discriminant_1), hash(discriminant_2));
}

#[test]
fn with_hash_map() {
let mut map: HashMap<Discriminant, usize> = HashMap::new();
Expand Down