Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Sep 6, 2024
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
2 changes: 1 addition & 1 deletion docs/docs-07-srpc-http.md
Original file line number Diff line number Diff line change
Expand Up @@ -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格式的空格
}
};
Expand Down
2 changes: 1 addition & 1 deletion docs/en/docs-07-srpc-http.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
};
Expand Down
4 changes: 2 additions & 2 deletions docs/en/rpc.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
12 changes: 6 additions & 6 deletions docs/rpc.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 7 additions & 7 deletions src/message/rpc_message.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,8 @@ class RPCMessage
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
Expand Down Expand Up @@ -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
Expand Down
7 changes: 6 additions & 1 deletion src/message/rpc_message_srpc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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;
Expand Down
7 changes: 6 additions & 1 deletion src/message/rpc_message_trpc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/rpc_basic.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>>;
Expand Down
10 changes: 8 additions & 2 deletions src/rpc_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,18 @@ class RPCContext
// 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() { }
Expand Down
4 changes: 2 additions & 2 deletions src/rpc_context.inl
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 9 additions & 1 deletion src/rpc_task.inl
Original file line number Diff line number Diff line change
Expand Up @@ -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&)>;
Expand Down Expand Up @@ -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>
Expand Down