Skip to content

Commit

Permalink
make message_type_url free function
Browse files Browse the repository at this point in the history
  • Loading branch information
huangminghuang committed Dec 16, 2023
1 parent 98510d9 commit 08872db
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 122 deletions.
7 changes: 6 additions & 1 deletion include/hpp_proto/field_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ constexpr ToType bit_cast(FromType const &from) noexcept {
} // namespace std
#endif


namespace hpp::proto {

// workaround for clang not supporting floating-point types in non-type template
Expand Down Expand Up @@ -523,4 +522,10 @@ constexpr bool is_default_value(const T &val) {
return Default == val;
}
}

inline const char *message_name(auto &&v)
requires requires { message_type_url(v); }
{
return message_type_url(v).c_str() + std::size("type.googleapis.com");
}
} // namespace hpp::proto
4 changes: 2 additions & 2 deletions include/hpp_proto/json_serializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -260,10 +260,10 @@ struct to_json<T> {
if (bytes_written < 0) {
ctx.error = glz::error_code::syntax_error;
if constexpr (requires {
value.protobuf_message_name_;
hpp::proto::message_name(value);
ctx.error_message_name;
}) {
ctx.error_message_name = value.protobuf_message_name_;
ctx.error_message_name = hpp::proto::message_name(value);
}
return;
}
Expand Down
9 changes: 9 additions & 0 deletions include/hpp_proto/memory_resource_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,15 @@ constexpr auto make_growable(concepts::has_memory_resource auto &&context, std::
return growable_span{base, context.memory_resource};
}

template <typename T>
constexpr auto make_growable(concepts::memory_resource auto &mr, std::span<T> &base) {
return growable_span{base, mr};
}

constexpr auto make_growable(concepts::memory_resource auto &mr, std::string_view &base) {
return growable_span{base, mr};
}

template <typename T>
constexpr T &make_growable(auto && /* unused */, T &base) {
return base;
Expand Down
36 changes: 35 additions & 1 deletion include/hpp_proto/pb_serializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
#include <hpp_proto/expected.h>
#include <hpp_proto/memory_resource_utils.h>


namespace hpp {
namespace proto {

Expand Down Expand Up @@ -108,6 +107,9 @@ concept variant = requires(Type variant) {
std::variant_size_v<std::remove_cvref_t<Type>>;
};

template <typename Type>
concept string = std::same_as<Type, std::string> || std::same_as<Type, std::string_view>;

template <typename Type>
concept has_local_meta = concepts::tuple<typename Type::pb_meta>;

Expand Down Expand Up @@ -1922,6 +1924,38 @@ template <typename T, typename Buffer, concepts::memory_resource MemoryResource>
return std::make_error_code(pb_serializer::deserialize(msg, std::forward<Buffer>(buffer), mr));
}

namespace concepts {
template <typename T>
concept is_any = requires(T &obj) {
{ obj.type_url } -> concepts::string;
{ obj.value } -> concepts::contiguous_byte_range;
};
}

[[nodiscard]] std::error_code pack_any(concepts::is_any auto &any, auto &&msg) {
any.type_url = message_type_url(msg);
return write_proto(msg, any.value);
}

[[nodiscard]] std::error_code unpack_any(concepts::is_any auto &&any, auto &&msg) {
if (std::string_view{any.type_url}.ends_with(message_name(msg))) {
return read_proto(msg, any.value);
}
return std::make_error_code(std::errc::invalid_argument);
}

[[nodiscard]] std::error_code pack_any(concepts::is_any auto &any, auto &&msg, concepts::memory_resource auto &mr) {
any.type_url = message_type_url(msg);
return write_proto(msg, detail::make_growable(mr, any.value));
}

[[nodiscard]] std::error_code unpack_any(concepts::is_any auto &&any, auto &&msg, concepts::memory_resource auto &mr) {
if (std::string_view{any.type_url}.ends_with(message_name(msg))) {
return read_proto(msg, any.value, mr);
}
return std::make_error_code(std::errc::invalid_argument);
}

} // namespace proto
} // namespace hpp

Expand Down
2 changes: 1 addition & 1 deletion include/hpp_proto/timestamp_codec.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ struct timestamp_codec {
constexpr static std::size_t max_encode_size(auto &&) noexcept { return std::size("yyyy-mm-ddThh:mm:ss.000000000Z"); }

static int64_t encode(auto &&value, auto &&b) noexcept {
if (value.nanos > 999999999) [[unlikely]]
if (value.nanos > 999999999 || value.nanos < 0) [[unlikely]]
return -1;
using namespace std::chrono;
const auto seconds_in_a_day = seconds(24h).count();
Expand Down
49 changes: 31 additions & 18 deletions protoc-plugin/hpp_gen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,7 @@ struct msg_code_generator : code_generator {
}

for (auto *m : order_messages(descriptor.messages)) {
process(*m, descriptor.proto.package);
process(*m, "", descriptor.proto.package);
}

std::copy(out_of_class_data.begin(), out_of_class_data.end(), target);
Expand Down Expand Up @@ -842,7 +842,7 @@ struct msg_code_generator : code_generator {
}

// NOLINTBEGIN(misc-no-recursion,readability-function-cognitive-complexity)
void process(message_descriptor_t &descriptor, const std::string &pb_scope) {
void process(message_descriptor_t &descriptor, const std::string &cpp_scope, const std::string &pb_scope) {
if (descriptor.is_map_entry) {
return;
}
Expand All @@ -851,6 +851,11 @@ struct msg_code_generator : code_generator {
descriptor.pb_name = pb_scope + "." + descriptor.pb_name;
}

std::string qualified_cpp_name = descriptor.cpp_name;
if (!cpp_scope.empty()) {
qualified_cpp_name = cpp_scope + "::" + qualified_cpp_name;
}

for (const auto &fwd : descriptor.forward_declarations) {
fmt::format_to(target, "{}struct {};\n", indent(), fwd);
}
Expand All @@ -864,14 +869,12 @@ struct msg_code_generator : code_generator {
fmt::format_to(target, "{}constexpr static bool reflect = false;\n", indent());
}

fmt::format_to(target, "{}static constexpr auto protobuf_message_name_ = \"{}\";\n", indent(), descriptor.pb_name);

for (auto &e : descriptor.enums) {
process(*e);
}

for (auto *m : order_messages(descriptor.messages)) {
process(*m, descriptor.pb_name);
process(*m, qualified_cpp_name, descriptor.pb_name);
}

for (auto *f : descriptor.fields) {
Expand Down Expand Up @@ -995,6 +998,9 @@ struct msg_code_generator : code_generator {

indent_num -= 2;
fmt::format_to(target, "{}}};\n\n", indent());
fmt::format_to(out_of_class_target,
"constexpr auto message_type_url(const {0}&) {{ return \"type.googleapis.com/{1}\"_cts; }}\n",
qualified_cpp_name, descriptor.pb_name);
}
// NOLINTEND(misc-no-recursion,readability-function-cognitive-complexity)
};
Expand Down Expand Up @@ -1355,21 +1361,28 @@ struct glaze_meta_generator : code_generator {
" static constexpr auto value = object(\n",
qualified_name);

for (auto *f : descriptor.fields) {
if (!f->proto.oneof_index.has_value()) {
process(*f);
} else {
auto index = *f->proto.oneof_index;
auto &oneof = *(descriptor.oneofs[index]);
if (oneof.fields[0]->proto.number == f->proto.number) {
process(oneof);
if (descriptor.pb_name == "google.protobuf.Any") {
fmt::format_to(target,
"\"@type\", hpp::proto::as_optional_ref<&T::type_url>,\n"
"\"@value\", hpp::proto::as_optional_ref<&T::value>",
qualified_name);

} else {
for (auto *f : descriptor.fields) {
if (!f->proto.oneof_index.has_value()) {
process(*f);
} else {
auto index = *f->proto.oneof_index;
auto &oneof = *(descriptor.oneofs[index]);
if (oneof.fields[0]->proto.number == f->proto.number) {
process(oneof);
}
}
}
}

if (!descriptor.fields.empty()) {
auto &content = file.content;
content.resize(content.size() - 2);
if (!descriptor.fields.empty()) {
auto &content = file.content;
content.resize(content.size() - 2);
}
}

fmt::format_to(target, ");\n}};\n\n");
Expand Down
Loading

0 comments on commit 08872db

Please sign in to comment.