google-protobuf-z 3.5.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/ext/google/protobuf_c/defs.c +2257 -0
- data/ext/google/protobuf_c/encode_decode.c +1453 -0
- data/ext/google/protobuf_c/extconf.rb +17 -0
- data/ext/google/protobuf_c/map.c +849 -0
- data/ext/google/protobuf_c/message.c +734 -0
- data/ext/google/protobuf_c/protobuf.c +121 -0
- data/ext/google/protobuf_c/protobuf.h +611 -0
- data/ext/google/protobuf_c/repeated_field.c +654 -0
- data/ext/google/protobuf_c/storage.c +1019 -0
- data/ext/google/protobuf_c/upb.c +14915 -0
- data/ext/google/protobuf_c/upb.h +8969 -0
- data/ext/google/protobuf_c/wrap_memcpy.c +51 -0
- data/lib/google/protobuf.rb +77 -0
- data/lib/google/protobuf/message_exts.rb +53 -0
- data/lib/google/protobuf/repeated_field.rb +188 -0
- data/lib/google/protobuf/well_known_types.rb +224 -0
- data/tests/basic.rb +348 -0
- data/tests/generated_code_test.rb +21 -0
- data/tests/stress.rb +38 -0
- metadata +128 -0
@@ -0,0 +1,51 @@
|
|
1
|
+
// Protocol Buffers - Google's data interchange format
|
2
|
+
// Copyright 2017 Google Inc. All rights reserved.
|
3
|
+
// https://developers.google.com/protocol-buffers/
|
4
|
+
//
|
5
|
+
// Redistribution and use in source and binary forms, with or without
|
6
|
+
// modification, are permitted provided that the following conditions are
|
7
|
+
// met:
|
8
|
+
//
|
9
|
+
// * Redistributions of source code must retain the above copyright
|
10
|
+
// notice, this list of conditions and the following disclaimer.
|
11
|
+
// * Redistributions in binary form must reproduce the above
|
12
|
+
// copyright notice, this list of conditions and the following disclaimer
|
13
|
+
// in the documentation and/or other materials provided with the
|
14
|
+
// distribution.
|
15
|
+
// * Neither the name of Google Inc. nor the names of its
|
16
|
+
// contributors may be used to endorse or promote products derived from
|
17
|
+
// this software without specific prior written permission.
|
18
|
+
//
|
19
|
+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
20
|
+
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
21
|
+
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
22
|
+
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
23
|
+
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
24
|
+
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
25
|
+
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
26
|
+
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
27
|
+
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
28
|
+
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
29
|
+
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
30
|
+
|
31
|
+
#include <string.h>
|
32
|
+
|
33
|
+
// On x86-64 Linux with glibc, we link against the 2.2.5 version of memcpy so
|
34
|
+
// that we avoid depending on the 2.14 version of the symbol. This way,
|
35
|
+
// distributions that are using pre-2.14 versions of glibc can successfully use
|
36
|
+
// the gem we distribute (https://github.com/protocolbuffers/protobuf/issues/2783).
|
37
|
+
//
|
38
|
+
// This wrapper is enabled by passing the linker flags -Wl,-wrap,memcpy in
|
39
|
+
// extconf.rb.
|
40
|
+
#ifdef __linux__
|
41
|
+
#if defined(__x86_64__) && defined(__GNU_LIBRARY__)
|
42
|
+
__asm__(".symver memcpy,memcpy@GLIBC_2.2.5");
|
43
|
+
void *__wrap_memcpy(void *dest, const void *src, size_t n) {
|
44
|
+
return memcpy(dest, src, n);
|
45
|
+
}
|
46
|
+
#else
|
47
|
+
void *__wrap_memcpy(void *dest, const void *src, size_t n) {
|
48
|
+
return memmove(dest, src, n);
|
49
|
+
}
|
50
|
+
#endif
|
51
|
+
#endif
|
@@ -0,0 +1,77 @@
|
|
1
|
+
# Protocol Buffers - Google's data interchange format
|
2
|
+
# Copyright 2008 Google Inc. All rights reserved.
|
3
|
+
# https://developers.google.com/protocol-buffers/
|
4
|
+
#
|
5
|
+
# Redistribution and use in source and binary forms, with or without
|
6
|
+
# modification, are permitted provided that the following conditions are
|
7
|
+
# met:
|
8
|
+
#
|
9
|
+
# * Redistributions of source code must retain the above copyright
|
10
|
+
# notice, this list of conditions and the following disclaimer.
|
11
|
+
# * Redistributions in binary form must reproduce the above
|
12
|
+
# copyright notice, this list of conditions and the following disclaimer
|
13
|
+
# in the documentation and/or other materials provided with the
|
14
|
+
# distribution.
|
15
|
+
# * Neither the name of Google Inc. nor the names of its
|
16
|
+
# contributors may be used to endorse or promote products derived from
|
17
|
+
# this software without specific prior written permission.
|
18
|
+
#
|
19
|
+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
20
|
+
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
21
|
+
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
22
|
+
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
23
|
+
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
24
|
+
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
25
|
+
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
26
|
+
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
27
|
+
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
28
|
+
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
29
|
+
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
30
|
+
|
31
|
+
# require mixins before we hook them into the java & c code
|
32
|
+
require 'google/protobuf/message_exts'
|
33
|
+
|
34
|
+
# We define these before requiring the platform-specific modules.
|
35
|
+
# That way the module init can grab references to these.
|
36
|
+
module Google
|
37
|
+
module Protobuf
|
38
|
+
class Error < StandardError; end
|
39
|
+
class ParseError < Error; end
|
40
|
+
class TypeError < ::TypeError; end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
if RUBY_PLATFORM == "java"
|
45
|
+
require 'json'
|
46
|
+
require 'google/protobuf_java'
|
47
|
+
else
|
48
|
+
begin
|
49
|
+
require "google/#{RUBY_VERSION.sub(/\.\d+$/, '')}/protobuf_c"
|
50
|
+
rescue LoadError
|
51
|
+
require 'google/protobuf_c'
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
require 'google/protobuf/repeated_field'
|
56
|
+
|
57
|
+
module Google
|
58
|
+
module Protobuf
|
59
|
+
|
60
|
+
def self.encode(msg)
|
61
|
+
msg.to_proto
|
62
|
+
end
|
63
|
+
|
64
|
+
def self.encode_json(msg, options = {})
|
65
|
+
msg.to_json(options)
|
66
|
+
end
|
67
|
+
|
68
|
+
def self.decode(klass, proto)
|
69
|
+
klass.decode(proto)
|
70
|
+
end
|
71
|
+
|
72
|
+
def self.decode_json(klass, json)
|
73
|
+
klass.decode_json(json)
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
77
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
# Protocol Buffers - Google's data interchange format
|
2
|
+
# Copyright 2008 Google Inc. All rights reserved.
|
3
|
+
# https://developers.google.com/protocol-buffers/
|
4
|
+
#
|
5
|
+
# Redistribution and use in source and binary forms, with or without
|
6
|
+
# modification, are permitted provided that the following conditions are
|
7
|
+
# met:
|
8
|
+
#
|
9
|
+
# * Redistributions of source code must retain the above copyright
|
10
|
+
# notice, this list of conditions and the following disclaimer.
|
11
|
+
# * Redistributions in binary form must reproduce the above
|
12
|
+
# copyright notice, this list of conditions and the following disclaimer
|
13
|
+
# in the documentation and/or other materials provided with the
|
14
|
+
# distribution.
|
15
|
+
# * Neither the name of Google Inc. nor the names of its
|
16
|
+
# contributors may be used to endorse or promote products derived from
|
17
|
+
# this software without specific prior written permission.
|
18
|
+
#
|
19
|
+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
20
|
+
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
21
|
+
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
22
|
+
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
23
|
+
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
24
|
+
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
25
|
+
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
26
|
+
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
27
|
+
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
28
|
+
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
29
|
+
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
30
|
+
|
31
|
+
module Google
|
32
|
+
module Protobuf
|
33
|
+
module MessageExts
|
34
|
+
|
35
|
+
#this is only called in jruby; mri loades the ClassMethods differently
|
36
|
+
def self.included(klass)
|
37
|
+
klass.extend(ClassMethods)
|
38
|
+
end
|
39
|
+
|
40
|
+
module ClassMethods
|
41
|
+
end
|
42
|
+
|
43
|
+
def to_json(options = {})
|
44
|
+
self.class.encode_json(self, options)
|
45
|
+
end
|
46
|
+
|
47
|
+
def to_proto
|
48
|
+
self.class.encode(self)
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,188 @@
|
|
1
|
+
# Protocol Buffers - Google's data interchange format
|
2
|
+
# Copyright 2008 Google Inc. All rights reserved.
|
3
|
+
# https://developers.google.com/protocol-buffers/
|
4
|
+
#
|
5
|
+
# Redistribution and use in source and binary forms, with or without
|
6
|
+
# modification, are permitted provided that the following conditions are
|
7
|
+
# met:
|
8
|
+
#
|
9
|
+
# * Redistributions of source code must retain the above copyright
|
10
|
+
# notice, this list of conditions and the following disclaimer.
|
11
|
+
# * Redistributions in binary form must reproduce the above
|
12
|
+
# copyright notice, this list of conditions and the following disclaimer
|
13
|
+
# in the documentation and/or other materials provided with the
|
14
|
+
# distribution.
|
15
|
+
# * Neither the name of Google Inc. nor the names of its
|
16
|
+
# contributors may be used to endorse or promote products derived from
|
17
|
+
# this software without specific prior written permission.
|
18
|
+
#
|
19
|
+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
20
|
+
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
21
|
+
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
22
|
+
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
23
|
+
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
24
|
+
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
25
|
+
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
26
|
+
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
27
|
+
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
28
|
+
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
29
|
+
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
30
|
+
|
31
|
+
require 'forwardable'
|
32
|
+
|
33
|
+
#
|
34
|
+
# This class makes RepeatedField act (almost-) like a Ruby Array.
|
35
|
+
# It has convenience methods that extend the core C or Java based
|
36
|
+
# methods.
|
37
|
+
#
|
38
|
+
# This is a best-effort to mirror Array behavior. Two comments:
|
39
|
+
# 1) patches always welcome :)
|
40
|
+
# 2) if performance is an issue, feel free to rewrite the method
|
41
|
+
# in jruby and C. The source code has plenty of examples
|
42
|
+
#
|
43
|
+
# KNOWN ISSUES
|
44
|
+
# - #[]= doesn't allow less used approaches such as `arr[1, 2] = 'fizz'`
|
45
|
+
# - #concat should return the orig array
|
46
|
+
# - #push should accept multiple arguments and push them all at the same time
|
47
|
+
#
|
48
|
+
module Google
|
49
|
+
module Protobuf
|
50
|
+
class RepeatedField
|
51
|
+
extend Forwardable
|
52
|
+
|
53
|
+
# methods defined in C or Java:
|
54
|
+
# +
|
55
|
+
# [], at
|
56
|
+
# []=
|
57
|
+
# concat
|
58
|
+
# clear
|
59
|
+
# dup, clone
|
60
|
+
# each
|
61
|
+
# push, <<
|
62
|
+
# replace
|
63
|
+
# length, size
|
64
|
+
# ==
|
65
|
+
# to_ary, to_a
|
66
|
+
# also all enumerable
|
67
|
+
#
|
68
|
+
# NOTE: using delegators rather than method_missing to make the
|
69
|
+
# relationship explicit instead of implicit
|
70
|
+
def_delegators :to_ary,
|
71
|
+
:&, :*, :-, :'<=>',
|
72
|
+
:assoc, :bsearch, :bsearch_index, :combination, :compact, :count,
|
73
|
+
:cycle, :dig, :drop, :drop_while, :eql?, :fetch, :find_index, :flatten,
|
74
|
+
:include?, :index, :inspect, :join,
|
75
|
+
:pack, :permutation, :product, :pretty_print, :pretty_print_cycle,
|
76
|
+
:rassoc, :repeated_combination, :repeated_permutation, :reverse,
|
77
|
+
:rindex, :rotate, :sample, :shuffle, :shelljoin,
|
78
|
+
:to_s, :transpose, :uniq, :|
|
79
|
+
|
80
|
+
|
81
|
+
def first(n=nil)
|
82
|
+
n ? self[0...n] : self[0]
|
83
|
+
end
|
84
|
+
|
85
|
+
|
86
|
+
def last(n=nil)
|
87
|
+
n ? self[(self.size-n-1)..-1] : self[-1]
|
88
|
+
end
|
89
|
+
|
90
|
+
|
91
|
+
def pop(n=nil)
|
92
|
+
if n
|
93
|
+
results = []
|
94
|
+
n.times{ results << pop_one }
|
95
|
+
return results
|
96
|
+
else
|
97
|
+
return pop_one
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
|
102
|
+
def empty?
|
103
|
+
self.size == 0
|
104
|
+
end
|
105
|
+
|
106
|
+
# array aliases into enumerable
|
107
|
+
alias_method :each_index, :each_with_index
|
108
|
+
alias_method :slice, :[]
|
109
|
+
alias_method :values_at, :select
|
110
|
+
alias_method :map, :collect
|
111
|
+
|
112
|
+
|
113
|
+
class << self
|
114
|
+
def define_array_wrapper_method(method_name)
|
115
|
+
define_method(method_name) do |*args, &block|
|
116
|
+
arr = self.to_a
|
117
|
+
result = arr.send(method_name, *args)
|
118
|
+
self.replace(arr)
|
119
|
+
return result if result
|
120
|
+
return block ? block.call : result
|
121
|
+
end
|
122
|
+
end
|
123
|
+
private :define_array_wrapper_method
|
124
|
+
|
125
|
+
|
126
|
+
def define_array_wrapper_with_result_method(method_name)
|
127
|
+
define_method(method_name) do |*args, &block|
|
128
|
+
# result can be an Enumerator, Array, or nil
|
129
|
+
# Enumerator can sometimes be returned if a block is an optional argument and it is not passed in
|
130
|
+
# nil usually specifies that no change was made
|
131
|
+
result = self.to_a.send(method_name, *args, &block)
|
132
|
+
if result
|
133
|
+
new_arr = result.to_a
|
134
|
+
self.replace(new_arr)
|
135
|
+
if result.is_a?(Enumerator)
|
136
|
+
# generate a fresh enum; rewinding the exiting one, in Ruby 2.2, will
|
137
|
+
# reset the enum with the same length, but all the #next calls will
|
138
|
+
# return nil
|
139
|
+
result = new_arr.to_enum
|
140
|
+
# generate a wrapper enum so any changes which occur by a chained
|
141
|
+
# enum can be captured
|
142
|
+
ie = ProxyingEnumerator.new(self, result)
|
143
|
+
result = ie.to_enum
|
144
|
+
end
|
145
|
+
end
|
146
|
+
result
|
147
|
+
end
|
148
|
+
end
|
149
|
+
private :define_array_wrapper_with_result_method
|
150
|
+
end
|
151
|
+
|
152
|
+
|
153
|
+
%w(delete delete_at shift slice! unshift).each do |method_name|
|
154
|
+
define_array_wrapper_method(method_name)
|
155
|
+
end
|
156
|
+
|
157
|
+
|
158
|
+
%w(collect! compact! delete_if fill flatten! insert reverse!
|
159
|
+
rotate! select! shuffle! sort! sort_by! uniq!).each do |method_name|
|
160
|
+
define_array_wrapper_with_result_method(method_name)
|
161
|
+
end
|
162
|
+
alias_method :keep_if, :select!
|
163
|
+
alias_method :map!, :collect!
|
164
|
+
alias_method :reject!, :delete_if
|
165
|
+
|
166
|
+
|
167
|
+
# propagates changes made by user of enumerator back to the original repeated field.
|
168
|
+
# This only applies in cases where the calling function which created the enumerator,
|
169
|
+
# such as #sort!, modifies itself rather than a new array, such as #sort
|
170
|
+
class ProxyingEnumerator < Struct.new(:repeated_field, :external_enumerator)
|
171
|
+
def each(*args, &block)
|
172
|
+
results = []
|
173
|
+
external_enumerator.each_with_index do |val, i|
|
174
|
+
result = yield(val)
|
175
|
+
results << result
|
176
|
+
#nil means no change occurred from yield; usually occurs when #to_a is called
|
177
|
+
if result
|
178
|
+
repeated_field[i] = result if result != val
|
179
|
+
end
|
180
|
+
end
|
181
|
+
results
|
182
|
+
end
|
183
|
+
end
|
184
|
+
|
185
|
+
|
186
|
+
end
|
187
|
+
end
|
188
|
+
end
|
@@ -0,0 +1,224 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
# Protocol Buffers - Google's data interchange format
|
3
|
+
# Copyright 2008 Google Inc. All rights reserved.
|
4
|
+
# https://developers.google.com/protocol-buffers/
|
5
|
+
#
|
6
|
+
# Redistribution and use in source and binary forms, with or without
|
7
|
+
# modification, are permitted provided that the following conditions are
|
8
|
+
# met:
|
9
|
+
#
|
10
|
+
# * Redistributions of source code must retain the above copyright
|
11
|
+
# notice, this list of conditions and the following disclaimer.
|
12
|
+
# * Redistributions in binary form must reproduce the above
|
13
|
+
# copyright notice, this list of conditions and the following disclaimer
|
14
|
+
# in the documentation and/or other materials provided with the
|
15
|
+
# distribution.
|
16
|
+
# * Neither the name of Google Inc. nor the names of its
|
17
|
+
# contributors may be used to endorse or promote products derived from
|
18
|
+
# this software without specific prior written permission.
|
19
|
+
#
|
20
|
+
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
21
|
+
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
22
|
+
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
23
|
+
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
24
|
+
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
25
|
+
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
26
|
+
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
27
|
+
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
28
|
+
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
29
|
+
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
30
|
+
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
31
|
+
|
32
|
+
require 'google/protobuf/any_pb'
|
33
|
+
require 'google/protobuf/duration_pb'
|
34
|
+
require 'google/protobuf/field_mask_pb'
|
35
|
+
require 'google/protobuf/struct_pb'
|
36
|
+
require 'google/protobuf/timestamp_pb'
|
37
|
+
|
38
|
+
module Google
|
39
|
+
module Protobuf
|
40
|
+
|
41
|
+
Any.class_eval do
|
42
|
+
def self.pack(msg, type_url_prefix='type.googleapis.com/')
|
43
|
+
any = self.new
|
44
|
+
any.pack(msg, type_url_prefix)
|
45
|
+
any
|
46
|
+
end
|
47
|
+
|
48
|
+
def pack(msg, type_url_prefix='type.googleapis.com/')
|
49
|
+
if type_url_prefix.empty? or type_url_prefix[-1] != '/' then
|
50
|
+
self.type_url = "#{type_url_prefix}/#{msg.class.descriptor.name}"
|
51
|
+
else
|
52
|
+
self.type_url = "#{type_url_prefix}#{msg.class.descriptor.name}"
|
53
|
+
end
|
54
|
+
self.value = msg.to_proto
|
55
|
+
end
|
56
|
+
|
57
|
+
def unpack(klass)
|
58
|
+
if self.is(klass) then
|
59
|
+
klass.decode(self.value)
|
60
|
+
else
|
61
|
+
nil
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def type_name
|
66
|
+
return self.type_url.split("/")[-1]
|
67
|
+
end
|
68
|
+
|
69
|
+
def is(klass)
|
70
|
+
return self.type_name == klass.descriptor.name
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
Timestamp.class_eval do
|
75
|
+
def to_time
|
76
|
+
Time.at(self.to_f)
|
77
|
+
end
|
78
|
+
|
79
|
+
def from_time(time)
|
80
|
+
self.seconds = time.to_i
|
81
|
+
self.nanos = time.nsec
|
82
|
+
end
|
83
|
+
|
84
|
+
def to_i
|
85
|
+
self.seconds
|
86
|
+
end
|
87
|
+
|
88
|
+
def to_f
|
89
|
+
self.seconds + (self.nanos.quo(1_000_000_000))
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
Duration.class_eval do
|
94
|
+
def to_f
|
95
|
+
self.seconds + (self.nanos.to_f / 1_000_000_000)
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
class UnexpectedStructType < Google::Protobuf::Error; end
|
100
|
+
|
101
|
+
Value.class_eval do
|
102
|
+
def to_ruby(recursive = false)
|
103
|
+
case self.kind
|
104
|
+
when :struct_value
|
105
|
+
if recursive
|
106
|
+
self.struct_value.to_h
|
107
|
+
else
|
108
|
+
self.struct_value
|
109
|
+
end
|
110
|
+
when :list_value
|
111
|
+
if recursive
|
112
|
+
self.list_value.to_a
|
113
|
+
else
|
114
|
+
self.list_value
|
115
|
+
end
|
116
|
+
when :null_value
|
117
|
+
nil
|
118
|
+
when :number_value
|
119
|
+
self.number_value
|
120
|
+
when :string_value
|
121
|
+
self.string_value
|
122
|
+
when :bool_value
|
123
|
+
self.bool_value
|
124
|
+
else
|
125
|
+
raise UnexpectedStructType
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
def from_ruby(value)
|
130
|
+
case value
|
131
|
+
when NilClass
|
132
|
+
self.null_value = 0
|
133
|
+
when Numeric
|
134
|
+
self.number_value = value
|
135
|
+
when String
|
136
|
+
self.string_value = value
|
137
|
+
when TrueClass
|
138
|
+
self.bool_value = true
|
139
|
+
when FalseClass
|
140
|
+
self.bool_value = false
|
141
|
+
when Struct
|
142
|
+
self.struct_value = value
|
143
|
+
when Hash
|
144
|
+
self.struct_value = Struct.from_hash(value)
|
145
|
+
when ListValue
|
146
|
+
self.list_value = value
|
147
|
+
when Array
|
148
|
+
self.list_value = ListValue.from_a(value)
|
149
|
+
else
|
150
|
+
raise UnexpectedStructType
|
151
|
+
end
|
152
|
+
end
|
153
|
+
end
|
154
|
+
|
155
|
+
Struct.class_eval do
|
156
|
+
def [](key)
|
157
|
+
self.fields[key].to_ruby
|
158
|
+
rescue NoMethodError
|
159
|
+
nil
|
160
|
+
end
|
161
|
+
|
162
|
+
def []=(key, value)
|
163
|
+
unless key.is_a?(String)
|
164
|
+
raise UnexpectedStructType, "Struct keys must be strings."
|
165
|
+
end
|
166
|
+
self.fields[key] ||= Google::Protobuf::Value.new
|
167
|
+
self.fields[key].from_ruby(value)
|
168
|
+
end
|
169
|
+
|
170
|
+
def to_h
|
171
|
+
ret = {}
|
172
|
+
self.fields.each { |key, val| ret[key] = val.to_ruby(true) }
|
173
|
+
ret
|
174
|
+
end
|
175
|
+
|
176
|
+
def self.from_hash(hash)
|
177
|
+
ret = Struct.new
|
178
|
+
hash.each { |key, val| ret[key] = val }
|
179
|
+
ret
|
180
|
+
end
|
181
|
+
|
182
|
+
def has_key?(key)
|
183
|
+
self.fields.has_key?(key)
|
184
|
+
end
|
185
|
+
end
|
186
|
+
|
187
|
+
ListValue.class_eval do
|
188
|
+
include Enumerable
|
189
|
+
|
190
|
+
def length
|
191
|
+
self.values.length
|
192
|
+
end
|
193
|
+
|
194
|
+
def [](index)
|
195
|
+
self.values[index].to_ruby
|
196
|
+
end
|
197
|
+
|
198
|
+
def []=(index, value)
|
199
|
+
self.values[index].from_ruby(value)
|
200
|
+
end
|
201
|
+
|
202
|
+
def <<(value)
|
203
|
+
wrapper = Google::Protobuf::Value.new
|
204
|
+
wrapper.from_ruby(value)
|
205
|
+
self.values << wrapper
|
206
|
+
end
|
207
|
+
|
208
|
+
def each
|
209
|
+
self.values.each { |x| yield(x.to_ruby) }
|
210
|
+
end
|
211
|
+
|
212
|
+
def to_a
|
213
|
+
self.values.map { |x| x.to_ruby(true) }
|
214
|
+
end
|
215
|
+
|
216
|
+
def self.from_a(arr)
|
217
|
+
ret = ListValue.new
|
218
|
+
arr.each { |val| ret << val }
|
219
|
+
ret
|
220
|
+
end
|
221
|
+
end
|
222
|
+
|
223
|
+
end
|
224
|
+
end
|