Add method to validate and set service config json

reviewable/pr19021/r1
Yash Tibrewal 6 years ago
parent 987cd3d5ce
commit 4e711fab64
  1. 23
      include/grpcpp/support/channel_arguments_impl.h
  2. 15
      src/cpp/common/channel_arguments.cc
  3. 8
      test/cpp/end2end/service_config_end2end_test.cc

@ -40,8 +40,24 @@ class SecureChannelCredentials;
/// Options for channel creation. The user can use generic setters to pass
/// key value pairs down to C channel creation code. For gRPC related options,
/// concrete setters are provided.
class ChannelArguments {
class ChannelArguments : private ::grpc::GrpcLibraryCodegen {
public:
/// NOTE: class experimental_type is not part of the public API of this class.
/// TODO(yashykt): Integrate into public API when this is no longer
/// experimental.
class experimental_type {
public:
explicit experimental_type(ChannelArguments* args) : args_(args) {}
/// Validates \a service_config_json. If valid, set the service config and
/// returns an empty string. If invalid, returns the validation error.
grpc::string ValidateAndSetServiceConfigJSON(
const grpc::string& service_config_json);
private:
ChannelArguments* args_;
};
ChannelArguments();
~ChannelArguments();
@ -125,6 +141,11 @@ class ChannelArguments {
return out;
}
/// NOTE: The function experimental() is not stable public API. It is a view
/// to the experimental components of this class. It may be changed or removed
/// at any time.
experimental_type experimental() { return experimental_type(this); }
private:
friend class grpc_impl::SecureChannelCredentials;
friend class grpc::testing::ChannelArgumentsTest;

@ -23,6 +23,7 @@
#include <grpc/support/log.h>
#include <grpcpp/grpcpp.h>
#include <grpcpp/resource_quota.h>
#include "src/core/ext/filters/client_channel/service_config.h"
#include "src/core/lib/channel/channel_args.h"
#include "src/core/lib/iomgr/exec_ctx.h"
#include "src/core/lib/iomgr/socket_mutator.h"
@ -35,7 +36,7 @@ ChannelArguments::ChannelArguments() {
}
ChannelArguments::ChannelArguments(const ChannelArguments& other)
: strings_(other.strings_) {
: ::grpc::GrpcLibraryCodegen(), strings_(other.strings_) {
args_.reserve(other.args_.size());
auto list_it_dst = strings_.begin();
auto list_it_src = other.strings_.begin();
@ -215,4 +216,16 @@ void ChannelArguments::SetChannelArgs(grpc_channel_args* channel_args) const {
}
}
grpc::string
ChannelArguments::experimental_type::ValidateAndSetServiceConfigJSON(
const grpc::string& service_config_json) {
grpc_error* error = GRPC_ERROR_NONE;
grpc_core::ServiceConfig::Create(service_config_json.c_str(), &error);
if (error != GRPC_ERROR_NONE) {
return grpc_error_string(error);
}
args_->SetServiceConfigJSON(service_config_json);
return "";
}
} // namespace grpc_impl

@ -231,7 +231,9 @@ class ServiceConfigEnd2endTest : public ::testing::Test {
std::shared_ptr<Channel> BuildChannelWithDefaultServiceConfig() {
ChannelArguments args;
args.SetServiceConfigJSON(ValidDefaultServiceConfig());
EXPECT_THAT(args.experimental().ValidateAndSetServiceConfigJSON(
ValidDefaultServiceConfig()),
::testing::StrEq(""));
args.SetPointer(GRPC_ARG_FAKE_RESOLVER_RESPONSE_GENERATOR,
response_generator_.get());
return ::grpc::CreateCustomChannel("fake:///", creds_, args);
@ -239,6 +241,10 @@ class ServiceConfigEnd2endTest : public ::testing::Test {
std::shared_ptr<Channel> BuildChannelWithInvalidDefaultServiceConfig() {
ChannelArguments args;
EXPECT_THAT(
args.experimental().ValidateAndSetServiceConfigJSON(
InvalidDefaultServiceConfig()),
::testing::HasSubstr("failed to parse JSON for service config"));
args.SetServiceConfigJSON(InvalidDefaultServiceConfig());
args.SetPointer(GRPC_ARG_FAKE_RESOLVER_RESPONSE_GENERATOR,
response_generator_.get());

Loading…
Cancel
Save