From e21a79df6fc65cf88db285c27978afcc3bdc9c3f Mon Sep 17 00:00:00 2001 From: stephenberry Date: Wed, 18 Dec 2024 15:39:28 +0000 Subject: [PATCH] Committing clang-format changes --- include/glaze/json/jmespath.hpp | 96 ++++++++++++++++++++------------- tests/jmespath/jmespath.cpp | 46 ++++++++-------- 2 files changed, 81 insertions(+), 61 deletions(-) diff --git a/include/glaze/json/jmespath.hpp b/include/glaze/json/jmespath.hpp index 50fbcfde5f..88cb3bf1f1 100644 --- a/include/glaze/json/jmespath.hpp +++ b/include/glaze/json/jmespath.hpp @@ -164,7 +164,7 @@ namespace glz // If no delimiter found, return the whole string as first token return {s, "", tokenization_error::none}; } - + inline constexpr tokenization_error finalize_tokens(std::vector& tokens) { std::vector final_tokens; @@ -239,10 +239,12 @@ namespace glz if (!remaining.empty() && (remaining.front() == '.' || remaining.front() == '|')) { return tokenization_error::unexpected_delimiter; } - } else { + } + else { return tokenization_error::unexpected_delimiter; } - } else { + } + else { break; } } @@ -284,12 +286,12 @@ namespace glz struct ArrayParseResult { bool is_array_access = false; // True if "key[...]" - bool error = false; // True if parsing encountered an error - std::string_view key; // The part before the first '[' + bool error = false; // True if parsing encountered an error + std::string_view key; // The part before the first '[' std::optional start; // For a single index or slice start - std::optional end; // For slice end - std::optional step; // For slice step - size_t colon_count = 0; // Number of ':' characters found inside the brackets + std::optional end; // For slice end + std::optional step; // For slice step + size_t colon_count = 0; // Number of ':' characters found inside the brackets }; inline constexpr std::optional parse_int(std::string_view s) @@ -367,7 +369,8 @@ namespace glz auto val = parse_int(parts[0]); if (!val.has_value()) { result.error = true; - } else { + } + else { result.start = val; } } @@ -377,7 +380,8 @@ namespace glz auto val = parse_int(parts[1]); if (!val.has_value()) { result.error = true; - } else { + } + else { result.end = val; } } @@ -387,7 +391,8 @@ namespace glz auto val = parse_int(parts[2]); if (!val.has_value()) { result.error = true; - } else { + } + else { result.step = val; } } @@ -398,7 +403,8 @@ namespace glz auto val = parse_int(inside); if (!val.has_value()) { result.error = true; - } else { + } + else { result.start = val; } } @@ -414,7 +420,7 @@ namespace glz return result; } } - + namespace detail { template @@ -423,15 +429,16 @@ namespace glz { ctx.error = error_code::syntax_error; } - + template - requires(Opts.format == JSON && readable_array_t) - inline void handle_slice(const jmespath::ArrayParseResult& decomposed_key, T&& value, context& ctx, auto&& it, auto&& end) + requires(Opts.format == JSON && readable_array_t) + inline void handle_slice(const jmespath::ArrayParseResult& decomposed_key, T&& value, context& ctx, auto&& it, + auto&& end) { GLZ_SKIP_WS(); // Determine slice parameters - int32_t step_idx = decomposed_key.step.value_or(1); + int32_t step_idx = decomposed_key.step.value_or(1); bool has_negative_index = (decomposed_key.start.value_or(0) < 0) || (decomposed_key.end.value_or(0) < 0); // If we have negative indices or step != 1, fall back to the original method (read all then slice) @@ -442,7 +449,8 @@ namespace glz if (*it == ']') { // empty array ++it; // consume ']' - } else { + } + else { while (true) { detail::read::template op(value.emplace_back(), ctx, it, end); if (bool(ctx.error)) [[unlikely]] @@ -470,7 +478,7 @@ namespace glz }; const int32_t start_idx = wrap_index(decomposed_key.start.value_or(0)); - const int32_t end_idx = wrap_index(decomposed_key.end.value_or(size)); + const int32_t end_idx = wrap_index(decomposed_key.end.value_or(size)); if (step_idx == 1) { if (start_idx < end_idx) { @@ -480,10 +488,12 @@ namespace glz if (static_cast(end_idx - start_idx) < value.size()) { value.erase(value.begin() + (end_idx - start_idx), value.end()); } - } else { + } + else { value.clear(); } - } else { + } + else { // For steps != 1 (or negative steps), the fallback path was already chosen. // Just apply the same logic as before. std::size_t dest = 0; @@ -491,7 +501,8 @@ namespace glz for (int32_t i = start_idx; i < end_idx; i += step_idx) { value[dest++] = std::move(value[i]); } - } else { + } + else { for (int32_t i = start_idx; i > end_idx; i += step_idx) { value[dest++] = std::move(value[i]); } @@ -524,12 +535,14 @@ namespace glz skip_value::op(ctx, it, end); if (bool(ctx.error)) [[unlikely]] return; - } else if (current_index >= start_idx && current_index < end_idx) { + } + else if (current_index >= start_idx && current_index < end_idx) { // Read this element into value detail::read::template op(value.emplace_back(), ctx, it, end); if (bool(ctx.error)) [[unlikely]] return; - } else { + } + else { // current_index >= end_idx, we can skip reading into value skip_value::op(ctx, it, end); if (bool(ctx.error)) [[unlikely]] @@ -563,7 +576,7 @@ namespace glz static constexpr auto N = tokens.size(); constexpr bool use_padded = resizable && non_const_buffer && !has_disable_padding(Options); - + static constexpr auto Opts = use_padded ? is_padded_on() : is_padded_off(); if constexpr (use_padded) { @@ -607,7 +620,7 @@ namespace glz // SINGLE INDEX SCENARIO (no slice, just an index) if constexpr (decomposed_key.start.has_value()) { constexpr auto n = decomposed_key.start.value(); - + if constexpr (I == (N - 1)) { // Skip until we reach the target element n for (int32_t i = 0; i < n; ++i) { @@ -627,7 +640,8 @@ namespace glz detail::read::template op(value, ctx, it, end); } else { - // Not the last token. We must still parse the element at index n so the next indexing can proceed. + // Not the last token. We must still parse the element at index n so the next indexing can + // proceed. for (int32_t i = 0; i < n; ++i) { skip_value::op(ctx, it, end); if (bool(ctx.error)) [[unlikely]] @@ -641,7 +655,8 @@ namespace glz GLZ_SKIP_WS(); } } - } else { + } + else { ctx.error = error_code::array_element_not_found; return; } @@ -699,7 +714,8 @@ namespace glz detail::read::template op(value, ctx, it, end); } return; - } else { + } + else { ctx.error = error_code::array_element_not_found; return; } @@ -767,20 +783,22 @@ namespace glz return {ctx.error, ctx.custom_error_message, size_t(it - start), ctx.includer_error}; } - + // A "compiled" jmespath expression, which can be pre-computed for efficient traversal struct jmespath_expression { std::string_view path{}; jmespath::tokenization_error error{}; std::vector tokens{}; // evaluated tokens - - jmespath_expression(const std::string_view input_path) noexcept : path(input_path) { + + jmespath_expression(const std::string_view input_path) noexcept : path(input_path) + { error = jmespath::tokenize_full_jmespath(path, tokens); } - + template - jmespath_expression(const char (&input_path)[N]) noexcept : path(input_path) { + jmespath_expression(const char (&input_path)[N]) noexcept : path(input_path) + { error = jmespath::tokenize_full_jmespath(path, tokens); } jmespath_expression(const jmespath_expression&) noexcept = default; @@ -798,7 +816,7 @@ namespace glz if (bool(expression.error)) { return {error_code::syntax_error, "JMESPath invalid expression"}; } - + const auto& tokens = expression.tokens; const auto N = tokens.size(); @@ -848,7 +866,7 @@ namespace glz // Single index scenario if (decomposed_key.start.has_value()) { const int32_t n = decomposed_key.start.value(); - + if (I == (N - 1)) { // Skip until we reach the target element n for (int32_t i = 0; i < n; ++i) { @@ -868,7 +886,8 @@ namespace glz detail::read::template op(value, ctx, it, end); } else { - // Not the last token. We must still parse the element at index n so the next indexing can proceed. + // Not the last token. We must still parse the element at index n so the next indexing can + // proceed. for (int32_t i = 0; i < n; ++i) { skip_value::op(ctx, it, end); if (bool(ctx.error)) [[unlikely]] @@ -939,7 +958,8 @@ namespace glz detail::read::template op(value, ctx, it, end); } return; - } else { + } + else { ctx.error = error_code::array_element_not_found; return; } diff --git a/tests/jmespath/jmespath.cpp b/tests/jmespath/jmespath.cpp index eda1916e7b..1cb26d5b43 100644 --- a/tests/jmespath/jmespath.cpp +++ b/tests/jmespath/jmespath.cpp @@ -28,19 +28,19 @@ struct Home suite jmespath_read_tests = [] { Home home{.family = {.father = {"Gilbert", "Fox", 28}, - .mother = {"Anne", "Fox", 30}, - .children = {{"Lilly", "Fox", 7}, {"Vincent", "Fox", 3}}}, - .address = "123 Maple Street"}; + .mother = {"Anne", "Fox", 30}, + .children = {{"Lilly", "Fox", 7}, {"Vincent", "Fox", 3}}}, + .address = "123 Maple Street"}; std::string buffer{}; expect(not glz::write_json(home, buffer)); - + "compile-time read_jmespath"_test = [&] { std::string first_name{}; auto ec = glz::read_jmespath<"family.father.first_name">(first_name, buffer); expect(not ec) << glz::format_error(ec, buffer); expect(first_name == "Gilbert"); - + std::string mother_last_name{}; ec = glz::read_jmespath<"family.mother.last_name">(mother_last_name, buffer); expect(not ec) << glz::format_error(ec, buffer); @@ -61,7 +61,7 @@ suite jmespath_read_tests = [] { expect(child.first_name == "Lilly"); expect(not glz::read_jmespath<"family.children[1]">(child, buffer)); expect(child.first_name == "Vincent"); - + Person non_existent_child{}; ec = glz::read_jmespath<"family.children[3]">(non_existent_child, buffer); expect(ec) << "Expected error for out-of-bounds index"; @@ -72,7 +72,7 @@ suite jmespath_read_tests = [] { auto ec = glz::read_jmespath("family.father.first_name", first_name, buffer); expect(not ec) << glz::format_error(ec, buffer); expect(first_name == "Gilbert"); - + std::string mother_last_name{}; ec = glz::read_jmespath("family.mother.last_name", mother_last_name, buffer); expect(not ec) << glz::format_error(ec, buffer); @@ -93,12 +93,12 @@ suite jmespath_read_tests = [] { expect(child.first_name == "Lilly"); expect(not glz::read_jmespath("family.children[1]", child, buffer)); expect(child.first_name == "Vincent"); - + Person non_existent_child{}; ec = glz::read_jmespath("family.children[3]", non_existent_child, buffer); expect(ec) << "Expected error for out-of-bounds index"; }; - + "pre-compiled run-time"_test = [&] { Person child{}; // A runtime expression can be pre-computed and saved for more efficient lookups @@ -106,11 +106,11 @@ suite jmespath_read_tests = [] { expect(not glz::read_jmespath(expression, child, buffer)); expect(child.first_name == "Lilly"); }; - + "compile-time error_handling"_test = [&] { std::string middle_name{}; auto ec = glz::read_jmespath<"family.father.middle_name">(middle_name, buffer); - //std::cout << glz::format_error(ec, buffer) << '\n'; + // std::cout << glz::format_error(ec, buffer) << '\n'; expect(ec) << "Expected error for non-existent field"; // Invalid JMESPath expression @@ -118,7 +118,7 @@ suite jmespath_read_tests = [] { // so invalid expressions would cause a compile-time error. // Therefore, runtime tests should cover invalid expressions. }; - + "run-time error_handling"_test = [&] { // Access non-existent field std::string middle_name{}; @@ -134,10 +134,10 @@ suite jmespath_read_tests = [] { suite jmespath_slice_tests = [] { "slice compile-time"_test = [] { - std::vector data{0,1,2,3,4,5,6,7,8,9}; + std::vector data{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; std::string buffer{}; expect(not glz::write_json(data, buffer)); - + std::vector slice{}; expect(not glz::read_jmespath<"[0:5]">(slice, buffer)); expect(slice.size() == 5); @@ -147,12 +147,12 @@ suite jmespath_slice_tests = [] { expect(slice[3] == 3); expect(slice[4] == 4); }; - + "slice run-time"_test = [] { - std::vector data{0,1,2,3,4,5,6,7,8,9}; + std::vector data{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; std::string buffer{}; expect(not glz::write_json(data, buffer)); - + std::vector slice{}; expect(not glz::read_jmespath("[0:5]", slice, buffer)); expect(slice.size() == 5); @@ -162,22 +162,22 @@ suite jmespath_slice_tests = [] { expect(slice[3] == 3); expect(slice[4] == 4); }; - + "slice compile-time multi-bracket"_test = [] { - std::vector> data{{1,2},{3,4,5},{6,7}}; + std::vector> data{{1, 2}, {3, 4, 5}, {6, 7}}; std::string buffer{}; expect(not glz::write_json(data, buffer)); - + int v{}; expect(not glz::read_jmespath<"[1][2]">(v, buffer)); expect(v == 5); }; - + "slice run-time multi-bracket"_test = [] { - std::vector> data{{1,2},{3,4,5},{6,7}}; + std::vector> data{{1, 2}, {3, 4, 5}, {6, 7}}; std::string buffer{}; expect(not glz::write_json(data, buffer)); - + int v{}; expect(not glz::read_jmespath("[1][2]", v, buffer)); expect(v == 5);