protobuf 2.8.13 → 3.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (116) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGES.md +84 -5
  3. data/CONTRIBUTING.md +3 -3
  4. data/Rakefile +46 -7
  5. data/lib/protobuf/cli.rb +2 -20
  6. data/lib/protobuf/decoder.rb +74 -0
  7. data/lib/protobuf/deprecator.rb +42 -0
  8. data/lib/protobuf/descriptors/google/protobuf/compiler/plugin.pb.rb +17 -16
  9. data/lib/protobuf/descriptors/google/protobuf/descriptor.pb.rb +86 -85
  10. data/lib/protobuf/encoder.rb +62 -0
  11. data/lib/protobuf/enum.rb +298 -37
  12. data/lib/protobuf/field/base_field.rb +41 -27
  13. data/lib/protobuf/field/bool_field.rb +22 -4
  14. data/lib/protobuf/field/bytes_field.rb +36 -15
  15. data/lib/protobuf/field/double_field.rb +10 -3
  16. data/lib/protobuf/field/enum_field.rb +21 -18
  17. data/lib/protobuf/field/field_array.rb +26 -16
  18. data/lib/protobuf/field/fixed32_field.rb +10 -4
  19. data/lib/protobuf/field/fixed64_field.rb +10 -3
  20. data/lib/protobuf/field/float_field.rb +18 -5
  21. data/lib/protobuf/field/int32_field.rb +14 -4
  22. data/lib/protobuf/field/int64_field.rb +14 -4
  23. data/lib/protobuf/field/integer_field.rb +9 -4
  24. data/lib/protobuf/field/message_field.rb +16 -7
  25. data/lib/protobuf/field/sfixed32_field.rb +10 -3
  26. data/lib/protobuf/field/sfixed64_field.rb +12 -7
  27. data/lib/protobuf/field/signed_integer_field.rb +7 -0
  28. data/lib/protobuf/field/sint32_field.rb +14 -4
  29. data/lib/protobuf/field/sint64_field.rb +14 -4
  30. data/lib/protobuf/field/string_field.rb +11 -1
  31. data/lib/protobuf/field/uint32_field.rb +14 -4
  32. data/lib/protobuf/field/uint64_field.rb +14 -4
  33. data/lib/protobuf/field/varint_field.rb +11 -9
  34. data/lib/protobuf/field.rb +42 -25
  35. data/lib/protobuf/generators/enum_generator.rb +12 -1
  36. data/lib/protobuf/generators/field_generator.rb +1 -1
  37. data/lib/protobuf/lifecycle.rb +3 -4
  38. data/lib/protobuf/message/fields.rb +122 -0
  39. data/lib/protobuf/message/serialization.rb +84 -0
  40. data/lib/protobuf/message.rb +21 -221
  41. data/lib/protobuf/optionable.rb +23 -0
  42. data/lib/protobuf/rpc/client.rb +2 -4
  43. data/lib/protobuf/rpc/connector.rb +0 -2
  44. data/lib/protobuf/rpc/connectors/common.rb +2 -2
  45. data/lib/protobuf/rpc/dynamic_discovery.pb.rb +14 -16
  46. data/lib/protobuf/rpc/env.rb +58 -0
  47. data/lib/protobuf/rpc/error.rb +8 -5
  48. data/lib/protobuf/rpc/middleware/exception_handler.rb +36 -0
  49. data/lib/protobuf/rpc/middleware/logger.rb +91 -0
  50. data/lib/protobuf/rpc/middleware/request_decoder.rb +83 -0
  51. data/lib/protobuf/rpc/middleware/response_encoder.rb +88 -0
  52. data/lib/protobuf/rpc/middleware/runner.rb +18 -0
  53. data/lib/protobuf/rpc/middleware.rb +25 -0
  54. data/lib/protobuf/rpc/rpc.pb.rb +15 -16
  55. data/lib/protobuf/rpc/server.rb +14 -64
  56. data/lib/protobuf/rpc/servers/socket/server.rb +0 -2
  57. data/lib/protobuf/rpc/servers/socket/worker.rb +11 -15
  58. data/lib/protobuf/rpc/servers/zmq/util.rb +4 -1
  59. data/lib/protobuf/rpc/servers/zmq/worker.rb +5 -13
  60. data/lib/protobuf/rpc/servers/zmq_runner.rb +1 -1
  61. data/lib/protobuf/rpc/service.rb +38 -72
  62. data/lib/protobuf/rpc/service_dispatcher.rb +20 -108
  63. data/lib/protobuf/version.rb +1 -2
  64. data/lib/protobuf.rb +5 -13
  65. data/protobuf.gemspec +5 -5
  66. data/spec/benchmark/tasks.rb +2 -77
  67. data/spec/functional/zmq_server_spec.rb +13 -21
  68. data/spec/lib/protobuf/cli_spec.rb +5 -43
  69. data/spec/lib/protobuf/enum_spec.rb +194 -61
  70. data/spec/lib/protobuf/field_spec.rb +194 -0
  71. data/spec/lib/protobuf/generators/enum_generator_spec.rb +24 -1
  72. data/spec/lib/protobuf/generators/field_generator_spec.rb +6 -6
  73. data/spec/lib/protobuf/message_spec.rb +52 -70
  74. data/spec/lib/protobuf/optionable_spec.rb +46 -0
  75. data/spec/lib/protobuf/rpc/client_spec.rb +1 -93
  76. data/spec/lib/protobuf/rpc/connector_spec.rb +1 -7
  77. data/spec/lib/protobuf/rpc/connectors/zmq_spec.rb +8 -0
  78. data/spec/lib/protobuf/rpc/middleware/exception_handler_spec.rb +62 -0
  79. data/spec/lib/protobuf/rpc/middleware/logger_spec.rb +49 -0
  80. data/spec/lib/protobuf/rpc/middleware/request_decoder_spec.rb +115 -0
  81. data/spec/lib/protobuf/rpc/middleware/response_encoder_spec.rb +75 -0
  82. data/spec/lib/protobuf/rpc/servers/socket_server_spec.rb +0 -6
  83. data/spec/lib/protobuf/rpc/servers/zmq/util_spec.rb +10 -0
  84. data/spec/lib/protobuf/rpc/service_dispatcher_spec.rb +30 -105
  85. data/spec/lib/protobuf/rpc/service_filters_spec.rb +4 -4
  86. data/spec/lib/protobuf/rpc/service_spec.rb +20 -24
  87. data/spec/lib/protobuf_spec.rb +3 -3
  88. data/spec/spec_helper.rb +5 -4
  89. data/spec/support/packed_field.rb +15 -14
  90. data/spec/support/server.rb +4 -21
  91. data/spec/support/test/defaults.pb.rb +4 -4
  92. data/spec/support/test/enum.pb.rb +13 -1
  93. data/spec/support/test/enum.proto +15 -0
  94. data/spec/support/test/extended.pb.rb +1 -1
  95. data/spec/support/test/google_unittest.pb.rb +239 -241
  96. data/spec/support/test/google_unittest_import.pb.rb +2 -2
  97. data/spec/support/test/multi_field_extensions.pb.rb +2 -2
  98. data/spec/support/test/resource.pb.rb +19 -18
  99. data/spec/support/test/resource.proto +1 -0
  100. data/spec/support/test/resource_service.rb +5 -0
  101. metadata +78 -57
  102. data/bin/rprotoc +0 -8
  103. data/lib/protobuf/enum_value.rb +0 -85
  104. data/lib/protobuf/evented.rb +0 -37
  105. data/lib/protobuf/ext/eventmachine.rb +0 -14
  106. data/lib/protobuf/field/extension_fields.rb +0 -32
  107. data/lib/protobuf/message/decoder.rb +0 -72
  108. data/lib/protobuf/message/message.rb +0 -1
  109. data/lib/protobuf/rpc/connectors/em_client.rb +0 -84
  110. data/lib/protobuf/rpc/connectors/eventmachine.rb +0 -87
  111. data/lib/protobuf/rpc/servers/evented/server.rb +0 -36
  112. data/lib/protobuf/rpc/servers/evented_runner.rb +0 -31
  113. data/spec/functional/embedded_service_spec.rb +0 -7
  114. data/spec/functional/evented_server_spec.rb +0 -64
  115. data/spec/lib/protobuf/enum_value_spec.rb +0 -29
  116. data/spec/lib/protobuf/rpc/servers/evented_server_spec.rb +0 -19
