Update always_print_fields_with_no_presence to ensure compatibility with protobuf v26.x (#408)

This commit is contained in:
liyingxin 2024-09-06 17:16:15 +08:00 committed by GitHub
parent 060ce7988e
commit bb882f9882
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 49 additions and 25 deletions

View File

@ -183,7 +183,7 @@ public:
{
resp->set_message("Hi back");
resp->set_error(0); // 0是error类型int32在proto3中的默认值
ctx->set_json_always_print_primitive_fields(true); // 带上所有原始域
ctx->set_json_always_print_fields_with_no_presence(true); // 带上所有原始域
ctx->set_json_add_whitespace(true); // 增加json格式的空格
}
};

View File

@ -185,7 +185,7 @@ public:
{
resp->set_message("Hi back");
resp->set_error(0); // the type of error is int32 and 0 is the default value of int32
ctx->set_json_always_print_primitive_fields(true); // with all primitive fields
ctx->set_json_always_print_fields_with_no_presence(true); // all fields with no precense
ctx->set_json_add_whitespace(true); // add spaces, line breaks and indentation
}
};

View File

@ -360,9 +360,9 @@ For Server only. For JsonPrintOptions, whether to always print enums as ints.
For Server only. For JsonPrintOptions, whether to preserve proto field names.
#### `void set_json_always_print_primitive_fields(bool flag);`
#### `void set_json_always_print_fields_with_no_presence(bool flag);`
For Server only. For JsonPrintOptions, whether to always print primitive fields.
For Server only. For JsonPrintOptions, whether to always print fields with no presence.
## RPC Options

View File

@ -297,22 +297,22 @@ Server专用。如果通讯使用HTTP协议可以在回复中设置HTTP heade
#### ``bool add_http_header(const std::string& name, const std::string& value);``
Server专用。如果通讯使用HTTP协议可以在回复中添加HTTP header如果有重复name会保留多个value。
#### ``void log(const RPCLogVector& fields);``
#### ``void log(const RPCLogVector& fields);``
Server专用。透传数据相关请参考OpenTelemetry数据协议中的log语义。
#### ``void baggage(const std::string& key, const std::string& value);``
#### ``void baggage(const std::string& key, const std::string& value);``
Server专用。透传数据相关参考OpenTelemetry数据协议中的baggage语义。
#### ``void set_json_add_whitespace(bool on);``
#### ``void set_json_add_whitespace(bool on);``
Server专用。JsonPrintOptions相关可设置增加json空格等。
#### ``void set_json_always_print_enums_as_ints(bool flag);``
#### ``void set_json_always_print_enums_as_ints(bool flag);``
Server专用。JsonPrintOptions相关可设置用int打印enum名。
#### ``void set_json_preserve_proto_field_names(bool flag);``
#### ``void set_json_preserve_proto_field_names(bool flag);``
Server专用。JsonPrintOptions相关可设置保留原始字段名字。
#### ``void set_json_always_print_primitive_fields(bool flag);``
#### ``void set_json_always_print_fields_with_no_presence(bool flag);``
Server专用。JsonPrintOptions相关可设置带上所有默认的proto数据中的域。
## RPC Options

View File

@ -106,8 +106,8 @@ public:
virtual bool get_json_enums_as_ints() const;
virtual void set_json_preserve_names(bool on);
virtual bool get_json_preserve_names() const;
virtual void set_json_print_primitive(bool on);
virtual bool get_json_print_primitive() const;
virtual void set_json_fields_no_presence(bool on);
virtual bool get_json_fields_no_presence() const;
public:
//pb
@ -182,17 +182,17 @@ inline bool RPCMessage::get_json_preserve_names() const
return this->flags & SRPC_JSON_OPTION_PRESERVE_NAMES;
}
inline void RPCMessage::set_json_print_primitive(bool on)
inline void RPCMessage::set_json_fields_no_presence(bool on)
{
if (on)
this->flags |= SRPC_JSON_OPTION_PRINT_PRIMITIVE;
this->flags |= SRPC_JSON_OPTION_FIELDS_NO_PRECENCE;
else
this->flags &= ~SRPC_JSON_OPTION_PRINT_PRIMITIVE;
this->flags &= ~SRPC_JSON_OPTION_FIELDS_NO_PRECENCE;
}
inline bool RPCMessage::get_json_print_primitive() const
inline bool RPCMessage::get_json_fields_no_presence() const
{
return this->flags & SRPC_JSON_OPTION_PRINT_PRIMITIVE;
return this->flags & SRPC_JSON_OPTION_FIELDS_NO_PRECENCE;
}
} // namespace srpc

View File

