Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified about.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 4 additions & 16 deletions lib/headers/expressions/container.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,10 @@
#include <expressions/result.hpp>

namespace expressions {
class container : public std::enable_shared_from_this<container> {
struct container {
std::string regex_;
std::vector<std::string> arguments_{};

public:
/**
* Expression constructor
*
* @param regex
* @param arguments
*/
container(
std::string regex,
std::vector<std::string> arguments
);

/**
* Get the regex
*
Expand All @@ -46,16 +34,16 @@ namespace expressions {
* @param input
* @return
*/
std::shared_ptr<result>
query(const std::string &input);
result
query(const std::string &input) const;

/**
* Creates a expression from strings
*
* @param input
* @return std::shared_ptr<expression>
*/
static std::shared_ptr<container>
static container
from_string(const std::string &input);
};
}
24 changes: 11 additions & 13 deletions lib/headers/expressions/result.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,13 @@

#include <memory>
#include <unordered_map>
#include <optional>

namespace expressions {
class result : public std::enable_shared_from_this<result> {
struct result {
bool _matches = false;
std::unordered_map<std::string, std::string> _bindings;

public:
/**
* Expression result constructor
*
* @param matches
* @param bindings
*/
result(
bool matches,
std::unordered_map<std::string, std::string> bindings
);

/**
* Get matches
*
Expand All @@ -35,5 +24,14 @@ namespace expressions {
*/
std::unordered_map<std::string, std::string>
bindings() const;

/**
* Get parameter
*
* @param name
* @return
*/
std::string
get(const std::string & name) const;
};
}
22 changes: 9 additions & 13 deletions lib/sources/container.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
#include <expressions/container.hpp>

namespace expressions {
container::container(std::string regex, std::vector<std::string> arguments)
: regex_(std::move(regex)), arguments_(std::move(arguments)) {
}

std::string container::get_regex() const {
return regex_;
}
Expand All @@ -13,7 +9,7 @@ namespace expressions {
return arguments_;
}

std::shared_ptr<result> container::query(const std::string &input) {
result container::query(const std::string &input) const {
std::unordered_map<std::string, std::string> _bindings;
const std::regex _pattern(regex_);
bool _matches = false;
Expand All @@ -26,10 +22,10 @@ namespace expressions {
++_iterator;
}
}
return std::make_shared<result>(_matches, _bindings);
return { ._matches = _matches, ._bindings = _bindings };
}

std::shared_ptr<container> container::from_string(const std::string &input) {
container container::from_string(const std::string &input) {
std::size_t _open = input.find('{');
std::size_t _close = input.find('}');
std::size_t _position = 0;
Expand All @@ -38,26 +34,26 @@ namespace expressions {
std::string _regex;

if (_open == std::string::npos && _close == std::string::npos)
return std::make_shared<container>(std::string{input.data()}, _arguments);
return { .regex_ = std::string{ input }, .arguments_ = _arguments };

while (_open != std::string::npos && _close != std::string::npos) {
_regex.append(input.substr(_position, _open - _position));
std::string _value{input.substr(_open + 1, _close - _open - 1)};

if (std::find(_arguments.begin(), _arguments.end(), _value) != _arguments.end())
throw std::runtime_error("groups can't be repeated ... ");
throw std::runtime_error("The provided input contains repeated arguments.");

_regex.append(R"(([a-zA-Z0-9\-_]+))");
_arguments.emplace_back(_value);

_position = _close + 1;
_open = input.find('{', _close);
_close = input.find('}', _open);

if (_open == std::string::npos && _close == std::string::npos && _position != input.size())
_regex.append(input.substr(_position, input.size() - _position));
}

return std::make_shared<container>(_regex, _arguments);
if (_position != input.size())
_regex.append(input.substr(_position, input.size() - _position));

return { .regex_ = _regex , .arguments_ = _arguments };
}
}
11 changes: 7 additions & 4 deletions lib/sources/result.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
#include <expressions/result.hpp>

namespace expressions {
result::result(const bool matches, std::unordered_map<std::string, std::string> bindings)
: _matches(matches), _bindings(std::move(bindings)) {
}

bool result::matches() const {
return _matches;
}

std::unordered_map<std::string, std::string> result::bindings() const {
return _bindings;
}

std::string
result::get(const std::string &name) const {
if (_bindings.contains(name))
return _bindings.at(name);
throw std::runtime_error("The provided argument doesn't exists");
}
}
28 changes: 15 additions & 13 deletions tests/implementation_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,23 @@ TEST(Expressions, Assertions) {
using namespace expressions;

const auto _non_empty_expression = container::from_string("/users/{user}/details");
ASSERT_FALSE(_non_empty_expression->get_arguments().empty());
ASSERT_EQ(_non_empty_expression->get_arguments().size(), 1);
ASSERT_EQ(_non_empty_expression->get_arguments().at(0), "user");

ASSERT_FALSE(_non_empty_expression.get_arguments().empty());
ASSERT_EQ(_non_empty_expression.get_arguments().size(), 1);
ASSERT_EQ(_non_empty_expression.get_arguments().at(0), "user");

const auto _empty_expression = container::from_string("/ping");
ASSERT_TRUE(_empty_expression->get_arguments().empty());
ASSERT_EQ(_empty_expression->get_regex(), "/ping");
ASSERT_TRUE(_empty_expression.get_arguments().empty());
ASSERT_EQ(_empty_expression.get_regex(), "/ping");

const auto _non_empty_string_expression_result = _non_empty_expression.query("/users/80bdc6d1-524e-411a-b316-976a65a3ed3c/details");

const auto _non_empty_string_expression_result = _non_empty_expression->query("/users/80bdc6d1-524e-411a-b316-976a65a3ed3c/details");
ASSERT_TRUE(_non_empty_string_expression_result->matches());
ASSERT_FALSE(_non_empty_string_expression_result->bindings().empty());
ASSERT_EQ(_non_empty_string_expression_result->bindings().at("user"), "80bdc6d1-524e-411a-b316-976a65a3ed3c");
ASSERT_TRUE(_non_empty_string_expression_result.matches());
ASSERT_FALSE(_non_empty_string_expression_result.bindings().empty());
ASSERT_EQ(_non_empty_string_expression_result.get("user"), "80bdc6d1-524e-411a-b316-976a65a3ed3c");

const auto _non_empty_integer_expression_result = _non_empty_expression->query("/users/1337/details");
ASSERT_TRUE(_non_empty_integer_expression_result->matches());
ASSERT_FALSE(_non_empty_integer_expression_result->bindings().empty());
ASSERT_EQ(_non_empty_integer_expression_result->bindings().at("user"), "1337");
const auto _non_empty_integer_expression_result = _non_empty_expression.query("/users/1337/details");
ASSERT_TRUE(_non_empty_integer_expression_result.matches());
ASSERT_FALSE(_non_empty_integer_expression_result.bindings().empty());
ASSERT_EQ(_non_empty_integer_expression_result.get("user"), "1337");
}