@@ -3,9 +3,10 @@ require 'protobuf/field/float_field'
3
3
  module Protobuf
4
4
  module Field
5
5
  class DoubleField < FloatField
6
- def wire_type
7
- WireType::FIXED64
8
- end
6
+
7
+ ##
8
+ # Public Instance Methods
9
+ #
9
10
 
10
11
  def decode(bytes)
11
12
  bytes.unpack('E').first
@@ -14,6 +15,12 @@ module Protobuf
14
15
  def encode(value)
15
16
  [value].pack('E')
16
17
  end
18
+
19
+ def wire_type
20
+ WireType::FIXED64
21
+ end
22
+
17
23
  end
18
24
  end
19
25
  end
26
+
@@ -3,16 +3,13 @@ require 'protobuf/field/varint_field'
3
3
  module Protobuf
4
4
  module Field
5
5
  class EnumField < VarintField
6
+
7
+ ##
8
+ # Public Instance Methods
9
+ #
10
+
6
11
  def acceptable?(val)
7
- case val
8
- when Symbol then
9
- raise TypeError, "Enum #{val} is not known for type #{@type}" unless @type.const_defined?(val)
10
- when EnumValue then
11
- raise TypeError, "Enum #{val} is not owned by #{@type}" if val.parent_class != @type
12
- else
13
- raise TypeError, "Tag #{val} is not valid for Enum #{@type}" unless @type.valid_tag?(val)
14
- end
15
- true
12
+ ! type_class.fetch(val).nil?
16
13
  end