@ -17,6 +17,7 @@
#include <errno.h>
#include <vector>
#include <string>
#include <google/protobuf/stubs/common.h>
#include <google/protobuf/util/json_util.h>
#include <google/protobuf/util/type_resolver_util.h>
#include <google/protobuf/io/zero_copy_stream_impl_lite.h>
@ -467,7 +468,11 @@ int SRPCMessage::serialize(const ProtobufIDLMessage *pb_msg)
options.add_whitespace = this->get_json_add_whitespace();
options.always_print_enums_as_ints = this->get_json_enums_as_ints();
options.preserve_proto_field_names = this->get_json_preserve_names();
options.always_print_primitive_fields = this->get_json_print_primitive();
#if GOOGLE_PROTOBUF_VERSION >= 5026000
options.always_print_fields_with_no_presence = this->get_json_fields_no_presence();
#else
options.always_print_primitive_fields = this->get_json_fields_no_presence();
#endif
ret = BinaryToJsonStream(resolver, GetTypeUrl(pb_msg), &input_stream,
&output_stream, options).ok() ? 0 : -1;

View File

@ -17,6 +17,7 @@
#include <errno.h>
#include <vector>
#include <string>
#include <google/protobuf/stubs/common.h>
#include <google/protobuf/util/json_util.h>
#include <google/protobuf/util/type_resolver_util.h>
#include <google/protobuf/io/zero_copy_stream_impl_lite.h>
@ -912,7 +913,11 @@ int TRPCMessage::serialize(const ProtobufIDLMessage *pb_msg)
options.add_whitespace = this->get_json_add_whitespace();
options.always_print_enums_as_ints = this->get_json_enums_as_ints();
options.preserve_proto_field_names = this->get_json_preserve_names();
options.always_print_primitive_fields = this->get_json_print_primitive();
#if GOOGLE_PROTOBUF_VERSION >= 5026000
options.always_print_fields_with_no_presence = this->get_json_fields_no_presence();
#else
options.always_print_primitive_fields = this->get_json_fields_no_presence();
#endif
ret = BinaryToJsonStream(resolver, GetTypeUrl(pb_msg), &input_stream,
&output_stream, options).ok() ? 0 : -1;

View File

@ -79,7 +79,7 @@ static inline uint64_t ntohll(uint64_t x)
#define SRPC_JSON_OPTION_ADD_WHITESPACE (1<<3)
#define SRPC_JSON_OPTION_ENUM_AS_INITS (1<<4)
#define SRPC_JSON_OPTION_PRESERVE_NAMES (1<<5)
#define SRPC_JSON_OPTION_PRINT_PRIMITIVE (1<<6)
#define SRPC_JSON_OPTION_FIELDS_NO_PRECENCE (1<<6)
using ProtobufIDLMessage = google::protobuf::Message;
using RPCLogVector = std::vector<std::pair<std::string, std::string>>;

View File

@ -96,12 +96,18 @@ public:
// Whether to preserve proto field names.
virtual void set_json_preserve_proto_field_names(bool flag) = 0;
// Whether to always print primitive fields.
// Whether to always print primitive fields / with no presence.
// By default proto3 primitive fields with default values will be omitted
// in JSON output. For example, an int32 field set to 0 will be omitted.
// Set this flag to true will override the default behavior and print
// primitive fields regardless of their values.
virtual void set_json_always_print_primitive_fields(bool flag) = 0;
virtual void set_json_always_print_fields_with_no_presence(bool flag) = 0;
// deprecated : Please use set_json_always_print_fields_with_no_presence()
void set_json_always_print_primitive_fields(bool flag)
{
this->set_json_always_print_fields_with_no_presence(flag);
}
public:
virtual ~RPCContext() { }

View File

@ -261,10 +261,10 @@ public:
task_->get_resp()->set_json_preserve_names(on);
}
void set_json_always_print_primitive_fields(bool on) override
void set_json_always_print_fields_with_no_presence(bool on) override
{
if (this->is_server_task())
task_->get_resp()->set_json_print_primitive(on);
task_->get_resp()->set_json_fields_no_presence(on);
}
//void noreply() override;

View File

@ -130,7 +130,9 @@ public:
void set_json_add_whitespace(bool on);
void set_json_always_print_enums_as_ints(bool on);
void set_json_preserve_proto_field_names(bool on);
// deprecated : Please use set_json_always_print_fields_with_no_presence()
void set_json_always_print_primitive_fields(bool on);
void set_json_always_print_fields_with_no_presence(bool on);
protected:
using user_done_t = std::function<int (int, RPCWorker&)>;
@ -694,7 +696,13 @@ inline void RPCClientTask<RPCREQ, RPCRESP>::set_json_preserve_proto_field_names(
template<class RPCREQ, class RPCRESP>
inline void RPCClientTask<RPCREQ, RPCRESP>::set_json_always_print_primitive_fields(bool on)
{
this->req.set_json_print_primitive(on);
this->req.set_json_fields_no_presence(on);
}
template<class RPCREQ, class RPCRESP>
inline void RPCClientTask<RPCREQ, RPCRESP>::set_json_always_print_fields_with_no_presence(bool on)
{
this->req.set_json_fields_no_presence(on);
}
template<class RPCREQ, class RPCRESP>