Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 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
3 changes: 2 additions & 1 deletion src/facade/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
add_library(dfly_facade dragonfly_listener.cc dragonfly_connection.cc facade.cc
memcache_parser.cc redis_parser.cc reply_builder.cc op_status.cc
reply_capture.cc resp_expr.cc)
reply_capture.cc resp_expr.cc cmd_arg_parser.cc)

if (DF_USE_SSL)
set(TLS_LIB tls_lib)
Expand All @@ -16,6 +16,7 @@ cxx_link(facade_test dfly_facade gtest_main_ext)
cxx_test(memcache_parser_test dfly_facade LABELS DFLY)
cxx_test(redis_parser_test facade_test LABELS DFLY)
cxx_test(reply_builder_test facade_test LABELS DFLY)
cxx_test(cmd_arg_parser_test facade_test LABELS DFLY)

add_executable(ok_backend ok_main.cc)
cxx_link(ok_backend dfly_facade)
67 changes: 67 additions & 0 deletions src/facade/cmd_arg_parser.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright 2023, DragonflyDB authors. All rights reserved.
// See LICENSE for licensing terms.
//

#include "facade/cmd_arg_parser.h"

#include <absl/strings/ascii.h>
#include <absl/strings/match.h>
#include <absl/strings/numbers.h>

#include "base/logging.h"
#include "facade/error.h"
Comment on lines +6 to +12
Copy link
Contributor Author

@dranikpg dranikpg Aug 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved all function longer than 3 lines and all functions that have some sependencies (absl and our stuff)


namespace facade {

CmdArgParser::CheckProxy::operator bool() const {
if (idx_ >= parser_->args_.size())
return false;

std::string_view arg = parser_->SafeSV(idx_);
if ((!ignore_case_ && arg != tag_) || (ignore_case_ && !absl::EqualsIgnoreCase(arg, tag_)))
return false;

if (idx_ + expect_tail_ >= parser_->args_.size()) {
parser_->Report(SHORT_OPT_TAIL, idx_);
return false;
}

parser_->cur_i_++;

if (size_t uidx = idx_ + expect_tail_ + 1; next_upper_ && uidx < parser_->args_.size())
parser_->ToUpper(uidx);

return true;
}

template <typename T> T CmdArgParser::NextProxy::Int() {
T out;
if (absl::SimpleAtoi(operator std::string_view(), &out))
return out;
parser_->Report(INVALID_INT, idx_);
return T{0};
}

template uint64_t CmdArgParser::NextProxy::Int<uint64_t>();
template int64_t CmdArgParser::NextProxy::Int<int64_t>();

ErrorReply CmdArgParser::ErrorInfo::MakeReply() const {
switch (type) {
case INVALID_INT:
return ErrorReply{kInvalidIntErr};
default:
return ErrorReply{kSyntaxErr};
};
return ErrorReply{kSyntaxErr};
}

CmdArgParser::~CmdArgParser() {
DCHECK(!error_.has_value()) << "Parsing error occured but not checked";
}

void CmdArgParser::ToUpper(size_t i) {
for (auto& c : args_[i])
c = absl::ascii_toupper(c);
}

} // namespace facade
201 changes: 201 additions & 0 deletions src/facade/cmd_arg_parser.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
// Copyright 2023, DragonflyDB authors. All rights reserved.
// See LICENSE for licensing terms.
//

#pragma once

#include <optional>
#include <string_view>

#include "facade/facade_types.h"