17
14
 
18
15
  def encode(value)
@@ -25,23 +22,19 @@ module Protobuf
25
22
 
26
23
  private
27
24
 
28
- def typed_default_value
29
- if @default.is_a?(Symbol)
30
- @type.const_get(@default)
31
- else
32
- self.class.default
33
- end
34
- end
25
+ ##
26
+ # Private Instance Methods
27
+ #
35
28
 
36
29
  def define_setter
37
30
  field = self
38
- @message_class.class_eval do
31
+ message_class.class_eval do
39
32
  define_method("#{field.name}=") do |value|
40
33
  orig_value = value
41
34
  if value.nil?
42
35
  @values.delete(field.name)
43
36
  else
44
- value = field.type.fetch(value)
37
+ value = field.type_class.fetch(value)
45
38
  raise TypeError, "Invalid Enum value: #{orig_value.inspect} for #{field.name}" unless value
46
39
 
47
40
  @values[field.name] = value
@@ -49,6 +42,16 @@ module Protobuf
49
42
  end
50
43
  end
51
44
  end
45
+
46
+ def typed_default_value
47
+ if default.is_a?(Symbol)
48
+ type_class.const_get(default)
49
+ else
50
+ self.class.default
51
+ end
52
+ end
53
+
52
54
  end
53
55
  end
54
56
  end
57
+
@@ -1,9 +1,17 @@
1
1
  module Protobuf
2
2
  module Field
3
3
  class FieldArray < Array
4
+
5
+ ##
6
+ # Attributes
7
+ #
8
+
9
+ attr_reader :field
10
+
4
11
  ##
5
12
  # Constructor
6
13
  #
14
+
7
15
  def initialize(field)
8
16
  @field = field
9
17
  end
@@ -11,6 +19,7 @@ module Protobuf
11
19
  ##
12
20
  # Public Instance Methods
13
21
  #
22
+
14
23
  def []=(nth, val)
15
24
  super(nth, normalize(val)) unless val.nil?
16
25
  end
@@ -23,13 +32,9 @@ module Protobuf
23
32
  super(normalize(val)) unless val.nil?
24
33
  end
25
34
 
26
- def unshift(val)
27
- super(normalize(val)) unless val.nil?
28
- end
29
-
30
35
  def replace(val)
31
36
  raise_type_error(val) unless val.is_a?(Array)
32
- val = val.map! { |v| normalize(v) }
37
+ val.map! { |v| normalize(v) }
33
38
  super(val)
34
39
  end
35
40
 
@@ -42,7 +47,11 @@ module Protobuf
42
47
  end
43
48
 
44
49
  def to_s
45
- "[#{@field.name}]"
50
+ "[#{field.name}]"
51
+ end
52
+
53
+ def unshift(val)
54
+ super(normalize(val)) unless val.nil?
46
55
  end
