aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOnur Gündüz <ogunduz@gmail.com>2026-09-29 14:34:42 +0200
committerOnur Gündüz <ogunduz@gmail.com>2026-09-29 14:34:42 +0200
commit0ebdd46766744d649c93f4023d7f20cbfde51c1e (patch)
treec1ce87732fa766b591e380e1fb0b0a3f86d77807
parent66e86c9693a7184944cdd2a0dbf1514e9d0125dc (diff)
downloadele-0ebdd46766744d649c93f4023d7f20cbfde51c1e.tar.gz
ele-0ebdd46766744d649c93f4023d7f20cbfde51c1e.zip
Implement map-key presence and repeated has traversal
-rw-r--r--README.md4
-rw-r--r--src/compiled.rs90
-rw-r--r--src/eval.rs4
-rw-r--r--tests/cli_tests.rs3
-rw-r--r--tests/has_path_tests.rs98
5 files changed, 189 insertions, 10 deletions
diff --git a/README.md b/README.md
index 83757eb..74af167 100644
--- a/README.md
+++ b/README.md
@@ -29,7 +29,9 @@ usage: aip-filter [-p|--print] [--max-record-bytes N] [--] <expr> [file...]
* Bare literals search values recursively, including nested objects and arrays, but not object keys. String searches are case-insensitive substrings.
* An unquoted boolean field name evaluates that field. A dotted name resolves a field path first and otherwise becomes a global literal. Quote a search term to prevent field lookup, for example `'"active"'` or `'"example.com"'`.
* String `=` and `!=` ignore ASCII case and support one leading or trailing `*`. The two operators are opposites.
-* String `:` matches words or phrases bounded by whitespace or ASCII punctuation, or a leading/trailing wildcard. Lists match any element; maps match keys. `field:*` tests for a non-default value, including a nonempty list or map.
+* String `:` matches words or phrases bounded by whitespace or ASCII punctuation, or a leading/trailing wildcard. Lists match any element; maps match keys.
+* Top-level `field:*` tests for a non-default value, including a nonempty list or map. For a map entry, `m.foo:*` tests whether the key exists, even when its value is zero, false, empty, or null. JSON objects and `Value::Map` use map semantics; custom message paths use non-default presence.
+* The `:` operator can traverse repeated values: `r.foo:42` matches if an element has a matching `foo` value. A grouped right-hand side applies to one resolved element. Ordinary comparisons do not traverse arrays, and numeric path segments are never array indices.
* Functions can be parsed and printed, but evaluation rejects the entire filter if any function is present, including inside a negation or unused branch.
* The CLI compiles filters before reading records. Regexes, numeric literals, and field paths are reused across records. Filters can contain at most 32 regexes; each regex has a 1 MiB compiled-size limit and a 256 KiB DFA cache limit.
* Filters are limited to 64 KiB, 512 non-whitespace tokens, 64 recursive parser calls, and an expression tree of at most 128 levels and 512 nodes. Excessive filters return errors instead of recursing without a bound.
diff --git a/src/compiled.rs b/src/compiled.rs
index c9f5ab8..2981f78 100644
--- a/src/compiled.rs
+++ b/src/compiled.rs
@@ -28,7 +28,11 @@ enum Node<'a> {
field: Option<Source<'a>>,
boolean_only: bool,
},
- Comparison(Source<'a>, Predicate<'a>),
+ Comparison {
+ source: Source<'a>,
+ predicate: Predicate<'a>,
+ op: CompareOp,
+ },
}
#[derive(Debug)]
@@ -75,10 +79,11 @@ impl<'a> Node<'a> {
Expr::Unary { expr, .. } => Self::Not(Box::new(Self::compile(expr, regex_budget)?)),
Expr::Comparison {
op, left, right, ..
- } => Self::Comparison(
- Source::compile(left)?,
- Predicate::compile(*op, right, regex_budget)?,
- ),
+ } => Self::Comparison {
+ source: Source::compile(left)?,
+ predicate: Predicate::compile(*op, right, regex_budget)?,
+ op: *op,
+ },
Expr::Name {
name, is_string, ..
} => Self::Global {
@@ -121,7 +126,14 @@ impl<'a> Node<'a> {
}
record.matches_global(text)
}
- Self::Comparison(source, predicate) => source
+ Self::Comparison {
+ source,
+ predicate,
+ op: CompareOp::Has,
+ } => source.evaluate_has(record, predicate),
+ Self::Comparison {
+ source, predicate, ..
+ } => source
.resolve(record)
.is_some_and(|value| predicate.evaluate(&value)),
}
@@ -145,6 +157,30 @@ impl<'a> Source<'a> {
}
}
+ fn evaluate_has<T: Filterable>(&self, record: &T, predicate: &Predicate<'_>) -> bool {
+ if let Self::Field(path) = self {
+ for length in 1..path.len() {
+ let value = if length == 1 {
+ record
+ .field(path[0])
+ .or_else(|| record.field_path(&path[..length]))
+ } else {
+ record.field_path(&path[..length])
+ };
+ if let Some(value) = value
+ && matches!(
+ value,
+ Value::Map(_) | Value::JsonObject(_) | Value::List(_) | Value::JsonArray(_)
+ )
+ {
+ return has_path(value, &path[length..], predicate, false);
+ }
+ }
+ }
+ self.resolve(record)
+ .is_some_and(|value| predicate.evaluate(&value))
+ }
+
fn resolve<'s, T: Filterable>(&'s self, record: &'s T) -> Option<Value<'s>> {
let path = match self {
Self::String(text) => return Some(Value::String(text)),
@@ -216,14 +252,21 @@ impl<'a> Predicate<'a> {
}
fn evaluate(&self, value: &Value<'_>) -> bool {
+ self.evaluate_with_presence(value, false)
+ }
+
+ fn evaluate_with_presence(&self, value: &Value<'_>, map_entry: bool) -> bool {
match self {
Self::Binary(BinaryOp::And, left, right) => {
- left.evaluate(value) && right.evaluate(value)
+ left.evaluate_with_presence(value, map_entry)
+ && right.evaluate_with_presence(value, map_entry)
}
Self::Binary(BinaryOp::Or, left, right) => {
- left.evaluate(value) || right.evaluate(value)
+ left.evaluate_with_presence(value, map_entry)
+ || right.evaluate_with_presence(value, map_entry)
}
- Self::Not(expr) => !expr.evaluate(value),
+ Self::Not(expr) => !expr.evaluate_with_presence(value, map_entry),
+ Self::Literal(CompareOp::Has, literal) if map_entry && literal.is_presence() => true,
Self::Literal(op, literal) => eval_literal(*op, value, literal),
Self::Regex { regex, negated } => value
.as_str()
@@ -232,6 +275,35 @@ impl<'a> Predicate<'a> {
}
}
+// Map-key presence is independent of its value; only `:` traverses repeated values.
+fn has_path(value: Value<'_>, path: &[&str], predicate: &Predicate<'_>, map_entry: bool) -> bool {
+ let Some((segment, rest)) = path.split_first() else {
+ return predicate.evaluate_with_presence(&value, map_entry);
+ };
+ match value {
+ Value::Map(entries) => entries
+ .into_iter()
+ .find(|(key, _)| key.eq_ignore_ascii_case(segment))
+ .is_some_and(|(_, value)| has_path(value, rest, predicate, true)),
+ Value::JsonObject(entries) => entries
+ .get(*segment)
+ .or_else(|| {
+ entries
+ .iter()
+ .find(|(key, _)| key.eq_ignore_ascii_case(segment))
+ .map(|(_, value)| value)
+ })
+ .is_some_and(|value| has_path(Value::from(value), rest, predicate, true)),
+ Value::List(items) => items
+ .into_iter()
+ .any(|value| has_path(value, path, predicate, false)),
+ Value::JsonArray(items) => items
+ .iter()
+ .any(|value| has_path(Value::from(value), path, predicate, false)),
+ _ => false,
+ }
+}
+
fn field_path(expr: &Expr) -> Result<Vec<&str>, String> {
let mut path = Vec::new();
let mut current = expr;
diff --git a/src/eval.rs b/src/eval.rs
index 9c37b15..517409f 100644
--- a/src/eval.rs
+++ b/src/eval.rs
@@ -271,6 +271,10 @@ impl<'a> Literal<'a> {
Self { text, folded, number, boolean, word_prefix }
}
+ pub(crate) fn is_presence(&self) -> bool {
+ self.text == "*"
+ }
+
fn folded(&self) -> &str {
self.folded.as_deref().unwrap_or(&self.text)
}
diff --git a/tests/cli_tests.rs b/tests/cli_tests.rs
index 9e0f848..c283e19 100644
--- a/tests/cli_tests.rs
+++ b/tests/cli_tests.rs
@@ -128,6 +128,9 @@ fn has_and_boolean_values() {
matches("meta:*", r#"{"meta":{"owner":"alice"}}"#, true);
matches("meta:*", r#"{"meta":{}}"#, false);
matches("meta.owner=alice", r#"{"meta":{"owner":"alice"}}"#, true);
+ matches("meta.zero:*", r#"{"meta":{"zero":0}}"#, true);
+ matches("r.foo:42", r#"{"r":[{"foo":42}]}"#, true);
+ matches("r.foo:42", r#"{"r":[{"foo":0}]}"#, false);
}
#[test]
diff --git a/tests/has_path_tests.rs b/tests/has_path_tests.rs
new file mode 100644
index 0000000..459eaba
--- /dev/null
+++ b/tests/has_path_tests.rs
@@ -0,0 +1,98 @@
+use aip_filter::{Filterable, Value, parse};
+use serde_json::json;
+
+fn matches(record: &impl Filterable, text: &str) -> bool {
+ parse(text)
+ .unwrap()
+ .unwrap()
+ .compile()
+ .unwrap()
+ .evaluate(record)
+}
+
+#[test]
+fn map_presence_does_not_depend_on_the_value() {
+ for value in [
+ json!(0),
+ json!(false),
+ json!(""),
+ json!(null),
+ json!([]),
+ json!({}),
+ ] {
+ let record = json!({"m": {"foo": value}});
+ assert!(matches(&record, "m:foo"));
+ assert!(matches(&record, "m.foo:*"), "{record}");
+ assert!(matches(&record, "m.foo:(* OR absent)"));
+ assert!(!matches(&record, "m.foo:(NOT *)"));
+ assert!(!matches(&record, "m.missing:*"));
+ assert!(!matches(&record, "m.missing:(NOT *)"));
+ }
+ assert!(!matches(&json!({"m": {}}), "m:*"));
+ assert!(!matches(&json!({"r": []}), "r:*"));
+ assert!(!matches(&json!({"n": 0}), "n:*"));
+ assert!(matches(&json!({"m": {"foo": 0}}), "m.foo:(0 AND *)"));
+ assert!(matches(&json!({"m": {"FOO": 1, "foo": 0}}), "m.foo:0"));
+}
+
+#[test]
+fn has_traverses_repeated_values_without_array_indices() {
+ let record = json!({"r": [{"foo": 42}, {"foo": 43}, {"other": 0}, null]});
+ assert!(matches(&record, "r.foo:42"));
+ assert!(matches(&record, "r.foo:43"));
+ assert!(matches(&record, "r.foo:*"));
+ assert!(matches(&record, "r.foo:42 AND r.foo:43"));
+ assert!(!matches(&record, "r.foo:(42 AND 43)"));
+ assert!(!matches(&record, "r.foo:99"));
+ assert!(!matches(&record, "r.0.foo:42"));
+ assert!(!matches(&record, "r.foo = 42"));
+ assert!(!matches(&record, "r.foo != 42"));
+ assert!(!matches(&record, "r.foo > 0"));
+ assert!(!matches(&json!({"r": []}), "r.foo:*"));
+ assert!(!matches(&json!({"r": [{"other": 0}]}), "r.foo:(NOT *)"));
+ assert!(matches(&json!({"r": [{"foo": 0}]}), "r.foo:*"));
+ assert!(matches(
+ &json!({"r": [{"foo": [{"bar": 42}]}]}),
+ "r.foo.bar:42"
+ ));
+}
+
+#[test]
+fn owned_collections_follow_the_same_has_rules() {
+ struct Record;
+ impl Filterable for Record {
+ fn field(&self, name: &str) -> Option<Value<'_>> {
+ match name {
+ "m" => Some(Value::Map(vec![("foo", Value::Bool(false))])),
+ "r" => Some(Value::List(vec![Value::Map(vec![("foo", Value::Int(42))])])),
+ _ => None,
+ }
+ }
+ }
+ assert!(matches(&Record, "m.foo:*"));
+ assert!(matches(&Record, "r.foo:42"));
+ assert!(!matches(&Record, "r.foo = 42"));
+}
+
+#[test]
+fn custom_message_paths_keep_non_default_presence_rules() {
+ struct Message(i64);
+ impl Filterable for Message {
+ fn field(&self, _: &str) -> Option<Value<'_>> {
+ None
+ }
+ fn field_path(&self, path: &[&str]) -> Option<Value<'_>> {
+ match path {
+ ["inner", "value"] => Some(Value::Int(self.0)),
+ ["inner", "map"] => Some(Value::Map(vec![("value", Value::Int(self.0))])),
+ ["inner", "map", "value"] => Some(Value::Int(self.0)),
+ _ => None,
+ }
+ }
+ }
+ assert!(!matches(&Message(0), "inner.value:*"));
+ assert!(matches(&Message(42), "inner.value:*"));
+ assert!(matches(&Message(42), "inner.value:42"));
+ assert!(matches(&Message(0), "inner.map.value:*"));
+ assert!(matches(&Message(0), "inner.map.value:0"));
+}