replaced constant compress level names with ids

pull/7216/head
Alexander Polcyn 9 years ago
parent b72cc3dd87
commit 6c4709e275
  1. 70
      src/ruby/ext/grpc/rb_compression_options.c
  2. 22
      src/ruby/spec/compression_options_spec.rb

@ -47,6 +47,12 @@
static VALUE grpc_rb_cCompressionOptions = Qnil;
/* Ruby Ids for the names of valid compression levels. */
static VALUE id_compress_level_none = Qnil;
static VALUE id_compress_level_low = Qnil;
static VALUE id_compress_level_medium = Qnil;
static VALUE id_compress_level_high = Qnil;
/* grpc_rb_compression_options wraps a grpc_compression_options.
* It can be used to get the channel argument key-values for specific
* compression settings. */
@ -121,47 +127,27 @@ VALUE grpc_rb_compression_options_disable_compression_algorithm_internal(
return Qnil;
}
/* Provides a bitset as a ruby number that is suitable to pass to
* the GRPC core as a channel argument to enable compression algorithms. */
/* Gets the compression internal enum value of a compression level given its
* name. */
grpc_compression_level grpc_rb_compression_options_level_name_to_value_internal(
VALUE level_name) {
VALUE none_symbol = Qnil;
VALUE low_symbol = Qnil;
VALUE medium_symbol = Qnil;
VALUE high_symbol = Qnil;
Check_Type(level_name, T_SYMBOL);
/* Ruby symbols that correspond to names of valid compression levels */
none_symbol =
rb_const_get(grpc_rb_cCompressionOptions, rb_intern("COMPRESS_NONE_SYM"));
low_symbol =
rb_const_get(grpc_rb_cCompressionOptions, rb_intern("COMPRESS_LOW_SYM"));
medium_symbol = rb_const_get(grpc_rb_cCompressionOptions,
rb_intern("COMPRESS_MEDIUM_SYM"));
high_symbol =
rb_const_get(grpc_rb_cCompressionOptions, rb_intern("COMPRESS_HIGH_SYM"));
/* Check the compression level of the name passed in, and see which macro
* from the GRPC core header files match. */
if (RTEST(rb_funcall(level_name, rb_intern("=="), 1, none_symbol)) != 0) {
if (id_compress_level_none == SYM2ID(level_name)) {
return GRPC_COMPRESS_LEVEL_NONE;
} else if (RTEST(rb_funcall(level_name, rb_intern("=="), 1, low_symbol)) !=
0) {
} else if (id_compress_level_low == SYM2ID(level_name)) {
return GRPC_COMPRESS_LEVEL_LOW;
} else if (RTEST(rb_funcall(level_name, rb_intern("=="), 1, medium_symbol)) !=
0) {
} else if (id_compress_level_medium == SYM2ID(level_name)) {
return GRPC_COMPRESS_LEVEL_MED;
} else if (RTEST(rb_funcall(level_name, rb_intern("=="), 1, high_symbol)) !=
0) {
} else if (id_compress_level_high == SYM2ID(level_name)) {
return GRPC_COMPRESS_LEVEL_HIGH;
} else {
rb_raise(rb_eArgError,
"Unrecognized compression level name."
"Valid compression level names are none, low, medium, and high.");
}
rb_raise(rb_eArgError,
"Unrecognized compression level name."
"Valid compression level names are none, low, medium, and high.");
}
/* Wrapper over grpc_rb_compression_options_level_name_to_value available for
@ -170,6 +156,8 @@ grpc_compression_level grpc_rb_compression_options_level_name_to_value_internal(
VALUE grpc_rb_compression_options_level_name_to_value(VALUE self,
VALUE level_name) {
(void)self;
Check_Type(level_name, T_SYMBOL);
return INT2NUM((int)grpc_rb_compression_options_level_name_to_value_internal(
level_name));
}
@ -304,17 +292,13 @@ VALUE grpc_rb_compression_options_level_value_to_name_internal(
grpc_compression_level compression_value) {
switch (compression_value) {
case GRPC_COMPRESS_LEVEL_NONE:
return rb_const_get(grpc_rb_cCompressionOptions,
rb_intern("COMPRESS_NONE_SYM"));
return ID2SYM(id_compress_level_none);
case GRPC_COMPRESS_LEVEL_LOW:
return rb_const_get(grpc_rb_cCompressionOptions,
rb_intern("COMPRESS_LOW_SYM"));
return ID2SYM(id_compress_level_low);
case GRPC_COMPRESS_LEVEL_MED:
return rb_const_get(grpc_rb_cCompressionOptions,
rb_intern("COMPRESS_MEDIUM_SYM"));
return ID2SYM(id_compress_level_medium);
case GRPC_COMPRESS_LEVEL_HIGH:
return rb_const_get(grpc_rb_cCompressionOptions,
rb_intern("COMPRESS_HIGH_SYM"));
return ID2SYM(id_compress_level_high);
default:
rb_raise(
rb_eArgError,
@ -580,13 +564,9 @@ void Init_grpc_compression_options() {
rb_define_alias(grpc_rb_cCompressionOptions, "to_channel_arg_hash",
"to_hash");
/* Ruby symbols for the names of the different compression levels. */
rb_define_const(grpc_rb_cCompressionOptions, "COMPRESS_NONE_SYM",
ID2SYM(rb_intern("none")));
rb_define_const(grpc_rb_cCompressionOptions, "COMPRESS_LOW_SYM",
ID2SYM(rb_intern("low")));
rb_define_const(grpc_rb_cCompressionOptions, "COMPRESS_MEDIUM_SYM",
ID2SYM(rb_intern("medium")));
rb_define_const(grpc_rb_cCompressionOptions, "COMPRESS_HIGH_SYM",
ID2SYM(rb_intern("high")));
/* Ruby ids for the names of the different compression levels. */
id_compress_level_none = rb_intern("none");
id_compress_level_low = rb_intern("low");
id_compress_level_medium = rb_intern("medium");
id_compress_level_high = rb_intern("high");
}