47
56
 
48
57
  private
@@ -50,27 +59,28 @@ module Protobuf
50
59
  ##
51
60
  # Private Instance Methods
52
61
  #
62
+
53
63
  def normalize(value)
54
64
  value = value.to_proto if value.respond_to?(:to_proto)
55
- raise TypeError, "Unacceptable value #{value} for field #{@field.name} of type #{@field.type}" unless @field.acceptable?(value)
65
+ raise TypeError, "Unacceptable value #{value} for field #{field.name} of type #{field.type_class}" unless field.acceptable?(value)
56
66
 
57
- if @field.is_a?(::Protobuf::Field::EnumField)
58
- @field.type.fetch(value)
59
- elsif @field.is_a?(::Protobuf::Field::MessageField) && value.respond_to?(:to_hash)
60
- @field.type.new(value.to_hash)
67
+ if field.is_a?(::Protobuf::Field::EnumField)
68
+ field.type_class.fetch(value)
69
+ elsif field.is_a?(::Protobuf::Field::MessageField) && value.respond_to?(:to_hash)
70
+ field.type_class.new(value.to_hash)
61
71
  else
62
72
  value
63
73
  end
64
74
  end
65
75
 
66
76
  def raise_type_error(val)
67
- error_text = <<-TYPE_ERROR
68
- Expected repeated value of type '#{@field.type}'
69
- Got '#{val.class}' for repeated protobuf field #{@field.name}
77
+ raise TypeError, <<-TYPE_ERROR
78
+ Expected repeated value of type '#{field.type_class}'
79
+ Got '#{val.class}' for repeated protobuf field #{field.name}
70
80
  TYPE_ERROR
71
-
72
- raise TypeError, error_text
73
81
  end
82
+
74
83
  end
75
84
  end
76
85
  end
86
+
@@ -1,11 +1,12 @@
1
- require 'protobuf/field/uint32_field.rb'
1
+ require 'protobuf/field/uint32_field'
2
2
 
3
3
  module Protobuf
4
4
  module Field
5
5
  class Fixed32Field < Uint32Field
6
- def wire_type
7
- ::Protobuf::WireType::FIXED32
8
- end
6
+
7
+ ##
8
+ # Public Instance Methods
9
+ #
9
10
 
10
11
  def decode(bytes)
11
12
  bytes.unpack('V').first
@@ -14,6 +15,11 @@ module Protobuf
14
15
  def encode(value)
15
16
  [value].pack('V')
16
17
  end
18
+
19
+ def wire_type
20
+ ::Protobuf::WireType::FIXED32
21
+ end
22
+
17
23
  end
18
24
  end
19
25
  end
@@ -3,9 +3,10 @@ require 'protobuf/field/uint64_field'
3
3
  module Protobuf
4
4
  module Field
5
5
  class Fixed64Field < Uint64Field
6
- def wire_type
7
- ::Protobuf::WireType::FIXED64
8
- end
6
+
7
+ ##
8
+ # Public Instance Methods
9
+ #
9
10
 
10
11
  def decode(bytes)
11
12
  # we don't use 'Q' for pack/unpack. 'Q' is machine-dependent.
@@ -17,6 +18,12 @@ module Protobuf
17
18
  # we don't use 'Q' for pack/unpack. 'Q' is machine-dependent.
18
19
  [value & 0xffff_ffff, value >> 32].pack('VV')
19
20
  end
21
+
22
+ def wire_type
23
+ ::Protobuf::WireType::FIXED64
24
+ end
25
+
20
26
  end
21
27
  end
22
28
  end
29
+
@@ -3,10 +3,21 @@ require 'protobuf/field/base_field'
3
3
  module Protobuf
4
4
  module Field
5
5
  class FloatField < BaseField
6
- def self.default; 0.0; end
7
6
 
8
- def wire_type
9
- WireType::FIXED32
7
+ ##
8
+ # Class Methods
9
+ #
10
+
11
+ def self.default
12
+ 0.0
13
+ end
14
+
15
+ ##
16
+ # Public Instance Methods
17
+ #
18
+
19
+ def acceptable?(val)
20
+ val.respond_to?(:to_f)
10
21
  end
