Merge pull request #19939 from blowmage/ruby-struct-status-rb

Define Struct::Status in Ruby
pull/19827/head
apolcyn 5 years ago committed by GitHub
commit 79fc2e5c56
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 20
      src/ruby/end2end/errors_load_before_grpc_lib.rb
  2. 3
      src/ruby/ext/grpc/rb_grpc.c
  3. 1
      src/ruby/lib/grpc.rb
  4. 9
      src/ruby/lib/grpc/errors.rb
  5. 13
      src/ruby/lib/grpc/google_rpc_status_utils.rb
  6. 15
      src/ruby/lib/grpc/structs.rb
  7. 1
      src/ruby/spec/errors_spec.rb

@ -18,12 +18,32 @@ this_dir = File.expand_path(File.dirname(__FILE__))
grpc_lib_dir = File.join(File.dirname(this_dir), 'lib') grpc_lib_dir = File.join(File.dirname(this_dir), 'lib')
$LOAD_PATH.unshift(grpc_lib_dir) unless $LOAD_PATH.include?(grpc_lib_dir) $LOAD_PATH.unshift(grpc_lib_dir) unless $LOAD_PATH.include?(grpc_lib_dir)
def check_to_status(error)
my_status = error.to_status
fail('GRPC BadStatus#to_status not expected to return nil') if my_status.nil?
fail('GRPC BadStatus#to_status code expected to be 2') unless my_status.code == 2
fail('GRPC BadStatus#to_status details expected to be unknown') unless my_status.details == 'unknown'
fail('GRPC BadStatus#to_status metadata expected to be empty hash') unless my_status.metadata == {}
fail('GRPC library loaded after BadStatus#to_status') if GRPC::Core.const_defined?(:Channel)
end
def check_to_rpc_status(error)
my_rpc_status = error.to_rpc_status
fail('GRPC BadStatus#to_rpc_status expected to return nil') unless my_rpc_status.nil?
fail('GRPC library loaded after BadStatus#to_rpc_status') if GRPC::Core.const_defined?(:Channel)
end
def main def main
fail('GRPC constant loaded before expected') if Object.const_defined?(:GRPC) fail('GRPC constant loaded before expected') if Object.const_defined?(:GRPC)
require 'grpc/errors' require 'grpc/errors'
fail('GRPC constant not loaded when expected') unless Object.const_defined?(:GRPC) fail('GRPC constant not loaded when expected') unless Object.const_defined?(:GRPC)
fail('GRPC BadStatus not loaded after required') unless GRPC.const_defined?(:BadStatus) fail('GRPC BadStatus not loaded after required') unless GRPC.const_defined?(:BadStatus)
fail('GRPC Core not loaded after required') unless GRPC.const_defined?(:Core)
fail('GRPC StatusCodes not loaded after required') unless GRPC::Core.const_defined?(:StatusCodes)
fail('GRPC library loaded before required') if GRPC::Core.const_defined?(:Channel) fail('GRPC library loaded before required') if GRPC::Core.const_defined?(:Channel)
error = GRPC::BadStatus.new 2, 'unknown'
check_to_status(error)
check_to_rpc_status(error)
require 'grpc' require 'grpc'
fail('GRPC library not loaded after required') unless GRPC::Core.const_defined?(:Channel) fail('GRPC library not loaded after required') unless GRPC::Core.const_defined?(:Channel)
end end

