Skip to content

Commit

Permalink
Merge pull request #84 from hiltontj/hiltontj/83-func-as-arg-bug
Browse files Browse the repository at this point in the history
fix: bug found with new CTS
  • Loading branch information
hiltontj authored Mar 2, 2024
2 parents f2004fc + 950f8ec commit d6343ee
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 3 deletions.
4 changes: 4 additions & 0 deletions serde_json_path/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

# Unreleased

- **fixed**: bug preventing registered functions from being used as arguments to other functions ([#84])

[#84]: https://github.com/hiltontj/serde_json_path/pull/84

# 0.6.6 (23 February 2024)

- **docs**: update links to refer to RFC 9535 ([#81])
Expand Down
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
4 changes: 4 additions & 0 deletions serde_json_path_core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

# Unreleased

- **fixed**: bug preventing registered functions from being used as arguments to other functions ([#84])

[#84]: https://github.com/hiltontj/serde_json_path/pull/84

# 0.1.5 (23 February 2024)

- **docs**: update links to refer to RFC 9535 ([#81])
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
4 changes: 4 additions & 0 deletions serde_json_path_macros/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

# Unreleased

- **fixed**: bug preventing registered functions from being used as arguments to other functions ([#84])

[#84]: https://github.com/hiltontj/serde_json_path/pull/84