11
22
 
12
23
  def decode(bytes)
@@ -17,9 +28,11 @@ module Protobuf
17
28
  [value].pack('e')
18
29
  end
19
30
 
20
- def acceptable?(val)
21
- val.respond_to?(:to_f)
31
+ def wire_type
32
+ WireType::FIXED32
22
33
  end
34
+
23
35
  end
24
36
  end
25
37
  end
38
+
@@ -3,10 +3,20 @@ require 'protobuf/field/integer_field'
3
3
  module Protobuf
4
4
  module Field
5
5
  class Int32Field < IntegerField
6
- def self.max; INT32_MAX; end
7
- def self.min; INT32_MIN; end
8
- def max; INT32_MAX; end
9
- def min; INT32_MIN; end
6
+
7
+ ##
8
+ # Class Methods
9
+ #
10
+
11
+ def self.max
12
+ INT32_MAX
13
+ end
14
+
15
+ def self.min
16
+ INT32_MIN
17
+ end
18
+
10
19
  end
11
20
  end
12
21
  end
22
+
@@ -3,10 +3,20 @@ require 'protobuf/field/integer_field'
3
3
  module Protobuf
4
4
  module Field
5
5
  class Int64Field < IntegerField
6
- def self.max; INT64_MAX; end
7
- def self.min; INT64_MIN; end
8
- def max; INT64_MAX; end
9
- def min; INT64_MIN; end
6
+
7
+ ##
8
+ # Class Methods
9
+ #
10
+
11
+ def self.max
12
+ INT64_MAX
13
+ end
14
+
15
+ def self.min
16
+ INT64_MIN
17
+ end
18
+
10
19
  end
11
20
  end
12
21
  end
22
+
@@ -4,16 +4,21 @@ module Protobuf
4
4
  module Field
5
5
  class IntegerField < VarintField
6
6
 
7
- def encode(value)
8
- # original Google's library uses 64bits integer for negative value
9
- VarintField.encode(value & 0xffff_ffff_ffff_ffff)
10
- end
7
+ ##
8
+ # Public Instance Methods
9
+ #
11
10
 
12
11
  def decode(value)
13
12
  value -= 0x1_0000_0000_0000_0000 if (value & 0x8000_0000_0000_0000).nonzero?
14
13
  value
15
14
  end
16
15
 
16
+ def encode(value)
17
+ # original Google's library uses 64bits integer for negative value
18
+ ::Protobuf::Field::VarintField.encode(value & 0xffff_ffff_ffff_ffff)
19
+ end
20
+
17
21
  end
18
22
  end
19
23
  end
24
+
@@ -3,18 +3,21 @@ require 'protobuf/field/base_field'
3
3
  module Protobuf
4
4
  module Field
5
5
  class MessageField < BaseField
6
- RAISE_TYPE = lambda { |field, val| raise TypeError, "Expected value of type '#{field.type}' for field #{field.name}, but got '#{val.class}'" }
7
6
 
8
7
  ##
9
8
  # Public Instance Methods
10
9
  #
10
+
11
11
  def acceptable?(val)
12
- RAISE_TYPE.call(self, val) unless val.instance_of?(type) || val.respond_to?(:to_hash)
12
+ unless val.instance_of?(type_class) || val.respond_to?(:to_hash)
13
+ raise TypeError, "Expected value of type '#{type_class}' for field #{name}, but got '#{val.class}'"
14
+ end
15
+
13
16
  true
14
17
  end
15
18
 
16
19
  def decode(bytes)
17
- type.decode(bytes)
20
+ type_class.decode(bytes)
18
21
  end
19
22
 
20
23
  def encode(value)
@@ -33,25 +36,31 @@ module Protobuf
33
36
 
34
37
  private
35
38
 
39
+ ##
40
+ # Private Instance Methods
41
+ #
42
+
36
43
  def define_setter
37
44
  field = self
38
- @message_class.class_eval do
45
+ message_class.class_eval do
39
46
  define_method("#{field.name}=") do |val|
40
47
  case
41
48
  when val.nil? then
42
49
  @values.delete(field.name)
43
- when val.is_a?(field.type) then
50
+ when val.is_a?(field.type_class) then
44
51
  @values[field.name] = val
45
52
  when val.respond_to?(:to_proto) then
