From d1039b8d3e44f77b0e1be3a3062da8798112210c Mon Sep 17 00:00:00 2001 From: Juan Cruz Viotti Date: Tue, 28 Jan 2025 14:22:49 -0400 Subject: [PATCH] Revise Pointer and YAML public interfaces (#1498) Signed-off-by: Juan Cruz Viotti --- .../sourcemeta/core/jsonpointer_position.h | 4 ++-- src/core/jsonpointer/position.cc | 15 +++++++++------ src/core/yaml/include/sourcemeta/core/yaml.h | 8 ++++---- src/core/yaml/yaml.cc | 6 +++--- test/jsonpointer/jsonpointer_position_test.cc | 10 +++++----- test/yaml/yaml_parse_test.cc | 18 +++++++++--------- 6 files changed, 32 insertions(+), 29 deletions(-) diff --git a/src/core/jsonpointer/include/sourcemeta/core/jsonpointer_position.h b/src/core/jsonpointer/include/sourcemeta/core/jsonpointer_position.h index b0e4663e8..6af0c84e0 100644 --- a/src/core/jsonpointer/include/sourcemeta/core/jsonpointer_position.h +++ b/src/core/jsonpointer/include/sourcemeta/core/jsonpointer_position.h @@ -27,7 +27,7 @@ namespace sourcemeta::core { /// #include /// /// const auto input{"{\n \"foo\": \"bar\"\n}"};; -/// sourcemeta::core::PositionTracker tracker; +/// sourcemeta::core::PointerPositionTracker tracker; /// sourcemeta::core::parse_json(stream, std::ref(tracker)); /// assert(tracker.size() == 2); /// const auto foo{tracker.get(sourcemeta::core::Pointer{"foo"})}; @@ -41,7 +41,7 @@ namespace sourcemeta::core { /// assert(std::get<3>(foo.value()) == 2); /// assert(std::get<4>(foo.value()) == 14); /// ``` -class SOURCEMETA_CORE_JSONPOINTER_EXPORT PositionTracker { +class SOURCEMETA_CORE_JSONPOINTER_EXPORT PointerPositionTracker { public: using Pointer = GenericPointer>; using Position = diff --git a/src/core/jsonpointer/position.cc b/src/core/jsonpointer/position.cc index c43907a97..afe9e1386 100644 --- a/src/core/jsonpointer/position.cc +++ b/src/core/jsonpointer/position.cc @@ -4,10 +4,11 @@ namespace sourcemeta::core { -auto PositionTracker::operator()(const JSON::ParsePhase phase, const JSON::Type, - const std::uint64_t line, - const std::uint64_t column, const JSON &value) - -> void { +auto PointerPositionTracker::operator()(const JSON::ParsePhase phase, + const JSON::Type, + const std::uint64_t line, + const std::uint64_t column, + const JSON &value) -> void { if (phase == JSON::ParsePhase::Pre) { this->stack.push({line, column}); if (value.is_string()) { @@ -28,13 +29,15 @@ auto PositionTracker::operator()(const JSON::ParsePhase phase, const JSON::Type, } } -auto PositionTracker::get(const Pointer &pointer) const +auto PointerPositionTracker::get(const Pointer &pointer) const -> std::optional { const auto result{this->data.find(pointer)}; return result == this->data.cend() ? std::nullopt : std::optional{result->second}; } -auto PositionTracker::size() const -> std::size_t { return this->data.size(); } +auto PointerPositionTracker::size() const -> std::size_t { + return this->data.size(); +} } // namespace sourcemeta::core diff --git a/src/core/yaml/include/sourcemeta/core/yaml.h b/src/core/yaml/include/sourcemeta/core/yaml.h index 7986119f9..5df8ffb61 100644 --- a/src/core/yaml/include/sourcemeta/core/yaml.h +++ b/src/core/yaml/include/sourcemeta/core/yaml.h @@ -35,12 +35,12 @@ namespace sourcemeta::core { /// /// const std::string input{"hello: world"}; /// const sourcemeta::core::JSON document = -/// sourcemeta::core::from_yaml(input); +/// sourcemeta::core::parse_yaml(input); /// sourcemeta::core::prettify(document, std::cerr); /// std::cerr << "\n"; /// ``` SOURCEMETA_CORE_YAML_EXPORT -auto from_yaml(const JSON::String &input) -> JSON; +auto parse_yaml(const JSON::String &input) -> JSON; /// @ingroup yaml /// @@ -56,12 +56,12 @@ auto from_yaml(const JSON::String &input) -> JSON; /// /// const std::filesystem::path path{"test.yaml"}; /// const sourcemeta::core::JSON document = -/// sourcemeta::core::from_yaml(path); +/// sourcemeta::core::read_yaml(path); /// sourcemeta::core::prettify(document, std::cerr); /// std::cerr << "\n"; /// ``` SOURCEMETA_CORE_YAML_EXPORT -auto from_yaml(const std::filesystem::path &path) -> JSON; +auto read_yaml(const std::filesystem::path &path) -> JSON; } // namespace sourcemeta::core diff --git a/src/core/yaml/yaml.cc b/src/core/yaml/yaml.cc index d937f3b06..c77b90bb8 100644 --- a/src/core/yaml/yaml.cc +++ b/src/core/yaml/yaml.cc @@ -91,7 +91,7 @@ static auto internal_parse_json(yaml_parser_t *parser) namespace sourcemeta::core { -auto from_yaml(const JSON::String &input) -> JSON { +auto parse_yaml(const JSON::String &input) -> JSON { yaml_parser_t parser; if (!yaml_parser_initialize(&parser)) { throw YAMLError("Could not initialize the YAML parser"); @@ -112,11 +112,11 @@ auto from_yaml(const JSON::String &input) -> JSON { } } -auto from_yaml(const std::filesystem::path &path) -> JSON { +auto read_yaml(const std::filesystem::path &path) -> JSON { auto stream = read_file(path); std::ostringstream buffer; buffer << stream.rdbuf(); - return from_yaml(buffer.str()); + return parse_yaml(buffer.str()); } } // namespace sourcemeta::core diff --git a/test/jsonpointer/jsonpointer_position_test.cc b/test/jsonpointer/jsonpointer_position_test.cc index bf52a7d23..1d6f8f99d 100644 --- a/test/jsonpointer/jsonpointer_position_test.cc +++ b/test/jsonpointer/jsonpointer_position_test.cc @@ -12,7 +12,7 @@ TEST(JSONPointer_position, track_1) { } ])JSON"}; - sourcemeta::core::PositionTracker tracker; + sourcemeta::core::PointerPositionTracker tracker; sourcemeta::core::parse_json(input, std::ref(tracker)); EXPECT_EQ(tracker.size(), 4); @@ -20,20 +20,20 @@ TEST(JSONPointer_position, track_1) { sourcemeta::core::Pointer pointer_1; EXPECT_TRUE(tracker.get(pointer_1).has_value()); EXPECT_EQ(tracker.get(pointer_1).value(), - sourcemeta::core::PositionTracker::Position({1, 1, 7, 1})); + sourcemeta::core::PointerPositionTracker::Position({1, 1, 7, 1})); sourcemeta::core::Pointer pointer_2{0}; EXPECT_TRUE(tracker.get(pointer_2).has_value()); EXPECT_EQ(tracker.get(pointer_2).value(), - sourcemeta::core::PositionTracker::Position({2, 3, 6, 3})); + sourcemeta::core::PointerPositionTracker::Position({2, 3, 6, 3})); sourcemeta::core::Pointer pointer_3{0, "foo"}; EXPECT_TRUE(tracker.get(pointer_3).has_value()); EXPECT_EQ(tracker.get(pointer_3).value(), - sourcemeta::core::PositionTracker::Position({3, 12, 5, 5})); + sourcemeta::core::PointerPositionTracker::Position({3, 12, 5, 5})); sourcemeta::core::Pointer pointer_4{0, "foo", "bar"}; EXPECT_TRUE(tracker.get(pointer_4).has_value()); EXPECT_EQ(tracker.get(pointer_4).value(), - sourcemeta::core::PositionTracker::Position({4, 14, 4, 14})); + sourcemeta::core::PointerPositionTracker::Position({4, 14, 4, 14})); } diff --git a/test/yaml/yaml_parse_test.cc b/test/yaml/yaml_parse_test.cc index c585ffd9a..fbe5918f9 100644 --- a/test/yaml/yaml_parse_test.cc +++ b/test/yaml/yaml_parse_test.cc @@ -5,7 +5,7 @@ TEST(YAML_parse, scalar_1) { const std::string input{"1"}; - const auto result{sourcemeta::core::from_yaml(input)}; + const auto result{sourcemeta::core::parse_yaml(input)}; const sourcemeta::core::JSON expected{1}; EXPECT_EQ(result, expected); } @@ -13,7 +13,7 @@ TEST(YAML_parse, scalar_1) { TEST(YAML_parse, object_1) { const std::string input{"hello: world\nfoo: 1\nbar: true"}; - const auto result{sourcemeta::core::from_yaml(input)}; + const auto result{sourcemeta::core::parse_yaml(input)}; const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ "hello": "world", @@ -27,7 +27,7 @@ TEST(YAML_parse, object_1) { TEST(YAML_parse, object_2) { const std::string input{"foo: >\n bar\n baz"}; - const auto result{sourcemeta::core::from_yaml(input)}; + const auto result{sourcemeta::core::parse_yaml(input)}; const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ "foo": "bar baz" @@ -39,7 +39,7 @@ TEST(YAML_parse, object_2) { TEST(YAML_parse, array_1) { const std::string input{"- foo\n- true"}; - const auto result{sourcemeta::core::from_yaml(input)}; + const auto result{sourcemeta::core::parse_yaml(input)}; const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON([ "foo", true ])JSON"); @@ -49,24 +49,24 @@ TEST(YAML_parse, array_1) { TEST(YAML_parse, empty) { const std::string input{""}; - EXPECT_THROW(sourcemeta::core::from_yaml(input), + EXPECT_THROW(sourcemeta::core::parse_yaml(input), sourcemeta::core::YAMLParseError); } TEST(YAML_parse, blank) { const std::string input{" "}; - EXPECT_THROW(sourcemeta::core::from_yaml(input), + EXPECT_THROW(sourcemeta::core::parse_yaml(input), sourcemeta::core::YAMLParseError); } TEST(YAML_parse, invalid_1) { const std::string input{"{ xx"}; - EXPECT_THROW(sourcemeta::core::from_yaml(input), + EXPECT_THROW(sourcemeta::core::parse_yaml(input), sourcemeta::core::YAMLParseError); } TEST(YAML_parse, stub_test_1) { - const auto result{sourcemeta::core::from_yaml( + const auto result{sourcemeta::core::read_yaml( std::filesystem::path{STUBS_PATH} / "test_1.yaml")}; const sourcemeta::core::JSON expected = sourcemeta::core::parse_json(R"JSON({ "foo": "bar", @@ -77,7 +77,7 @@ TEST(YAML_parse, stub_test_1) { } TEST(YAML_parse, file_not_exists) { - EXPECT_THROW(sourcemeta::core::from_yaml(std::filesystem::path{STUBS_PATH} / + EXPECT_THROW(sourcemeta::core::read_yaml(std::filesystem::path{STUBS_PATH} / "not_exists.yaml"), std::filesystem::filesystem_error); }