@ -312,8 +312,7 @@ void Init_grpc_c() {
grpc_rb_mGrpcCore = rb_define_module_under(grpc_rb_mGRPC, "Core"); grpc_rb_mGrpcCore = rb_define_module_under(grpc_rb_mGRPC, "Core");
grpc_rb_sNewServerRpc = rb_struct_define( grpc_rb_sNewServerRpc = rb_struct_define(
"NewServerRpc", "method", "host", "deadline", "metadata", "call", NULL); "NewServerRpc", "method", "host", "deadline", "metadata", "call", NULL);
grpc_rb_sStatus = grpc_rb_sStatus = rb_const_get(rb_cStruct, rb_intern("Status"));
rb_struct_define("Status", "code", "details", "metadata", NULL);
sym_code = ID2SYM(rb_intern("code")); sym_code = ID2SYM(rb_intern("code"));
sym_details = ID2SYM(rb_intern("details")); sym_details = ID2SYM(rb_intern("details"));
sym_metadata = ID2SYM(rb_intern("metadata")); sym_metadata = ID2SYM(rb_intern("metadata"));

@ -15,6 +15,7 @@
ssl_roots_path = File.expand_path('../../../../etc/roots.pem', __FILE__) ssl_roots_path = File.expand_path('../../../../etc/roots.pem', __FILE__)
require_relative 'grpc/errors' require_relative 'grpc/errors'
require_relative 'grpc/structs'
require_relative 'grpc/grpc' require_relative 'grpc/grpc'
require_relative 'grpc/logconfig' require_relative 'grpc/logconfig'
require_relative 'grpc/notifier' require_relative 'grpc/notifier'

@ -12,7 +12,9 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
require_relative './structs'
require_relative './core/status_codes' require_relative './core/status_codes'
require_relative './google_rpc_status_utils'
# GRPC contains the General RPC module. # GRPC contains the General RPC module.
module GRPC module GRPC
@ -57,12 +59,7 @@ module GRPC
# #
# @return [Google::Rpc::Status, nil] # @return [Google::Rpc::Status, nil]
def to_rpc_status def to_rpc_status
# Lazily require google_rpc_status_utils to scope GoogleRpcStatusUtils.extract_google_rpc_status(to_status)
# loading protobuf_c.so to the users of this method.
require_relative './google_rpc_status_utils'
status = to_status
return if status.nil?
GoogleRpcStatusUtils.extract_google_rpc_status(status)
rescue Google::Protobuf::ParseError => parse_error rescue Google::Protobuf::ParseError => parse_error
GRPC.logger.warn('parse error: to_rpc_status failed') GRPC.logger.warn('parse error: to_rpc_status failed')
GRPC.logger.warn(parse_error) GRPC.logger.warn(parse_error)

@ -12,8 +12,7 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
require_relative './grpc' require_relative './structs'
require 'google/rpc/status_pb'
# GRPC contains the General RPC module. # GRPC contains the General RPC module.
module GRPC module GRPC
@ -28,8 +27,14 @@ module GRPC
def self.extract_google_rpc_status(status) def self.extract_google_rpc_status(status)
fail ArgumentError, 'bad type' unless status.is_a? Struct::Status fail ArgumentError, 'bad type' unless status.is_a? Struct::Status
grpc_status_details_bin_trailer = 'grpc-status-details-bin' grpc_status_details_bin_trailer = 'grpc-status-details-bin'
return nil if status.metadata[grpc_status_details_bin_trailer].nil? binstatus = status.metadata[grpc_status_details_bin_trailer]
Google::Rpc::Status.decode(status.metadata[grpc_status_details_bin_trailer]) return nil if binstatus.nil?
# Lazily load grpc_c and protobuf_c.so for users of this method.
require_relative './grpc'
require 'google/rpc/status_pb'
Google::Rpc::Status.decode(binstatus)
end end
end end
end end

@ -0,0 +1,15 @@
# Copyright 2015 gRPC authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
Struct.new('Status', :code, :details, :metadata)

@ -14,6 +14,7 @@
require 'spec_helper' require 'spec_helper'
require 'google/protobuf/well_known_types' require 'google/protobuf/well_known_types'
require 'google/rpc/status_pb'
require_relative '../pb/src/proto/grpc/testing/messages_pb' require_relative '../pb/src/proto/grpc/testing/messages_pb'
describe GRPC::BadStatus do describe GRPC::BadStatus do

Loading…
Cancel
Save