diff --git a/src/tags_handlers/insertion_tags_handler.cc b/src/tags_handlers/insertion_tags_handler.cc new file mode 100644 index 0000000..81c9f5e --- /dev/null +++ b/src/tags_handlers/insertion_tags_handler.cc @@ -0,0 +1,21 @@ +#include "tags_handlers/insertion_tags_handler.h" +#include "tags_handlers_errors.h" + +using namespace opustags; + +InsertionTagsHandler::InsertionTagsHandler( + const int streamno, + const std::string &tag_key, + const std::string &tag_value) + : StreamTagsHandler(streamno), tag_key(tag_key), tag_value(tag_value) +{ +} + +bool InsertionTagsHandler::edit_impl(Tags &tags) +{ + if (tags.find(tag_key) != tags.end()) + throw TagAlreadyExistsError(tag_key); + + tags[tag_key] = tag_value; + return true; +} diff --git a/src/tags_handlers/insertion_tags_handler.h b/src/tags_handlers/insertion_tags_handler.h new file mode 100644 index 0000000..889cc9d --- /dev/null +++ b/src/tags_handlers/insertion_tags_handler.h @@ -0,0 +1,23 @@ +#pragma once + +#include "tags_handlers/stream_tags_handler.h" + +namespace opustags { + + class InsertionTagsHandler : public StreamTagsHandler + { + public: + InsertionTagsHandler( + const int streamno, + const std::string &tag_key, + const std::string &tag_value); + + protected: + bool edit_impl(Tags &) override; + + private: + const std::string tag_key; + const std::string tag_value; + }; + +} diff --git a/src/tags_handlers_errors.cc b/src/tags_handlers_errors.cc new file mode 100644 index 0000000..1f33c6d --- /dev/null +++ b/src/tags_handlers_errors.cc @@ -0,0 +1,8 @@ +#include "tags_handlers_errors.h" + +using namespace opustags; + +TagAlreadyExistsError::TagAlreadyExistsError(const std::string &tag_key) + : std::runtime_error("Tag already exists: " + tag_key) +{ +} diff --git a/src/tags_handlers_errors.h b/src/tags_handlers_errors.h new file mode 100644 index 0000000..99e5d56 --- /dev/null +++ b/src/tags_handlers_errors.h @@ -0,0 +1,12 @@ +#pragma once + +#include + +namespace opustags { + + struct TagAlreadyExistsError : std::runtime_error + { + TagAlreadyExistsError(const std::string &tag_key); + }; + +} diff --git a/tests/tags_handlers/insertion_tags_handler_test.cc b/tests/tags_handlers/insertion_tags_handler_test.cc new file mode 100644 index 0000000..9167932 --- /dev/null +++ b/tests/tags_handlers/insertion_tags_handler_test.cc @@ -0,0 +1,19 @@ +#include "tags_handlers/insertion_tags_handler.h" +#include "catch.h" + +using namespace opustags; + +TEST_CASE("Insertion tags handler test") +{ + Tags tags; + const auto streamno = 1; + const auto expected_tag_key = "tag_key"; + const auto expected_tag_value = "tag_value"; + InsertionTagsHandler handler( + streamno, expected_tag_key, expected_tag_value); + + REQUIRE(handler.edit(streamno, tags)); + REQUIRE(tags.size() == 1); + REQUIRE(tags[expected_tag_key] == expected_tag_value); + REQUIRE_THROWS(handler.edit(streamno, tags)); +}