Skip to content

Commit

Permalink
fix: bug found with new CTS
Browse files Browse the repository at this point in the history
  • Loading branch information
hiltontj committed Feb 26, 2024
1 parent f2004fc commit b0da168
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
7 changes: 7 additions & 0 deletions serde_json_path/src/parser/selector/function/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ use serde_json_path_core::spec::functions::{Function, LogicalType, NodesType, Va
///
/// These come directly from the JSONPath specification, which includes a registry of standardized
/// functions.
///
/// # Note
///
/// There is a function in `serde_json_path_core/src/spec/functions.rs` that gives
/// the return type for each function registered here. When adding new functions to
/// the register, i.e., when new functions are standardized, the function there needs
/// to be updated too.
pub(crate) static REGISTRY: Lazy<HashMap<&'static str, &'static Function>> = Lazy::new(|| {
let mut m = HashMap::new();
m.insert("length", &LENGTH_FUNC);
Expand Down
26 changes: 24 additions & 2 deletions serde_json_path_core/src/spec/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -618,14 +618,36 @@ impl FunctionExprArg {
return Ok(f.result_type);
}
}
Err(FunctionValidationError::Undefined {
name: func.name.to_owned(),
registered_function_result_type(&func.name).ok_or_else(|| {
FunctionValidationError::Undefined {
name: func.name.to_owned(),
}
})
}
}
}
}

/// Get the result type of a registered function
///
/// This is a hack, but must be done, because the REGISTRY defined in `serde_json_path`
/// is not accessible here. Therefore, this must also be maintained when new functions
/// are added to the registry in the standard.
///
/// An alternative would be to define the registry of functions in core, we would then
/// just not have the convenience macro for defining them, along with their validators
/// and evaluators.
fn registered_function_result_type(name: &str) -> Option<FunctionArgType> {
match name {
"length" => Some(FunctionArgType::Value),
"count" => Some(FunctionArgType::Value),
"match" => Some(FunctionArgType::Logical),
"search" => Some(FunctionArgType::Logical),
"value" => Some(FunctionArgType::Value),
_ => None,
}
}

/// Function argument types
///
/// This is used to describe the type of a function argument to determine if it will be valid as a
Expand Down

0 comments on commit b0da168

Please sign in to comment.