46
53
  @values[field.name] = val.to_proto
47
54
  when val.respond_to?(:to_hash) then
48
- @values[field.name] = field.type.new(val.to_hash)
55
+ @values[field.name] = field.type_class.new(val.to_hash)
49
56
  else
50
- RAISE_TYPE.call(field, val)
57
+ raise TypeError, "Expected value of type '#{field.type_class}' for field #{field.name}, but got '#{val.class}'"
51
58
  end
52
59
  end
53
60
  end
54
61
  end
62
+
55
63
  end
56
64
  end
57
65
  end
66
+
@@ -3,9 +3,10 @@ require 'protobuf/field/int32_field'
3
3
  module Protobuf
4
4
  module Field
5
5
  class Sfixed32Field < Int32Field
6
- def wire_type
7
- ::Protobuf::WireType::FIXED32
8
- end
6
+
7
+ ##
8
+ # Public Instance Methods
9
+ #
9
10
 
10
11
  def decode(bytes)
11
12
  value = bytes.unpack('V').first
@@ -16,6 +17,12 @@ module Protobuf
16
17
  def encode(value)
17
18
  [value].pack('V')
18
19
  end
20
+
21
+ def wire_type
22
+ ::Protobuf::WireType::FIXED32
23
+ end
24
+
19
25
  end
20
26
  end
21
27
  end
28
+
@@ -3,22 +3,27 @@ require 'protobuf/field/int64_field'
3
3
  module Protobuf
4
4
  module Field
5
5
  class Sfixed64Field < Int64Field
6
- def wire_type
7
- ::Protobuf::WireType::FIXED64
8
- end
6
+
7
+ ##
8
+ # Public Instance Methods
9
+ #
9
10
 
10
11
  def decode(bytes)
11
- # we don't use 'Q' for pack/unpack. 'Q' is machine-dependent.
12
- values = bytes.unpack('VV')
12
+ values = bytes.unpack('VV') # 'Q' is machine-dependent, don't use
13
13
  value = values[0] + (values[1] << 32)
14
14
  value -= 0x1_0000_0000_0000_0000 if (value & 0x8000_0000_0000_0000).nonzero?
15
15
  value
16
16
  end
17
17
 
18
18
  def encode(value)
19
- # we don't use 'Q' for pack/unpack. 'Q' is machine-dependent.
20
- [value & 0xffff_ffff, value >> 32].pack('VV')
19
+ [value & 0xffff_ffff, value >> 32].pack('VV') # 'Q' is machine-dependent, don't use
20
+ end
21
+
22
+ def wire_type
23
+ ::Protobuf::WireType::FIXED64
21
24
  end
25
+
22
26
  end
23
27
  end
24
28
  end
29
+
@@ -3,6 +3,11 @@ require 'protobuf/field/varint_field'
3
3
  module Protobuf
4
4
  module Field
5
5
  class SignedIntegerField < VarintField
6
+
7
+ ##
8
+ # Public Instance Methods
9
+ #
10
+
6
11
  def decode(value)
7
12
  if (value & 1).zero?
8
13
  value >> 1 # positive value
@@ -18,6 +23,8 @@ module Protobuf
18
23
  VarintField.encode(~(value << 1))
19
24
  end
20
25
  end
26
+
21
27
  end
22
28
  end
23
29
  end
30
+
@@ -3,10 +3,20 @@ require 'protobuf/field/signed_integer_field'
3
3
  module Protobuf
4
4
  module Field
5
5
  class Sint32Field < SignedIntegerField
6
- def self.max; INT32_MAX; end
7
- def self.min; INT32_MIN; end
8
- def max; INT32_MAX; end
9
- def min; INT32_MIN; end
6
+
7
+ ##
8
+ # Class Methods
9
+ #
10
+
11
+ def self.max
12
+ INT32_MAX
13
+ end
14
+
15
+ def self.min
16
+ INT32_MIN
17
+ end
18
+
10
19
  end
11
20
  end
12
21
  end
22
+
@@ -3,10 +3,20 @@ require 'protobuf/field/signed_integer_field'
3
3
  module Protobuf
4
4
  module Field
5
5
  class Sint64Field < SignedIntegerField
