use serde_json::Value;
use crate::ConvertError;
use crate::json::{self, child, index, invalid};
use qql_core::ast::{ComparisonOp, FilterExpr, GeoPoint, Value as AstValue};
pub(crate) fn decode_match(
value: &Value,
path: &str,
field: &str,
) -> Result<FilterExpr, ConvertError> {
let obj = json::object(value, path)?;
let mut variant: Option<FilterExpr> = None;
for key in obj.keys() {
let expr = match key.as_str() {
"value" => {
let raw = json::required(obj, "value", path)?;
let literal = match raw {
Value::String(_) | Value::Bool(_) => {
json::json_to_ast_value(raw, &child(path, "value"))?
}
Value::Number(n) if n.as_i64().is_some() || n.as_u64().is_some() => {
json::json_to_ast_value(raw, &child(path, "value"))?
}
other => {
return Err(invalid(
child(path, "value"),
format!(
"match value must be a string, integer, or boolean, got {}",
json::type_name(other)
),
));
}
};
FilterExpr::Compare {
field: field.to_string(),
op: ComparisonOp::Eq,
value: literal,
}
}
"text" => FilterExpr::MatchText {
field: field.to_string(),
text: json::required(obj, "text", path)
.and_then(|v| Ok(json::string_at(v, &child(path, "text"))?.to_string()))?,
},
"phrase" => FilterExpr::MatchPhrase {
field: field.to_string(),
text: json::required(obj, "phrase", path)
.and_then(|v| Ok(json::string_at(v, &child(path, "phrase"))?.to_string()))?,
},
"prefix" => FilterExpr::MatchPrefix {
field: field.to_string(),
prefix: json::required(obj, "prefix", path)
.and_then(|v| Ok(json::string_at(v, &child(path, "prefix"))?.to_string()))?,
},
"any" => {
let any_path = child(path, "any");
let raw =
json::required(obj, "any", path).and_then(|v| json::array(v, &any_path))?;
let values = raw
.iter()
.enumerate()
.map(|(i, item)| match item {
Value::String(_) | Value::Bool(_) => {
json::json_to_ast_value(item, &index(&any_path, i))
}
Value::Number(n) if n.as_i64().is_some() || n.as_u64().is_some() => {
json::json_to_ast_value(item, &index(&any_path, i))
}
other => Err(invalid(
index(&any_path, i),
format!(
"match any values must be strings or integers, got {}",
json::type_name(other)
),
)),
})
.collect::<Result<Vec<AstValue>, _>>()?;
if values.is_empty() {
return Err(invalid(any_path, "match any must list at least one value"));
}
FilterExpr::MatchAny {
field: field.to_string(),
values,
}
}
"keyword" => FilterExpr::Compare {
field: field.to_string(),
op: ComparisonOp::Eq,
value: AstValue::Str(
json::string_at(
json::required(obj, "keyword", path)?,
&child(path, "keyword"),
)?
.to_string(),
),
},
"integer" => FilterExpr::Compare {
field: field.to_string(),
op: ComparisonOp::Eq,
value: {
let n = json::required(obj, "integer", path)
.and_then(|v| json::u64_at(v, &child(path, "integer")))?;
i64::try_from(n)
.map(AstValue::Int)
.unwrap_or(AstValue::UInt(n))
},
},
"boolean" => FilterExpr::Compare {
field: field.to_string(),
op: ComparisonOp::Eq,
value: AstValue::Bool(
json::required(obj, "boolean", path)
.and_then(|v| json::bool_at(v, &child(path, "boolean")))?,
),
},
"text_any" => FilterExpr::MatchTokens {
field: field.to_string(),
text: json::required(obj, "text_any", path)
.and_then(|v| Ok(json::string_at(v, &child(path, "text_any"))?.to_string()))?,
},
"except" => {
let except_path = child(path, "except");
let raw = json::required(obj, "except", path)
.and_then(|v| json::array(v, &except_path))?;
let values = raw
.iter()
.enumerate()
.map(|(i, item)| match item {
Value::String(_) | Value::Bool(_) => {
json::json_to_ast_value(item, &index(&except_path, i))
}
Value::Number(n) if n.as_i64().is_some() || n.as_u64().is_some() => {
json::json_to_ast_value(item, &index(&except_path, i))
}
other => Err(invalid(
index(&except_path, i),
format!(
"match except values must be strings or integers, got {}",
json::type_name(other)
),
)),
})
.collect::<Result<Vec<AstValue>, _>>()?;
if values.is_empty() {
return Err(invalid(
except_path,
"match except must list at least one value",
));
}
FilterExpr::MatchExcept {
field: field.to_string(),
values,
}
}
other => {
return Err(invalid(child(path, other), "unknown Match variant"));
}
};
if variant.replace(expr).is_some() {
return Err(invalid(
path,
"match object must contain exactly one variant",
));
}
}
variant.ok_or_else(|| invalid(path, "empty match object"))
}
pub(crate) fn decode_range(
value: &Value,
path: &str,
field: &str,
) -> Result<FilterExpr, ConvertError> {
let obj = json::object(value, path)?;
let mut typed: Vec<(ComparisonOp, AstValue)> = Vec::new();
for key in obj.keys() {
let op = match key.as_str() {
"gt" => ComparisonOp::Gt,
"gte" => ComparisonOp::Gte,
"lt" => ComparisonOp::Lt,
"lte" => ComparisonOp::Lte,
other => {
return Err(invalid(
child(path, other),
"unknown range bound (expected gt, gte, lt, lte)",
));
}
};
let raw = obj.get(key).expect("key iterated");
let literal = match raw {
Value::Null => continue,
Value::String(_) => {
let s = json::string_at(raw, &child(path, key))?;
AstValue::Str(s.to_string())
}
Value::Number(_) => {
if let Some(i) = raw.as_i64() {
AstValue::Int(i)
} else if let Some(u) = raw.as_u64() {
AstValue::UInt(u)
} else {
AstValue::Float(json::f64_at(raw, &child(path, key))?)
}
}
other => {
return Err(invalid(
child(path, key),
format!(
"range bounds must be numbers or datetimes, got {}",
json::type_name(other)
),
));
}
};
typed.push((op, literal));
}
if typed.is_empty() {
return Err(invalid(path, "range has no bounds"));
}
let find = |op: ComparisonOp| -> Option<AstValue> {
typed.iter().find(|(o, _)| *o == op).map(|(_, v)| v.clone())
};
let (gte, lte) = (find(ComparisonOp::Gte), find(ComparisonOp::Lte));
if let (Some(low), Some(high), true, true) = (
gte,
lte,
find(ComparisonOp::Gt).is_none(),
find(ComparisonOp::Lt).is_none(),
) {
return Ok(FilterExpr::Between {
field: field.to_string(),
low,
high,
});
}
let comparisons: Vec<FilterExpr> = typed
.into_iter()
.map(|(op, value)| FilterExpr::Compare {
field: field.to_string(),
op,
value,
})
.collect();
Ok(match comparisons.len() {
1 => comparisons.into_iter().next().expect("len checked"),
_ => FilterExpr::And {
operands: comparisons,
},
})
}
pub(crate) fn decode_geo(
name: &str,
value: &Value,
path: &str,
field: &str,
) -> Result<FilterExpr, ConvertError> {
match name {
"geo_bounding_box" => {
let obj = json::object(value, path)?;
let top_left = geo_point(
json::required(obj, "top_left", path)?,
&child(path, "top_left"),
)?;
let bottom_right = geo_point(
json::required(obj, "bottom_right", path)?,
&child(path, "bottom_right"),
)?;
Ok(FilterExpr::GeoBoundingBox {
field: field.to_string(),
top_left,
bottom_right,
})
}
"geo_radius" => {
let obj = json::object(value, path)?;
let center = geo_point(json::required(obj, "center", path)?, &child(path, "center"))?;
let radius = json::required(obj, "radius", path)
.and_then(|v| json::f64_at(v, &child(path, "radius")))?;
if radius <= 0.0 {
return Err(invalid(
child(path, "radius"),
"geo radius must be greater than zero",
));
}
Ok(FilterExpr::GeoRadius {
field: field.to_string(),
center,
radius,
})
}
"geo_polygon" => {
let obj = json::object(value, path)?;
let exterior = geo_ring(
json::required(obj, "exterior", path)?,
&child(path, "exterior"),
)?;
let interiors = match obj.get("interiors").filter(|v| !v.is_null()) {
None => Vec::new(),
Some(rings) => {
let rings_path = child(path, "interiors");
json::array(rings, &rings_path)?
.iter()
.enumerate()
.map(|(i, ring)| geo_ring(ring, &index(&rings_path, i)))
.collect::<Result<Vec<_>, _>>()?
}
};
Ok(FilterExpr::GeoPolygon {
field: field.to_string(),
exterior,
interiors,
})
}
other => Err(invalid(child(path, other), "unknown geo predicate")),
}
}
fn geo_point(value: &Value, path: &str) -> Result<GeoPoint, ConvertError> {
let obj = json::object(value, path)?;
let lat =
json::required(obj, "lat", path).and_then(|v| json::f64_at(v, &child(path, "lat")))?;
let lon =
json::required(obj, "lon", path).and_then(|v| json::f64_at(v, &child(path, "lon")))?;
Ok(GeoPoint { lat, lon })
}
fn geo_ring(value: &Value, path: &str) -> Result<Vec<GeoPoint>, ConvertError> {
let obj = json::object(value, path)?;
let points_path = child(path, "points");
let points = json::required(obj, "points", path)
.and_then(|v| json::array(v, &points_path))?
.iter()
.enumerate()
.map(|(i, point)| geo_point(point, &index(&points_path, i)))
.collect::<Result<Vec<_>, _>>()?;
if points.len() < 3 {
return Err(invalid(
points_path,
"polygon rings must have at least 3 vertices",
));
}
Ok(points)
}
pub(crate) fn decode_values_count(
value: &Value,
path: &str,
field: &str,
) -> Result<FilterExpr, ConvertError> {
let obj = json::object(value, path)?;
let mut typed: Vec<(ComparisonOp, u64)> = Vec::new();
for key in obj.keys() {
let op = match key.as_str() {
"gt" => ComparisonOp::Gt,
"gte" => ComparisonOp::Gte,
"lt" => ComparisonOp::Lt,
"lte" => ComparisonOp::Lte,
other => {
return Err(invalid(
child(path, other),
"unknown values_count bound (expected gt, gte, lt, lte)",
));
}
};
let raw = obj.get(key).expect("key iterated");
if raw.is_null() {
continue;
}
typed.push((op, json::u64_at(raw, &child(path, key))?));
}
if typed.is_empty() {
return Err(invalid(path, "values_count has no bounds"));
}
let find = |op: ComparisonOp| typed.iter().find(|(o, _)| *o == op).map(|(_, v)| *v);
if let (Some(gte), Some(lte)) = (find(ComparisonOp::Gte), find(ComparisonOp::Lte))
&& gte == lte
{
return Ok(FilterExpr::ValuesCount {
field: field.to_string(),
op: ComparisonOp::Eq,
count: gte,
});
}
let comparisons: Vec<FilterExpr> = typed
.into_iter()
.map(|(op, count)| FilterExpr::ValuesCount {
field: field.to_string(),
op,
count,
})
.collect();
Ok(match comparisons.len() {
1 => comparisons.into_iter().next().expect("len checked"),
_ => FilterExpr::And {
operands: comparisons,
},
})
}