Skip to content

Commit

Permalink
feat: protobuf: add support for optional keyword (#665)
Browse files Browse the repository at this point in the history
* feat: protobuf: add support for optional keyword

* Implement serializing/deserializing all fields

* Remove duplication

* Add _ as token for an empty optional value
  • Loading branch information
richardapeters authored Jul 3, 2024
1 parent 0fbcc1b commit b33ed06
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
29 changes: 26 additions & 3 deletions services/echo_console/Console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,20 @@ namespace application
return false;
}

Underscore::Underscore(std::size_t index)
: index(index)
{}

bool Underscore::operator==(const Underscore&) const
{
return true;
}

bool Underscore::operator!=(const Underscore&) const
{
return false;
}

LeftBrace::LeftBrace(std::size_t index)
: index(index)
{}
Expand Down Expand Up @@ -211,6 +225,8 @@ namespace application
return ConsoleToken::Comma(parseIndex++);
else if (line[parseIndex] == '.')
return ConsoleToken::Dot(parseIndex++);
else if (line[parseIndex] == '_')
return ConsoleToken::Underscore(parseIndex++);
else if (line[parseIndex] == '{')
return ConsoleToken::LeftBrace(parseIndex++);
else if (line[parseIndex] == '}')
Expand Down Expand Up @@ -851,6 +867,11 @@ namespace application
throw ConsoleExceptions::SyntaxError{ value.index };
}

MessageTokens::MessageTokenValue operator()(ConsoleToken::Underscore value) const
{
return Empty{};
}

MessageTokens::MessageTokenValue operator()(ConsoleToken::LeftBrace) const
{
invocation.currentToken = invocation.tokenizer.Token();
Expand Down Expand Up @@ -1112,9 +1133,11 @@ namespace application

void VisitOptional(const EchoFieldOptional& field) override
{
// Todo: How to specify the absense of an optional value?
EncodeFieldVisitor visitor(value, valueIndex, formatter, methodInvocation);
field.type->Accept(visitor);
if (!value.Is<Empty>())
{
EncodeFieldVisitor visitor(value, valueIndex, formatter, methodInvocation);
field.type->Accept(visitor);
}
}

void VisitRepeated(const EchoFieldRepeated& field) override
Expand Down
18 changes: 16 additions & 2 deletions services/echo_console/Console.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,17 @@ namespace application
std::size_t index;
};

class Underscore
{
public:
explicit Underscore(std::size_t index);

bool operator==(const Underscore& other) const;
bool operator!=(const Underscore& other) const;

std::size_t index;
};

class LeftBrace
{
public:
Expand Down Expand Up @@ -136,7 +147,7 @@ namespace application
bool value;
};

using Token = infra::Variant<End, Error, Comma, Dot, LeftBrace, RightBrace, LeftBracket, RightBracket, String, Integer, Boolean>;
using Token = infra::Variant<End, Error, Comma, Dot, Underscore, LeftBrace, RightBrace, LeftBracket, RightBracket, String, Integer, Boolean>;
}

class ConsoleTokenizer
Expand Down Expand Up @@ -186,9 +197,12 @@ namespace application
{};

private:
struct Empty
{};

struct MessageTokens
{
using MessageTokenValue = infra::Variant<std::string, int64_t, bool, MessageTokens, std::vector<MessageTokens>>;
using MessageTokenValue = infra::Variant<Empty, std::string, int64_t, bool, MessageTokens, std::vector<MessageTokens>>;

std::vector<std::pair<MessageTokenValue, std::size_t>> tokens;
};
Expand Down

0 comments on commit b33ed06

Please sign in to comment.