google-protobuf 3.8.0 → 3.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of google-protobuf might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/ext/google/protobuf_c/defs.c +942 -833
- data/ext/google/protobuf_c/encode_decode.c +482 -301
- data/ext/google/protobuf_c/extconf.rb +2 -4
- data/ext/google/protobuf_c/map.c +39 -58
- data/ext/google/protobuf_c/message.c +124 -114
- data/ext/google/protobuf_c/protobuf.c +30 -15
- data/ext/google/protobuf_c/protobuf.h +109 -57
- data/ext/google/protobuf_c/repeated_field.c +47 -21
- data/ext/google/protobuf_c/storage.c +305 -169
- data/ext/google/protobuf_c/upb.c +5335 -8998
- data/ext/google/protobuf_c/upb.h +4634 -8498
- data/lib/google/protobuf.rb +70 -0
- data/lib/google/protobuf/any_pb.rb +1 -1
- data/lib/google/protobuf/api_pb.rb +3 -3
- data/lib/google/protobuf/duration_pb.rb +1 -1
- data/lib/google/protobuf/empty_pb.rb +1 -1
- data/lib/google/protobuf/field_mask_pb.rb +1 -1
- data/lib/google/protobuf/source_context_pb.rb +1 -1
- data/lib/google/protobuf/struct_pb.rb +4 -4
- data/lib/google/protobuf/timestamp_pb.rb +1 -1
- data/lib/google/protobuf/type_pb.rb +8 -8
- data/lib/google/protobuf/well_known_types.rb +8 -2
- data/lib/google/protobuf/wrappers_pb.rb +9 -9
- data/tests/basic.rb +229 -67
- data/tests/generated_code_test.rb +0 -0
- data/tests/stress.rb +0 -0
- metadata +17 -10
data/lib/google/protobuf.rb
CHANGED
@@ -50,6 +50,76 @@ else
|
|
50
50
|
rescue LoadError
|
51
51
|
require 'google/protobuf_c'
|
52
52
|
end
|
53
|
+
|
54
|
+
module Google
|
55
|
+
module Protobuf
|
56
|
+
module Internal
|
57
|
+
def self.infer_package(names)
|
58
|
+
# Package is longest common prefix ending in '.', if any.
|
59
|
+
if not names.empty?
|
60
|
+
min, max = names.minmax
|
61
|
+
last_common_dot = nil
|
62
|
+
min.size.times { |i|
|
63
|
+
if min[i] != max[i] then break end
|
64
|
+
if min[i] == ?. then last_common_dot = i end
|
65
|
+
}
|
66
|
+
if last_common_dot
|
67
|
+
return min.slice(0, last_common_dot)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
nil
|
72
|
+
end
|
73
|
+
|
74
|
+
class NestingBuilder
|
75
|
+
def initialize(msg_names, enum_names)
|
76
|
+
@to_pos = {nil=>nil}
|
77
|
+
@msg_children = Hash.new { |hash, key| hash[key] = [] }
|
78
|
+
@enum_children = Hash.new { |hash, key| hash[key] = [] }
|
79
|
+
|
80
|
+
msg_names.each_with_index { |name, idx| @to_pos[name] = idx }
|
81
|
+
enum_names.each_with_index { |name, idx| @to_pos[name] = idx }
|
82
|
+
|
83
|
+
msg_names.each { |name| @msg_children[parent(name)] << name }
|
84
|
+
enum_names.each { |name| @enum_children[parent(name)] << name }
|
85
|
+
end
|
86
|
+
|
87
|
+
def build(package)
|
88
|
+
return build_msg(package)
|
89
|
+
end
|
90
|
+
|
91
|
+
private
|
92
|
+
def build_msg(msg)
|
93
|
+
return {
|
94
|
+
:pos => @to_pos[msg],
|
95
|
+
:msgs => @msg_children[msg].map { |child| build_msg(child) },
|
96
|
+
:enums => @enum_children[msg].map { |child| @to_pos[child] },
|
97
|
+
}
|
98
|
+
end
|
99
|
+
|
100
|
+
private
|
101
|
+
def parent(name)
|
102
|
+
idx = name.rindex(?.)
|
103
|
+
if idx
|
104
|
+
return name.slice(0, idx)
|
105
|
+
else
|
106
|
+
return nil
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
def self.fixup_descriptor(package, msg_names, enum_names)
|
112
|
+
if package.nil?
|
113
|
+
package = self.infer_package(msg_names + enum_names)
|
114
|
+
end
|
115
|
+
|
116
|
+
nesting = NestingBuilder.new(msg_names, enum_names).build(package)
|
117
|
+
|
118
|
+
return package, nesting
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
53
123
|
end
|
54
124
|
|
55
125
|
require 'google/protobuf/repeated_field'
|
@@ -34,8 +34,8 @@ end
|
|
34
34
|
|
35
35
|
module Google
|
36
36
|
module Protobuf
|
37
|
-
Api = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.protobuf.Api").msgclass
|
38
|
-
Method = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.protobuf.Method").msgclass
|
39
|
-
Mixin = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.protobuf.Mixin").msgclass
|
37
|
+
Api = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("google.protobuf.Api").msgclass
|
38
|
+
Method = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("google.protobuf.Method").msgclass
|
39
|
+
Mixin = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("google.protobuf.Mixin").msgclass
|
40
40
|
end
|
41
41
|
end
|
@@ -14,6 +14,6 @@ end
|
|
14
14
|
|
15
15
|
module Google
|
16
16
|
module Protobuf
|
17
|
-
Duration = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.protobuf.Duration").msgclass
|
17
|
+
Duration = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("google.protobuf.Duration").msgclass
|
18
18
|
end
|
19
19
|
end
|
@@ -13,6 +13,6 @@ end
|
|
13
13
|
|
14
14
|
module Google
|
15
15
|
module Protobuf
|
16
|
-
FieldMask = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.protobuf.FieldMask").msgclass
|
16
|
+
FieldMask = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("google.protobuf.FieldMask").msgclass
|
17
17
|
end
|
18
18
|
end
|
@@ -13,6 +13,6 @@ end
|
|
13
13
|
|
14
14
|
module Google
|
15
15
|
module Protobuf
|
16
|
-
SourceContext = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.protobuf.SourceContext").msgclass
|
16
|
+
SourceContext = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("google.protobuf.SourceContext").msgclass
|
17
17
|
end
|
18
18
|
end
|
@@ -29,9 +29,9 @@ end
|
|
29
29
|
|
30
30
|
module Google
|
31
31
|
module Protobuf
|
32
|
-
Struct = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.protobuf.Struct").msgclass
|
33
|
-
Value = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.protobuf.Value").msgclass
|
34
|
-
ListValue = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.protobuf.ListValue").msgclass
|
35
|
-
NullValue = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.protobuf.NullValue").enummodule
|
32
|
+
Struct = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("google.protobuf.Struct").msgclass
|
33
|
+
Value = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("google.protobuf.Value").msgclass
|
34
|
+
ListValue = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("google.protobuf.ListValue").msgclass
|
35
|
+
NullValue = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("google.protobuf.NullValue").enummodule
|
36
36
|
end
|
37
37
|
end
|
@@ -14,6 +14,6 @@ end
|
|
14
14
|
|
15
15
|
module Google
|
16
16
|
module Protobuf
|
17
|
-
Timestamp = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.protobuf.Timestamp").msgclass
|
17
|
+
Timestamp = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("google.protobuf.Timestamp").msgclass
|
18
18
|
end
|
19
19
|
end
|
@@ -79,13 +79,13 @@ end
|
|
79
79
|
|
80
80
|
module Google
|
81
81
|
module Protobuf
|
82
|
-
Type = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.protobuf.Type").msgclass
|
83
|
-
Field = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.protobuf.Field").msgclass
|
84
|
-
Field::Kind = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.protobuf.Field.Kind").enummodule
|
85
|
-
Field::Cardinality = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.protobuf.Field.Cardinality").enummodule
|
86
|
-
Enum = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.protobuf.Enum").msgclass
|
87
|
-
EnumValue = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.protobuf.EnumValue").msgclass
|
88
|
-
Option = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.protobuf.Option").msgclass
|
89
|
-
Syntax = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.protobuf.Syntax").enummodule
|
82
|
+
Type = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("google.protobuf.Type").msgclass
|
83
|
+
Field = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("google.protobuf.Field").msgclass
|
84
|
+
Field::Kind = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("google.protobuf.Field.Kind").enummodule
|
85
|
+
Field::Cardinality = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("google.protobuf.Field.Cardinality").enummodule
|
86
|
+
Enum = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("google.protobuf.Enum").msgclass
|
87
|
+
EnumValue = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("google.protobuf.EnumValue").msgclass
|
88
|
+
Option = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("google.protobuf.Option").msgclass
|
89
|
+
Syntax = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("google.protobuf.Syntax").enummodule
|
90
90
|
end
|
91
91
|
end
|
@@ -72,8 +72,14 @@ module Google
|
|
72
72
|
end
|
73
73
|
|
74
74
|
Timestamp.class_eval do
|
75
|
-
|
76
|
-
|
75
|
+
if RUBY_VERSION < "2.5"
|
76
|
+
def to_time
|
77
|
+
Time.at(self.to_f)
|
78
|
+
end
|
79
|
+
else
|
80
|
+
def to_time
|
81
|
+
Time.at(seconds, nanos, :nanosecond)
|
82
|
+
end
|
77
83
|
end
|
78
84
|
|
79
85
|
def from_time(time)
|
@@ -37,14 +37,14 @@ end
|
|
37
37
|
|
38
38
|
module Google
|
39
39
|
module Protobuf
|
40
|
-
DoubleValue = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.protobuf.DoubleValue").msgclass
|
41
|
-
FloatValue = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.protobuf.FloatValue").msgclass
|
42
|
-
Int64Value = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.protobuf.Int64Value").msgclass
|
43
|
-
UInt64Value = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.protobuf.UInt64Value").msgclass
|
44
|
-
Int32Value = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.protobuf.Int32Value").msgclass
|
45
|
-
UInt32Value = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.protobuf.UInt32Value").msgclass
|
46
|
-
BoolValue = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.protobuf.BoolValue").msgclass
|
47
|
-
StringValue = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.protobuf.StringValue").msgclass
|
48
|
-
BytesValue = Google::Protobuf::DescriptorPool.generated_pool.lookup("google.protobuf.BytesValue").msgclass
|
40
|
+
DoubleValue = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("google.protobuf.DoubleValue").msgclass
|
41
|
+
FloatValue = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("google.protobuf.FloatValue").msgclass
|
42
|
+
Int64Value = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("google.protobuf.Int64Value").msgclass
|
43
|
+
UInt64Value = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("google.protobuf.UInt64Value").msgclass
|
44
|
+
Int32Value = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("google.protobuf.Int32Value").msgclass
|
45
|
+
UInt32Value = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("google.protobuf.UInt32Value").msgclass
|
46
|
+
BoolValue = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("google.protobuf.BoolValue").msgclass
|
47
|
+
StringValue = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("google.protobuf.StringValue").msgclass
|
48
|
+
BytesValue = ::Google::Protobuf::DescriptorPool.generated_pool.lookup("google.protobuf.BytesValue").msgclass
|
49
49
|
end
|
50
50
|
end
|
data/tests/basic.rb
CHANGED
@@ -17,7 +17,6 @@ module BasicTest
|
|
17
17
|
add_message "BadFieldNames" do
|
18
18
|
optional :dup, :int32, 1
|
19
19
|
optional :class, :int32, 2
|
20
|
-
optional :"a.b", :int32, 3
|
21
20
|
end
|
22
21
|
end
|
23
22
|
|
@@ -33,11 +32,11 @@ module BasicTest
|
|
33
32
|
include CommonTests
|
34
33
|
|
35
34
|
def test_has_field
|
36
|
-
m =
|
37
|
-
assert !m.
|
38
|
-
m.
|
39
|
-
assert m.
|
40
|
-
assert
|
35
|
+
m = TestSingularFields.new
|
36
|
+
assert !m.has_singular_msg?
|
37
|
+
m.singular_msg = TestMessage2.new
|
38
|
+
assert m.has_singular_msg?
|
39
|
+
assert TestSingularFields.descriptor.lookup('singular_msg').has?(m)
|
41
40
|
|
42
41
|
m = OneofMessage.new
|
43
42
|
assert !m.has_my_oneof?
|
@@ -46,32 +45,31 @@ module BasicTest
|
|
46
45
|
assert_raise NoMethodError do
|
47
46
|
m.has_a?
|
48
47
|
end
|
49
|
-
|
50
|
-
OneofMessage.descriptor.lookup('a').has?(m)
|
51
|
-
end
|
48
|
+
assert_true OneofMessage.descriptor.lookup('a').has?(m)
|
52
49
|
|
53
|
-
m =
|
50
|
+
m = TestSingularFields.new
|
54
51
|
assert_raise NoMethodError do
|
55
|
-
m.
|
52
|
+
m.has_singular_int32?
|
56
53
|
end
|
57
54
|
assert_raise ArgumentError do
|
58
|
-
|
55
|
+
TestSingularFields.descriptor.lookup('singular_int32').has?(m)
|
59
56
|
end
|
60
57
|
|
61
58
|
assert_raise NoMethodError do
|
62
|
-
m.
|
59
|
+
m.has_singular_string?
|
63
60
|
end
|
64
61
|
assert_raise ArgumentError do
|
65
|
-
|
62
|
+
TestSingularFields.descriptor.lookup('singular_string').has?(m)
|
66
63
|
end
|
67
64
|
|
68
65
|
assert_raise NoMethodError do
|
69
|
-
m.
|
66
|
+
m.has_singular_bool?
|
70
67
|
end
|
71
68
|
assert_raise ArgumentError do
|
72
|
-
|
69
|
+
TestSingularFields.descriptor.lookup('singular_bool').has?(m)
|
73
70
|
end
|
74
71
|
|
72
|
+
m = TestMessage.new
|
75
73
|
assert_raise NoMethodError do
|
76
74
|
m.has_repeated_msg?
|
77
75
|
end
|
@@ -80,40 +78,59 @@ module BasicTest
|
|
80
78
|
end
|
81
79
|
end
|
82
80
|
|
81
|
+
def test_no_presence
|
82
|
+
m = TestSingularFields.new
|
83
|
+
|
84
|
+
# Explicitly setting to zero does not cause anything to be serialized.
|
85
|
+
m.singular_int32 = 0
|
86
|
+
assert_equal "", TestSingularFields.encode(m)
|
87
|
+
|
88
|
+
# Explicitly setting to a non-zero value *does* cause serialization.
|
89
|
+
m.singular_int32 = 1
|
90
|
+
assert_not_equal "", TestSingularFields.encode(m)
|
91
|
+
|
92
|
+
m.singular_int32 = 0
|
93
|
+
assert_equal "", TestSingularFields.encode(m)
|
94
|
+
end
|
95
|
+
|
83
96
|
def test_set_clear_defaults
|
84
|
-
m =
|
97
|
+
m = TestSingularFields.new
|
98
|
+
|
99
|
+
m.singular_int32 = -42
|
100
|
+
assert_equal -42, m.singular_int32
|
101
|
+
m.clear_singular_int32
|
102
|
+
assert_equal 0, m.singular_int32
|
103
|
+
|
104
|
+
m.singular_int32 = 50
|
105
|
+
assert_equal 50, m.singular_int32
|
106
|
+
TestSingularFields.descriptor.lookup('singular_int32').clear(m)
|
107
|
+
assert_equal 0, m.singular_int32
|
108
|
+
|
109
|
+
m.singular_string = "foo bar"
|
110
|
+
assert_equal "foo bar", m.singular_string
|
111
|
+
m.clear_singular_string
|
112
|
+
assert_equal "", m.singular_string
|
113
|
+
|
114
|
+
m.singular_string = "foo"
|
115
|
+
assert_equal "foo", m.singular_string
|
116
|
+
TestSingularFields.descriptor.lookup('singular_string').clear(m)
|
117
|
+
assert_equal "", m.singular_string
|
118
|
+
|
119
|
+
m.singular_msg = TestMessage2.new(:foo => 42)
|
120
|
+
assert_equal TestMessage2.new(:foo => 42), m.singular_msg
|
121
|
+
assert m.has_singular_msg?
|
122
|
+
m.clear_singular_msg
|
123
|
+
assert_equal nil, m.singular_msg
|
124
|
+
assert !m.has_singular_msg?
|
125
|
+
|
126
|
+
m.singular_msg = TestMessage2.new(:foo => 42)
|
127
|
+
assert_equal TestMessage2.new(:foo => 42), m.singular_msg
|
128
|
+
TestSingularFields.descriptor.lookup('singular_msg').clear(m)
|
129
|
+
assert_equal nil, m.singular_msg
|
130
|
+
end
|
85
131
|
|
86
|
-
|
87
|
-
|
88
|
-
m.clear_optional_int32
|
89
|
-
assert_equal 0, m.optional_int32
|
90
|
-
|
91
|
-
m.optional_int32 = 50
|
92
|
-
assert_equal 50, m.optional_int32
|
93
|
-
TestMessage.descriptor.lookup('optional_int32').clear(m)
|
94
|
-
assert_equal 0, m.optional_int32
|
95
|
-
|
96
|
-
m.optional_string = "foo bar"
|
97
|
-
assert_equal "foo bar", m.optional_string
|
98
|
-
m.clear_optional_string
|
99
|
-
assert_equal "", m.optional_string
|
100
|
-
|
101
|
-
m.optional_string = "foo"
|
102
|
-
assert_equal "foo", m.optional_string
|
103
|
-
TestMessage.descriptor.lookup('optional_string').clear(m)
|
104
|
-
assert_equal "", m.optional_string
|
105
|
-
|
106
|
-
m.optional_msg = TestMessage2.new(:foo => 42)
|
107
|
-
assert_equal TestMessage2.new(:foo => 42), m.optional_msg
|
108
|
-
assert m.has_optional_msg?
|
109
|
-
m.clear_optional_msg
|
110
|
-
assert_equal nil, m.optional_msg
|
111
|
-
assert !m.has_optional_msg?
|
112
|
-
|
113
|
-
m.optional_msg = TestMessage2.new(:foo => 42)
|
114
|
-
assert_equal TestMessage2.new(:foo => 42), m.optional_msg
|
115
|
-
TestMessage.descriptor.lookup('optional_msg').clear(m)
|
116
|
-
assert_equal nil, m.optional_msg
|
132
|
+
def test_clear_repeated_fields
|
133
|
+
m = TestMessage.new
|
117
134
|
|
118
135
|
m.repeated_int32.push(1)
|
119
136
|
assert_equal [1], m.repeated_int32
|
@@ -129,6 +146,7 @@ module BasicTest
|
|
129
146
|
m.a = "foo"
|
130
147
|
assert_equal "foo", m.a
|
131
148
|
assert m.has_my_oneof?
|
149
|
+
assert_equal :a, m.my_oneof
|
132
150
|
m.clear_a
|
133
151
|
assert !m.has_my_oneof?
|
134
152
|
|
@@ -144,7 +162,6 @@ module BasicTest
|
|
144
162
|
assert !m.has_my_oneof?
|
145
163
|
end
|
146
164
|
|
147
|
-
|
148
165
|
def test_initialization_map_errors
|
149
166
|
e = assert_raise ArgumentError do
|
150
167
|
TestMessage.new(:hello => "world")
|
@@ -170,10 +187,12 @@ module BasicTest
|
|
170
187
|
m = MapMessage.new(
|
171
188
|
:map_string_int32 => {"a" => 1, "b" => 2},
|
172
189
|
:map_string_msg => {"a" => TestMessage2.new(:foo => 1),
|
173
|
-
"b" => TestMessage2.new(:foo => 2)}
|
190
|
+
"b" => TestMessage2.new(:foo => 2)},
|
191
|
+
:map_string_enum => {"a" => :A, "b" => :B})
|
174
192
|
assert m.map_string_int32.keys.sort == ["a", "b"]
|
175
193
|
assert m.map_string_int32["a"] == 1
|
176
194
|
assert m.map_string_msg["b"].foo == 2
|
195
|
+
assert m.map_string_enum["a"] == :A
|
177
196
|
|
178
197
|
m.map_string_int32["c"] = 3
|
179
198
|
assert m.map_string_int32["c"] == 3
|
@@ -202,12 +221,27 @@ module BasicTest
|
|
202
221
|
end
|
203
222
|
end
|
204
223
|
|
224
|
+
def test_map_field_with_symbol
|
225
|
+
m = MapMessage.new
|
226
|
+
assert m.map_string_int32 == {}
|
227
|
+
assert m.map_string_msg == {}
|
228
|
+
|
229
|
+
m = MapMessage.new(
|
230
|
+
:map_string_int32 => {a: 1, "b" => 2},
|
231
|
+
:map_string_msg => {a: TestMessage2.new(:foo => 1),
|
232
|
+
b: TestMessage2.new(:foo => 10)})
|
233
|
+
assert_equal 1, m.map_string_int32[:a]
|
234
|
+
assert_equal 2, m.map_string_int32[:b]
|
235
|
+
assert_equal 10, m.map_string_msg[:b].foo
|
236
|
+
end
|
237
|
+
|
205
238
|
def test_map_inspect
|
206
239
|
m = MapMessage.new(
|
207
240
|
:map_string_int32 => {"a" => 1, "b" => 2},
|
208
241
|
:map_string_msg => {"a" => TestMessage2.new(:foo => 1),
|
209
|
-
"b" => TestMessage2.new(:foo => 2)}
|
210
|
-
|
242
|
+
"b" => TestMessage2.new(:foo => 2)},
|
243
|
+
:map_string_enum => {"a" => :A, "b" => :B})
|
244
|
+
expected = "<BasicTest::MapMessage: map_string_int32: {\"b\"=>2, \"a\"=>1}, map_string_msg: {\"b\"=><BasicTest::TestMessage2: foo: 2>, \"a\"=><BasicTest::TestMessage2: foo: 1>}, map_string_enum: {\"b\"=>:B, \"a\"=>:A}>"
|
211
245
|
assert_equal expected, m.inspect
|
212
246
|
end
|
213
247
|
|
@@ -218,6 +252,128 @@ module BasicTest
|
|
218
252
|
m.map_string_int32['aaa'] = 3
|
219
253
|
end
|
220
254
|
|
255
|
+
def test_map_wrappers
|
256
|
+
run_asserts = ->(m) {
|
257
|
+
assert_equal 2.0, m.map_double[0].value
|
258
|
+
assert_equal 4.0, m.map_float[0].value
|
259
|
+
assert_equal 3, m.map_int32[0].value
|
260
|
+
assert_equal 4, m.map_int64[0].value
|
261
|
+
assert_equal 5, m.map_uint32[0].value
|
262
|
+
assert_equal 6, m.map_uint64[0].value
|
263
|
+
assert_equal true, m.map_bool[0].value
|
264
|
+
assert_equal 'str', m.map_string[0].value
|
265
|
+
assert_equal 'fun', m.map_bytes[0].value
|
266
|
+
}
|
267
|
+
|
268
|
+
m = proto_module::Wrapper.new(
|
269
|
+
map_double: {0 => Google::Protobuf::DoubleValue.new(value: 2.0)},
|
270
|
+
map_float: {0 => Google::Protobuf::FloatValue.new(value: 4.0)},
|
271
|
+
map_int32: {0 => Google::Protobuf::Int32Value.new(value: 3)},
|
272
|
+
map_int64: {0 => Google::Protobuf::Int64Value.new(value: 4)},
|
273
|
+
map_uint32: {0 => Google::Protobuf::UInt32Value.new(value: 5)},
|
274
|
+
map_uint64: {0 => Google::Protobuf::UInt64Value.new(value: 6)},
|
275
|
+
map_bool: {0 => Google::Protobuf::BoolValue.new(value: true)},
|
276
|
+
map_string: {0 => Google::Protobuf::StringValue.new(value: 'str')},
|
277
|
+
map_bytes: {0 => Google::Protobuf::BytesValue.new(value: 'fun')},
|
278
|
+
)
|
279
|
+
|
280
|
+
run_asserts.call(m)
|
281
|
+
serialized = proto_module::Wrapper::encode(m)
|
282
|
+
m2 = proto_module::Wrapper::decode(serialized)
|
283
|
+
run_asserts.call(m2)
|
284
|
+
|
285
|
+
# Test the case where we are serializing directly from the parsed form
|
286
|
+
# (before anything lazy is materialized).
|
287
|
+
m3 = proto_module::Wrapper::decode(serialized)
|
288
|
+
serialized2 = proto_module::Wrapper::encode(m3)
|
289
|
+
m4 = proto_module::Wrapper::decode(serialized2)
|
290
|
+
run_asserts.call(m4)
|
291
|
+
|
292
|
+
# Test that the lazy form compares equal to the expanded form.
|
293
|
+
m5 = proto_module::Wrapper::decode(serialized2)
|
294
|
+
assert_equal m5, m
|
295
|
+
end
|
296
|
+
|
297
|
+
def test_map_wrappers_with_default_values
|
298
|
+
run_asserts = ->(m) {
|
299
|
+
assert_equal 0.0, m.map_double[0].value
|
300
|
+
assert_equal 0.0, m.map_float[0].value
|
301
|
+
assert_equal 0, m.map_int32[0].value
|
302
|
+
assert_equal 0, m.map_int64[0].value
|
303
|
+
assert_equal 0, m.map_uint32[0].value
|
304
|
+
assert_equal 0, m.map_uint64[0].value
|
305
|
+
assert_equal false, m.map_bool[0].value
|
306
|
+
assert_equal '', m.map_string[0].value
|
307
|
+
assert_equal '', m.map_bytes[0].value
|
308
|
+
}
|
309
|
+
|
310
|
+
m = proto_module::Wrapper.new(
|
311
|
+
map_double: {0 => Google::Protobuf::DoubleValue.new(value: 0.0)},
|
312
|
+
map_float: {0 => Google::Protobuf::FloatValue.new(value: 0.0)},
|
313
|
+
map_int32: {0 => Google::Protobuf::Int32Value.new(value: 0)},
|
314
|
+
map_int64: {0 => Google::Protobuf::Int64Value.new(value: 0)},
|
315
|
+
map_uint32: {0 => Google::Protobuf::UInt32Value.new(value: 0)},
|
316
|
+
map_uint64: {0 => Google::Protobuf::UInt64Value.new(value: 0)},
|
317
|
+
map_bool: {0 => Google::Protobuf::BoolValue.new(value: false)},
|
318
|
+
map_string: {0 => Google::Protobuf::StringValue.new(value: '')},
|
319
|
+
map_bytes: {0 => Google::Protobuf::BytesValue.new(value: '')},
|
320
|
+
)
|
321
|
+
|
322
|
+
run_asserts.call(m)
|
323
|
+
serialized = proto_module::Wrapper::encode(m)
|
324
|
+
m2 = proto_module::Wrapper::decode(serialized)
|
325
|
+
run_asserts.call(m2)
|
326
|
+
|
327
|
+
# Test the case where we are serializing directly from the parsed form
|
328
|
+
# (before anything lazy is materialized).
|
329
|
+
m3 = proto_module::Wrapper::decode(serialized)
|
330
|
+
serialized2 = proto_module::Wrapper::encode(m3)
|
331
|
+
m4 = proto_module::Wrapper::decode(serialized2)
|
332
|
+
run_asserts.call(m4)
|
333
|
+
|
334
|
+
# Test that the lazy form compares equal to the expanded form.
|
335
|
+
m5 = proto_module::Wrapper::decode(serialized2)
|
336
|
+
assert_equal m5, m
|
337
|
+
end
|
338
|
+
|
339
|
+
def test_map_wrappers_with_no_value
|
340
|
+
run_asserts = ->(m) {
|
341
|
+
assert_equal 0.0, m.map_double[0].value
|
342
|
+
assert_equal 0.0, m.map_float[0].value
|
343
|
+
assert_equal 0, m.map_int32[0].value
|
344
|
+
assert_equal 0, m.map_int64[0].value
|
345
|
+
assert_equal 0, m.map_uint32[0].value
|
346
|
+
assert_equal 0, m.map_uint64[0].value
|
347
|
+
assert_equal false, m.map_bool[0].value
|
348
|
+
assert_equal '', m.map_string[0].value
|
349
|
+
assert_equal '', m.map_bytes[0].value
|
350
|
+
}
|
351
|
+
|
352
|
+
m = proto_module::Wrapper.new(
|
353
|
+
map_double: {0 => Google::Protobuf::DoubleValue.new()},
|
354
|
+
map_float: {0 => Google::Protobuf::FloatValue.new()},
|
355
|
+
map_int32: {0 => Google::Protobuf::Int32Value.new()},
|
356
|
+
map_int64: {0 => Google::Protobuf::Int64Value.new()},
|
357
|
+
map_uint32: {0 => Google::Protobuf::UInt32Value.new()},
|
358
|
+
map_uint64: {0 => Google::Protobuf::UInt64Value.new()},
|
359
|
+
map_bool: {0 => Google::Protobuf::BoolValue.new()},
|
360
|
+
map_string: {0 => Google::Protobuf::StringValue.new()},
|
361
|
+
map_bytes: {0 => Google::Protobuf::BytesValue.new()},
|
362
|
+
)
|
363
|
+
run_asserts.call(m)
|
364
|
+
|
365
|
+
serialized = proto_module::Wrapper::encode(m)
|
366
|
+
m2 = proto_module::Wrapper::decode(serialized)
|
367
|
+
run_asserts.call(m2)
|
368
|
+
|
369
|
+
# Test the case where we are serializing directly from the parsed form
|
370
|
+
# (before anything lazy is materialized).
|
371
|
+
m3 = proto_module::Wrapper::decode(serialized)
|
372
|
+
serialized2 = proto_module::Wrapper::encode(m3)
|
373
|
+
m4 = proto_module::Wrapper::decode(serialized2)
|
374
|
+
run_asserts.call(m4)
|
375
|
+
end
|
376
|
+
|
221
377
|
def test_concurrent_decoding
|
222
378
|
o = Outer.new
|
223
379
|
o.items[0] = Inner.new
|
@@ -237,7 +393,8 @@ module BasicTest
|
|
237
393
|
m = MapMessage.new(
|
238
394
|
:map_string_int32 => {"a" => 1, "b" => 2},
|
239
395
|
:map_string_msg => {"a" => TestMessage2.new(:foo => 1),
|
240
|
-
"b" => TestMessage2.new(:foo => 2)}
|
396
|
+
"b" => TestMessage2.new(:foo => 2)},
|
397
|
+
:map_string_enum => {"a" => :A, "b" => :B})
|
241
398
|
m2 = MapMessage.decode(MapMessage.encode(m))
|
242
399
|
assert m == m2
|
243
400
|
|
@@ -267,6 +424,14 @@ module BasicTest
|
|
267
424
|
assert_match(/No such field: not_in_message/, e.message)
|
268
425
|
end
|
269
426
|
|
427
|
+
#def test_json_quoted_string
|
428
|
+
# m = TestMessage.decode_json(%q(
|
429
|
+
# "optionalInt64": "1",,
|
430
|
+
# }))
|
431
|
+
# puts(m)
|
432
|
+
# assert_equal 1, m.optional_int32
|
433
|
+
#end
|
434
|
+
|
270
435
|
def test_to_h
|
271
436
|
m = TestMessage.new(:optional_bool => true, :optional_double => -10.100001, :optional_string => 'foo', :repeated_string => ['bar1', 'bar2'], :repeated_msg => [TestMessage2.new(:foo => 100)])
|
272
437
|
expected_result = {
|
@@ -298,10 +463,12 @@ module BasicTest
|
|
298
463
|
m = MapMessage.new(
|
299
464
|
:map_string_int32 => {"a" => 1, "b" => 2},
|
300
465
|
:map_string_msg => {"a" => TestMessage2.new(:foo => 1),
|
301
|
-
"b" => TestMessage2.new(:foo => 2)}
|
466
|
+
"b" => TestMessage2.new(:foo => 2)},
|
467
|
+
:map_string_enum => {"a" => :A, "b" => :B})
|
302
468
|
expected_result = {
|
303
469
|
:map_string_int32 => {"a" => 1, "b" => 2},
|
304
|
-
:map_string_msg => {"a" => {:foo => 1}, "b" => {:foo => 2}}
|
470
|
+
:map_string_msg => {"a" => {:foo => 1}, "b" => {:foo => 2}},
|
471
|
+
:map_string_enum => {"a" => :A, "b" => :B}
|
305
472
|
}
|
306
473
|
assert_equal expected_result, m.to_h
|
307
474
|
end
|
@@ -311,26 +478,26 @@ module BasicTest
|
|
311
478
|
# TODO: Fix JSON in JRuby version.
|
312
479
|
return if RUBY_PLATFORM == "java"
|
313
480
|
m = MapMessage.new(:map_string_int32 => {"a" => 1})
|
314
|
-
expected = {mapStringInt32: {a: 1}, mapStringMsg: {}}
|
315
|
-
expected_preserve = {map_string_int32: {a: 1}, map_string_msg: {}}
|
316
|
-
|
481
|
+
expected = {mapStringInt32: {a: 1}, mapStringMsg: {}, mapStringEnum: {}}
|
482
|
+
expected_preserve = {map_string_int32: {a: 1}, map_string_msg: {}, map_string_enum: {}}
|
483
|
+
assert_equal JSON.parse(MapMessage.encode_json(m), :symbolize_names => true), expected
|
317
484
|
|
318
485
|
json = MapMessage.encode_json(m, :preserve_proto_fieldnames => true)
|
319
|
-
|
486
|
+
assert_equal JSON.parse(json, :symbolize_names => true), expected_preserve
|
320
487
|
|
321
488
|
m2 = MapMessage.decode_json(MapMessage.encode_json(m))
|
322
|
-
|
489
|
+
assert_equal m, m2
|
323
490
|
end
|
324
491
|
|
325
492
|
def test_json_maps_emit_defaults_submsg
|
326
493
|
# TODO: Fix JSON in JRuby version.
|
327
494
|
return if RUBY_PLATFORM == "java"
|
328
495
|
m = MapMessage.new(:map_string_msg => {"a" => TestMessage2.new})
|
329
|
-
expected = {mapStringInt32: {}, mapStringMsg: {a: {foo: 0}}}
|
496
|
+
expected = {mapStringInt32: {}, mapStringMsg: {a: {foo: 0}}, mapStringEnum: {}}
|
330
497
|
|
331
498
|
actual = MapMessage.encode_json(m, :emit_defaults => true)
|
332
499
|
|
333
|
-
|
500
|
+
assert_equal JSON.parse(actual, :symbolize_names => true), expected
|
334
501
|
end
|
335
502
|
|
336
503
|
def test_respond_to
|
@@ -351,11 +518,6 @@ module BasicTest
|
|
351
518
|
assert nil != file_descriptor
|
352
519
|
assert_equal "tests/basic_test.proto", file_descriptor.name
|
353
520
|
assert_equal :proto3, file_descriptor.syntax
|
354
|
-
|
355
|
-
file_descriptor = BadFieldNames.descriptor.file_descriptor
|
356
|
-
assert nil != file_descriptor
|
357
|
-
assert_equal nil, file_descriptor.name
|
358
|
-
assert_equal :proto3, file_descriptor.syntax
|
359
521
|
end
|
360
522
|
|
361
523
|
# Ruby 2.5 changed to raise FrozenError instead of RuntimeError
|