From 9b134b47b64dbbc5fef37ca309fe3f12ae63a94d Mon Sep 17 00:00:00 2001 From: Daniel Parker Date: Wed, 11 Dec 2024 14:32:10 -0500 Subject: [PATCH] custom functions doc --- doc/ref/jmespath/jmespath.md | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/doc/ref/jmespath/jmespath.md b/doc/ref/jmespath/jmespath.md index 70956473a..563ed76b7 100644 --- a/doc/ref/jmespath/jmespath.md +++ b/doc/ref/jmespath/jmespath.md @@ -164,12 +164,24 @@ namespace myspace { template class my_custom_functions : public jmespath::custom_functions { + using reference = const Json&; using pointer = const Json*; static thread_local size_t current_index; public: my_custom_functions() { + this->register_function("current_date_time", // function name + 0, // number of arguments + [](const jsoncons::span> params, + jmespath::dynamic_resources& resources, + std::error_code& ec) -> pointer + { + auto now = std::chrono::system_clock::now(); + auto milliseconds = std::chrono::duration_cast(now.time_since_epoch()); + return resources.make_json(milliseconds.count()); + } + ); this->register_function("current_index", // function name 0, // number of arguments [](const jsoncons::span> params, @@ -194,10 +206,10 @@ public: return resources.make_null(); } - const auto& context = params[0].value(); - const auto countValue = get_value(context, resources, params[1]); + reference context = params[0].value(); + reference countValue = get_value(context, resources, params[1]); const auto& expr = params[2].expression(); - const auto& argDefault = params[3]; + auto argDefault = params[3]; if (!countValue.is_number()) { @@ -243,8 +255,8 @@ public: return resources.make_null(); } - const auto arg0 = params[0].value(); - const auto arg1 = params[1].value(); + reference arg0 = params[0].value(); + reference arg1 = params[1].value(); if (!(arg0.is_number() && arg1.is_number())) { ec = jmespath::jmespath_errc::invalid_argument; @@ -265,7 +277,7 @@ public: ); } - static const Json& get_value(const Json& context, jmespath::dynamic_resources& resources, + static reference get_value(reference context, jmespath::dynamic_resources& resources, const jmespath::parameter& param) { if (param.is_expression()) @@ -277,7 +289,7 @@ public: } else { - const Json& value = param.value(); + reference value = param.value(); return value; } }