namespace facade {

// Utility class for easily parsing command options from argument lists.
struct CmdArgParser {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it actually ok to keep this header only?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, probably not (noticing 2s after giving the approval)

enum ErrorType {
OUT_OF_BOUNDS,
SHORT_OPT_TAIL,
INVALID_INT,
INVALID_CASES,
};

struct NextProxy;

template <typename T> struct CaseProxy {
operator T();

CaseProxy Case(std::string_view tag, T value);

private:
friend struct NextProxy;

CaseProxy(CmdArgParser* parser, size_t idx) : parser_{parser}, idx_{idx} {
}

CmdArgParser* parser_;
size_t idx_;
std::optional<T> value_;
};

struct NextProxy {
operator std::string_view() {
return parser_->SafeSV(idx_);
}

operator std::string() {
return std::string{operator std::string_view()};
}

template <typename T> T Int();

// Detect value based on cases.
// Returns default if the argument is not present among the cases list,
// and reports an error.
template <typename T> auto Case(std::string_view tag, T value) {
return CaseProxy<T>{parser_, idx_}.Case(tag, value);
}

private:
friend struct CmdArgParser;

NextProxy(CmdArgParser* parser, size_t idx) : parser_{parser}, idx_{idx} {
}

CmdArgParser* parser_;
size_t idx_;
};

struct CheckProxy {
explicit operator bool() const;

// Expect the tag to be followed by a number of arguments.
// Reports an error if the tag is matched but the condition is not met.
CheckProxy& ExpectTail(size_t tail) {
expect_tail_ = tail;
return *this;
}

// Call ToUpper on the next value after the flag and its expected tail.
CheckProxy& NextUpper() {
next_upper_ = true;
return *this;
}

CheckProxy& IgnoreCase() {
ignore_case_ = true;
return *this;
}

private:
friend struct CmdArgParser;

CheckProxy(CmdArgParser* parser, std::string_view tag, size_t idx)
: parser_{parser}, tag_{tag}, idx_{idx} {
}

CmdArgParser* parser_;
std::string_view tag_;
size_t idx_;
size_t expect_tail_ = 0;
bool next_upper_ = false;
bool ignore_case_ = false;
};

struct ErrorInfo {
ErrorType type;
size_t index;

ErrorReply MakeReply() const;
};

public:
CmdArgParser(CmdArgList args) : args_{args} {
}

// Debug asserts sure error was consumed
~CmdArgParser();

// Get next value without consuming it
NextProxy Peek() {
return NextProxy(this, cur_i_);
}

// Consume next value
NextProxy Next() {
if (cur_i_ >= args_.size())
Report(OUT_OF_BOUNDS, cur_i_);
return NextProxy{this, cur_i_++};
}

// Check if the next value if equal to a specific tag. If equal, its consumed.
CheckProxy Check(std::string_view tag) {
return CheckProxy(this, tag, cur_i_);
}

// Skip specified number of arguments
CmdArgParser& Skip(size_t n) {
cur_i_ += n;
return *this;
}

// In-place convert the next argument to uppercase
CmdArgParser& ToUpper() {
if (cur_i_ < args_.size())
ToUpper(cur_i_);
return *this;
}

// Return remaining arguments
CmdArgList Tail() const {
return args_.subspan(cur_i_);
}

// Return true if arguments are left and no errors occured
bool HasNext() {
return cur_i_ < args_.size() && !error_;
}

// Get optional error if occured
std::optional<ErrorInfo> Error() {
auto out = std::move(error_);
error_.reset();
return out;
}

private:
std::string_view SafeSV(size_t i) const {
if (i >= args_.size())
return "";
return ToSV(args_[i]);
}

void Report(ErrorType type, size_t idx) {
if (!error_)
error_ = {type, idx};
}

void ToUpper(size_t i);

private:
size_t cur_i_ = 0;
CmdArgList args_;

std::optional<ErrorInfo> error_;
};

template <typename T> CmdArgParser::CaseProxy<T>::operator T() {
if (!value_)
parser_->Report(INVALID_CASES, idx_);
return value_.value_or(T{});
}

template <typename T>
CmdArgParser::CaseProxy<T> CmdArgParser::CaseProxy<T>::Case(std::string_view tag, T value) {
std::string_view arg = parser_->SafeSV(idx_);
if (arg == tag)
value_ = std::move(value);
return *this;
}

} // namespace facade
Loading