OLD | NEW |
| (Empty) |
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 pack(msg, type_url_prefix='type.googleapis.com/') | |
43 if type_url_prefix.empty? or type_url_prefix[-1] != '/' then | |
44 self.type_url = "#{type_url_prefix}/#{msg.class.descriptor.name}" | |
45 else | |
46 self.type_url = "#{type_url_prefix}#{msg.class.descriptor.name}" | |
47 end | |
48 self.value = msg.to_proto | |
49 end | |
50 | |
51 def unpack(klass) | |
52 if self.is(klass) then | |
53 klass.decode(self.value) | |
54 else | |
55 nil | |
56 end | |
57 end | |
58 | |
59 def type_name | |
60 return self.type_url.split("/")[-1] | |
61 end | |
62 | |
63 def is(klass) | |
64 return self.type_name == klass.descriptor.name | |
65 end | |
66 end | |
67 | |
68 Timestamp.class_eval do | |
69 def to_time | |
70 Time.at(self.to_f) | |
71 end | |
72 | |
73 def from_time(time) | |
74 self.seconds = time.to_i | |
75 self.nanos = time.nsec | |
76 end | |
77 | |
78 def to_i | |
79 self.seconds | |
80 end | |
81 | |
82 def to_f | |
83 self.seconds + (self.nanos.to_f / 1_000_000_000) | |
84 end | |
85 end | |
86 | |
87 Duration.class_eval do | |
88 def to_f | |
89 self.seconds + (self.nanos.to_f / 1_000_000_000) | |
90 end | |
91 end | |
92 | |
93 class UnexpectedStructType < Google::Protobuf::Error; end | |
94 | |
95 Value.class_eval do | |
96 def to_ruby(recursive = false) | |
97 case self.kind | |
98 when :struct_value | |
99 if recursive | |
100 self.struct_value.to_h | |
101 else | |
102 self.struct_value | |
103 end | |
104 when :list_value | |
105 if recursive | |
106 self.list_value.to_a | |
107 else | |
108 self.list_value | |
109 end | |
110 when :null_value | |
111 nil | |
112 when :number_value | |
113 self.number_value | |
114 when :string_value | |
115 self.string_value | |
116 when :bool_value | |
117 self.bool_value | |
118 else | |
119 raise UnexpectedStructType | |
120 end | |
121 end | |
122 | |
123 def from_ruby(value) | |
124 case value | |
125 when NilClass | |
126 self.null_value = 0 | |
127 when Numeric | |
128 self.number_value = value | |
129 when String | |
130 self.string_value = value | |
131 when TrueClass | |
132 self.bool_value = true | |
133 when FalseClass | |
134 self.bool_value = false | |
135 when Struct | |
136 self.struct_value = value | |
137 when Hash | |
138 self.struct_value = Struct.from_hash(value) | |
139 when ListValue | |
140 self.list_value = value | |
141 when Array | |
142 self.list_value = ListValue.from_a(value) | |
143 else | |
144 raise UnexpectedStructType | |
145 end | |
146 end | |
147 end | |
148 | |
149 Struct.class_eval do | |
150 def [](key) | |
151 self.fields[key].to_ruby | |
152 end | |
153 | |
154 def []=(key, value) | |
155 unless key.is_a?(String) | |
156 raise UnexpectedStructType, "Struct keys must be strings." | |
157 end | |
158 self.fields[key] ||= Google::Protobuf::Value.new | |
159 self.fields[key].from_ruby(value) | |
160 end | |
161 | |
162 def to_h | |
163 ret = {} | |
164 self.fields.each { |key, val| ret[key] = val.to_ruby(true) } | |
165 ret | |
166 end | |
167 | |
168 def self.from_hash(hash) | |
169 ret = Struct.new | |
170 hash.each { |key, val| ret[key] = val } | |
171 ret | |
172 end | |
173 end | |
174 | |
175 ListValue.class_eval do | |
176 include Enumerable | |
177 | |
178 def length | |
179 self.values.length | |
180 end | |
181 | |
182 def [](index) | |
183 self.values[index].to_ruby | |
184 end | |
185 | |
186 def []=(index, value) | |
187 self.values[index].from_ruby(value) | |
188 end | |
189 | |
190 def <<(value) | |
191 wrapper = Google::Protobuf::Value.new | |
192 wrapper.from_ruby(value) | |
193 self.values << wrapper | |
194 end | |
195 | |
196 def each | |
197 self.values.each { |x| yield(x.to_ruby) } | |
198 end | |
199 | |
200 def to_a | |
201 self.values.map { |x| x.to_ruby(true) } | |
202 end | |
203 | |
204 def self.from_a(arr) | |
205 ret = ListValue.new | |
206 arr.each { |val| ret << val } | |
207 ret | |
208 end | |
209 end | |
210 | |
211 end | |
212 end | |
OLD | NEW |