@ -59,13 +59,6 @@ describe GRPC::Core::CompressionOptions do
high: 3
}
it 'compression level name constants should match expections' do
expect(GRPC::Core::CompressionOptions::COMPRESS_NONE_SYM).to eq(:none)
expect(GRPC::Core::CompressionOptions::COMPRESS_LOW_SYM).to eq(:low)
expect(GRPC::Core::CompressionOptions::COMPRESS_MEDIUM_SYM).to eq(:medium)
expect(GRPC::Core::CompressionOptions::COMPRESS_HIGH_SYM).to eq(:high)
end
it 'implements to_s' do
expect { GRPC::Core::CompressionOptions.new.to_s }.to_not raise_error
end
@ -143,10 +136,10 @@ describe GRPC::Core::CompressionOptions do
end
describe '#level_name_to_value' do
it 'should give the correct internal values from compression level names' do
cls = GRPC::Core::CompressionOptions
COMPRESS_LEVELS.each_pair do |name, internal_value|
expect(cls.level_name_to_value(name)).to eq(internal_value)
COMPRESS_LEVELS.each_pair do |name, internal_value|
it "should return value #{internal_value} for level #{name}" do
actual_value = GRPC::Core::CompressionOptions.level_name_to_value(name)
expect(actual_value).to eq(internal_value)
end
end
@ -161,6 +154,13 @@ describe GRPC::Core::CompressionOptions do
end
describe '#level_value_to_name' do
COMPRESS_LEVELS.each_pair do |name, internal_value|
it "should return level name #{name} for value #{internal_value}" do
actual_name = GRPC::Core::CompressionOptions.level_value_to_name(
internal_value)
expect(actual_name).to eq(name)
end
end
it 'should give the correct internal values from compression level names' do
cls = GRPC::Core::CompressionOptions
COMPRESS_LEVELS.each_pair do |name, internal_value|

Loading…
Cancel
Save