6
- def self.max; INT64_MAX; end
7
- def self.min; INT64_MIN; end
8
- def max; INT64_MAX; end
9
- def min; INT64_MIN; end
6
+
7
+ ##
8
+ # Class Methods
9
+ #
10
+
11
+ def self.max
12
+ INT64_MAX
13
+ end
14
+
15
+ def self.min
16
+ INT64_MIN
17
+ end
18
+
10
19
  end
11
20
  end
12
21
  end
22
+
@@ -3,15 +3,23 @@ require 'protobuf/field/bytes_field'
3
3
  module Protobuf
4
4
  module Field
5
5
  class StringField < BytesField
6
+
7
+ ##
8
+ # Constants
9
+ #
10
+
6
11
  ENCODING = 'UTF-8'.freeze
7
12
 
13
+ ##
14
+ # Public Instance Methods
15
+ #
16
+
8
17
  def decode(bytes)
9
18
  bytes_to_decode = bytes.dup
10
19
  bytes_to_decode.force_encoding(::Protobuf::Field::StringField::ENCODING)
11
20
  bytes_to_decode
12
21
  end
13
22
 
14
- # TODO: make replace character configurable?
15
23
  def encode(value)
16
24
  value_to_encode = value.dup
17
25
  value_to_encode.encode!(::Protobuf::Field::StringField::ENCODING, :invalid => :replace, :undef => :replace, :replace => "")
@@ -20,6 +28,8 @@ module Protobuf
20
28
  string_size = ::Protobuf::Field::VarintField.encode(value_to_encode.size)
21
29
  string_size << value_to_encode
22
30
  end
31
+
23
32
  end
24
33
  end
25
34
  end
35
+
@@ -3,10 +3,20 @@ require 'protobuf/field/varint_field'
3
3
  module Protobuf
4
4
  module Field
5
5
  class Uint32Field < VarintField
6
- def self.max; UINT32_MAX; end
7
- def self.min; 0; end
8
- def max; UINT32_MAX; end
9
- def min; 0; end
6
+
7
+ ##
8
+ # Class Methods
9
+ #
10
+
11
+ def self.max
12
+ UINT32_MAX
13
+ end
14
+
15
+ def self.min
16
+ 0
17
+ end
18
+
10
19
  end
11
20
  end
12
21
  end
22
+
@@ -3,10 +3,20 @@ require 'protobuf/field/varint_field'
3
3
  module Protobuf
4
4
  module Field
5
5
  class Uint64Field < VarintField
6
- def self.max; UINT64_MAX; end
7
- def self.min; 0; end
8
- def max; UINT64_MAX; end
9
- def min; 0; end
6
+
7
+ ##
8
+ # Class Methods
9
+ #
10
+
11
+ def self.max
12
+ UINT64_MAX
13
+ end
14
+
15
+ def self.min
16
+ 0
17
+ end
18
+
10
19
  end
11
20
  end
12
21
  end
22
+
@@ -3,6 +3,11 @@ require 'protobuf/field/base_field'
3
3
  module Protobuf
4
4
  module Field
5
5
  class VarintField < BaseField
6
+
7
+ ##
8
+ # Constants
9
+ #
10
+
6
11
  INT32_MAX = 2**31 - 1
7
12
  INT32_MIN = -2**31
8
13
  INT64_MAX = 2**63 - 1
@@ -13,18 +18,11 @@ module Protobuf
13
18
  ##
14
19
  # Class Methods
15
20
  #
21
+
16
22
  def self.default
17
23
  0
18
24
  end
19
25
 
20
- def self.decode(bytes)
21
- value = 0
22
- bytes.each_with_index do |byte, index|
23
- value |= byte << (7 * index)
24
- end
25
- value
26
- end
27
-
28
26
  def self.encode(value)
29
27
  bytes = []
30
28
  until value < 128
@@ -37,8 +35,11 @@ module Protobuf
37
35
  ##
38
36
  # Public Instance Methods
39
37
  #
38
+
40
39
  def acceptable?(val)
41
- (val > min || val < max) rescue false
40
+ (val > self.class.min || val < self.class.max)
41
+ rescue
42
+ false
42
43
  end
43
44
 
44
45
  def decode(value)
@@ -64,3 +65,4 @@ module Protobuf
64
65
  end
65
66
  end
66
67
  end
68
+