#[derive(Debug, Clone)]
pub struct AliasEntry {
pub from: &'static str,
pub to: &'static str,
pub model: Option<&'static str>,
}
impl AliasEntry {
pub const fn global(from: &'static str, to: &'static str) -> Self {
Self {
from,
to,
model: None,
}
}
pub const fn for_model(from: &'static str, to: &'static str, model: &'static str) -> Self {
Self {
from,
to,
model: Some(model),
}
}
}
pub const FIELD_ALIASES: &[AliasEntry] = &[
AliasEntry::global("filter", "where"),
AliasEntry::global("filters", "where"),
AliasEntry::global("conditions", "where"),
AliasEntry::global("condition", "where"),
AliasEntry::global("predicate", "where"),
AliasEntry::global("predicates", "where"),
AliasEntry::global("projection", "select"),
AliasEntry::global("projections", "select"),
AliasEntry::global("columns", "select"),
AliasEntry::global("project", "select"),
AliasEntry::global("project_list", "select"),
AliasEntry::global("fields", "select"),
AliasEntry::global("source", "from"),
AliasEntry::global("sources", "from"),
AliasEntry::global("tables", "from"),
AliasEntry::global("sort", "order_by"),
AliasEntry::global("sorts", "order_by"),
AliasEntry::global("sort_by", "order_by"),
AliasEntry::global("ordering", "order_by"),
AliasEntry::global("order", "order_by"),
AliasEntry::global("group", "group_by"),
AliasEntry::global("groupby", "group_by"),
AliasEntry::global("having_condition", "having"),
AliasEntry::global("limit_count", "limit"),
AliasEntry::global("max_rows", "limit"),
AliasEntry::global("max_results", "limit"),
AliasEntry::global("top", "limit"),
AliasEntry::global("offset_count", "offset"),
AliasEntry::global("skip", "offset"),
AliasEntry::global("start", "offset"),
AliasEntry::global("join", "joins"),
AliasEntry::global("relations", "joins"),
AliasEntry::global("cte", "ctes"),
AliasEntry::global("with", "ctes"),
AliasEntry::global("col", "column"),
AliasEntry::global("kind", "type"),
AliasEntry::global("field", "column"),
AliasEntry::global("table_name", "table"),
AliasEntry::global("alias_name", "alias"),
AliasEntry::global("desc", "descending"),
AliasEntry::global("operator", "op"),
AliasEntry::global("comparisons", "comparison"),
];
pub const MODEL_ALIASES: &[AliasEntry] = &[
];
#[must_use]
pub fn normalize_field_names(val: &mut serde_json::Value) -> bool {
normalize_field_names_impl(val, None)
}
#[must_use]
pub fn normalize_field_names_for_model(val: &mut serde_json::Value, model: &str) -> bool {
normalize_field_names_impl(val, Some(model))
}
fn normalize_field_names_impl(val: &mut serde_json::Value, model: Option<&str>) -> bool {
let mut changed = false;
match val {
serde_json::Value::Object(map) => {
let mut renames: Vec<(String, String)> = Vec::new();
for key in map.keys() {
let mut matched = false;
if let Some(model) = model {
for entry in MODEL_ALIASES {
if entry.model == Some(model) && key == entry.from {
if !map.contains_key(entry.to) {
renames.push((key.clone(), entry.to.to_owned()));
matched = true;
}
break;
}
}
}
if !matched {
for entry in FIELD_ALIASES {
if entry.model.is_none() && key == entry.from {
if !map.contains_key(entry.to) {
renames.push((key.clone(), entry.to.to_owned()));
}
break;
}
}
}
}
for (old_key, new_key) in &renames {
if let Some(v) = map.remove(old_key) {
map.insert(new_key.clone(), v);
changed = true;
}
}
let keys: Vec<String> = map.keys().cloned().collect();
for k in &keys {
if let Some(v) = map.get_mut(k) {
changed |= normalize_field_names_impl(v, model);
}
}
}
serde_json::Value::Array(arr) => {
for v in arr.iter_mut() {
changed |= normalize_field_names_impl(v, model);
}
}
_ => {}
}
changed
}
#[must_use]
pub fn resolve_alias(name: &str) -> Option<&'static str> {
FIELD_ALIASES
.iter()
.find(|entry| entry.model.is_none() && entry.from == name)
.map(|entry| entry.to)
}
#[must_use]
pub fn global_aliases() -> Vec<(&'static str, &'static str)> {
FIELD_ALIASES
.iter()
.filter(|e| e.model.is_none())
.map(|e| (e.from, e.to))
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;
#[test]
fn normalizes_filter_to_where() {
let mut val = json!({"filter": {"type": "comparison", "left": {"column": "age"}, "op": "gt", "right": {"value": 18}}});
assert!(normalize_field_names(&mut val));
assert!(val.get("where").is_some(), "filter should become where");
assert!(val.get("filter").is_none(), "filter should be removed");
}
#[test]
fn normalizes_projection_to_select() {
let mut val = json!({"projection": [{"type": "star"}], "from": {"table": "users"}});
assert!(normalize_field_names(&mut val));
assert!(val.get("select").is_some());
assert!(val.get("projection").is_none());
}
#[test]
fn normalizes_sort_to_order_by() {
let mut val = json!({"sort": [{"expr": {"column": "name"}, "descending": true}], "select": [{"type": "star"}], "from": {"table": "users"}});
assert!(normalize_field_names(&mut val));
assert!(val.get("order_by").is_some());
assert!(val.get("sort").is_none());
}
#[test]
fn normalizes_kind_to_type() {
let mut val = json!({"kind": "column_ref", "table": "users", "column": "id"});
assert!(normalize_field_names(&mut val));
assert_eq!(val.get("type").and_then(|v| v.as_str()), Some("column_ref"));
assert!(val.get("kind").is_none());
}
#[test]
fn normalizes_desc_to_descending() {
let mut val = json!({"expr": {"column": "name"}, "desc": true});
assert!(normalize_field_names(&mut val));
assert!(val.get("descending").is_some());
assert!(val.get("desc").is_none());
}
#[test]
fn does_not_overwrite_existing_canonical_field() {
let mut val = json!({"where": {"type": "comparison"}, "filter": {"type": "and"}});
assert!(!normalize_field_names(&mut val));
assert!(
val.get("filter").is_some(),
"filter stays when where already exists"
);
assert_eq!(
val.get("where")
.and_then(|v| v.get("type"))
.and_then(|v| v.as_str()),
Some("comparison")
);
}
#[test]
fn already_canonical_no_change() {
let mut val = json!({"select": [{"type": "star"}], "from": {"table": "users"}});
assert!(!normalize_field_names(&mut val));
}
#[test]
fn normalizes_recursively() {
let mut val = json!({
"select": [{"type": "star"}],
"from": {"table": "users"},
"filter": {
"type": "and",
"left": {"kind": "comparison", "field": "age", "operator": "gt", "right": {"value": 18}},
"right": {"kind": "comparison", "field": "status", "operator": "eq", "right": {"value": "active"}}
}
});
assert!(normalize_field_names(&mut val));
let where_obj = val.get("where").unwrap();
assert_eq!(where_obj.get("type").and_then(|v| v.as_str()), Some("and"));
let left = where_obj.get("left").unwrap();
assert_eq!(
left.get("type").and_then(|v| v.as_str()),
Some("comparison")
);
assert!(left.get("kind").is_none());
assert_eq!(left.get("column").and_then(|v| v.as_str()), Some("age"));
assert!(left.get("field").is_none());
assert_eq!(left.get("op").and_then(|v| v.as_str()), Some("gt"));
assert!(left.get("operator").is_none());
}
#[test]
fn resolve_alias_works() {
assert_eq!(resolve_alias("filter"), Some("where"));
assert_eq!(resolve_alias("projection"), Some("select"));
assert_eq!(resolve_alias("sort"), Some("order_by"));
assert_eq!(resolve_alias("where"), None); assert_eq!(resolve_alias("select"), None); assert_eq!(resolve_alias("nonexistent"), None);
}
#[test]
fn global_aliases_returns_all() {
let aliases = global_aliases();
assert!(aliases.contains(&("filter", "where")));
assert!(aliases.contains(&("projection", "select")));
assert!(aliases.contains(&("sort", "order_by")));
}
#[test]
fn model_specific_normalize() {
let mut val = json!({"filter": {"type": "comparison"}});
assert!(normalize_field_names_for_model(&mut val, "deepseek"));
assert!(val.get("where").is_some());
}
#[test]
fn normalizes_source_to_from() {
let mut val = json!({"select": [{"type": "star"}], "source": {"table": "users"}});
assert!(normalize_field_names(&mut val));
assert!(val.get("from").is_some());
assert!(val.get("source").is_none());
}
#[test]
fn normalizes_order_to_order_by() {
let mut val = json!({"select": [{"type": "star"}], "from": {"table": "users"}, "order": [{"expr": {"column": "name"}, "descending": true}]});
assert!(normalize_field_names(&mut val));
assert!(val.get("order_by").is_some());
assert!(val.get("order").is_none());
}
#[test]
fn normalizes_group_to_group_by() {
let mut val = json!({"select": [{"type": "star"}], "from": {"table": "users"}, "group": [{"column": "status"}]});
assert!(normalize_field_names(&mut val));
assert!(val.get("group_by").is_some());
assert!(val.get("group").is_none());
}
#[test]
fn empty_object_no_change() {
let mut val = json!({});
assert!(!normalize_field_names(&mut val));
}
#[test]
fn null_value_no_change() {
let mut val = serde_json::Value::Null;
assert!(!normalize_field_names(&mut val));
}
}