Class: Arf::RPC::Struct
Constant Summary
Types::Mixin::ArrayType, Types::Mixin::InOutStream, Types::Mixin::InputStream, Types::Mixin::MapType, Types::Mixin::OutputStream, Types::Mixin::Streamer
Class Method Summary
collapse
Instance Method Summary
collapse
included
Constructor Details
#initialize(**kwargs) ⇒ Struct
Returns a new instance of Struct.
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
|
# File 'lib/arf/rpc/struct.rb', line 134
def initialize(**kwargs)
cls = self.class
kwargs.each_pair do |k, v|
field = cls.field_by_name(k)
raise ArgumentError, "#{cls.name}: Unknown field #{k}" unless field
v = _coerce(v, field[:type])
send("#{k}=", v)
end
@skip_union_setter = true
cls.fields.each do |f|
next if f[:optional]
send("#{f[:name]}=", cls.default_for_type(f[:type])) if instance_variable_get("@#{f[:name]}").nil?
end
if union? && __arf_union_set_id.nil?
@skip_union_setter = false
f = cls.fields.first
send("#{f[:name]}=", cls.default_for_type(f[:type]))
end
ensure
@skip_union_setter = false
end
|
Class Method Details
.all_fields ⇒ Object
108
109
110
111
112
113
114
115
116
117
118
|
# File 'lib/arf/rpc/struct.rb', line 108
def self.all_fields
@all_fields ||= @fields
.map do |v|
next v unless v[:id] == :union
find_type(v[:type])
.all_fields
.map { _1.merge(via: v[:name]) }
end
.flatten
end
|
.arf_struct_id(v = nil) ⇒ Object
8
9
10
11
12
13
14
|
# File 'lib/arf/rpc/struct.rb', line 8
def self.arf_struct_id(v = nil)
return @arf_struct_id if v.nil?
@arf_struct_id = v
Arf::Proto::Registry.register! self
v
end
|
.default_for_type(type) ⇒ Object
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
# File 'lib/arf/rpc/struct.rb', line 58
def self.default_for_type(type)
if ::Arf::Types::INTEGERS.include?(type)
0
elsif ::Arf::Types::FLOATS.include?(type)
0.0
elsif type == :string
""
elsif type == :bool
false
elsif type == :bytes
""
elsif type.is_a? MapType
{}
elsif type.is_a? ArrayType
[]
elsif type.is_a? String
cls = find_type(type)
if cls.ancestors.include?(Arf::RPC::Enum)
default_for_type(cls)
else
cls.new
end
elsif type.ancestors.include?(Arf::RPC::Enum)
type.options.keys.first
else
raise ArgumentError, "Invalid type #{type.inspect} (#{type.class.name})"
end
end
|
.field(id, name, type, optional: false) ⇒ Object
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
# File 'lib/arf/rpc/struct.rb', line 90
def self.field(id, name, type, optional: false)
@fields ||= []
@fields << { id:, name: name.to_s, type:, optional: }
attr_reader(name)
validator = validator_for_type(type, optional:)
define_method("#{name}=") do |v|
unless validator.call(v)
raise ArgumentError, "Invalid value for #{self.class.name}.#{name}, type #{type}: #{v.inspect}"
end
v = _coerce(v, type)
instance_variable_set("@__arf_union_set_id", id) if union? && !@skip_union_setter
instance_variable_set("@#{name}", v)
end
end
|
.field_by_id(id) ⇒ Object
121
|
# File 'lib/arf/rpc/struct.rb', line 121
def self.field_by_id(id) = @fields.find { _1[:id] == id }
|
.field_by_name(name) ⇒ Object
122
|
# File 'lib/arf/rpc/struct.rb', line 122
def self.field_by_name(name) = name.to_s.then { |n| @fields.find { _1[:name] == n } }
|
.fields ⇒ Object
120
|
# File 'lib/arf/rpc/struct.rb', line 120
def self.fields = @fields || []
|
.union! ⇒ Object
124
125
126
127
|
# File 'lib/arf/rpc/struct.rb', line 124
def self.union!
@union = true
attr_reader(:__arf_union_set_id)
end
|
.union? ⇒ Boolean
129
|
# File 'lib/arf/rpc/struct.rb', line 129
def self.union? = @union || false
|
.validate_subclass(type, v) ⇒ Object
16
17
18
19
20
21
22
23
|
# File 'lib/arf/rpc/struct.rb', line 16
def self.validate_subclass(type, v)
cls = find_type(type)
if cls.ancestors.include?(Arf::RPC::Enum)
!cls.to_i(v).nil?
else
v.is_a?(cls) || v.is_a?(Hash)
end
end
|
.validator_for_type(type, optional:) ⇒ Object
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
|
# File 'lib/arf/rpc/struct.rb', line 25
def self.validator_for_type(type, optional:)
validator = if ::Arf::Types::INTEGERS.include?(type)
->(v) { v.is_a?(Float) || v.is_a?(Integer) }
elsif ::Arf::Types::FLOATS.include?(type)
->(v) { v.is_a?(Float) || v.is_a?(Integer) }
elsif type == :string
->(v) { v.is_a?(String) }
elsif type == :bool
->(v) { v.is_a?(TrueClass) || v.is_a?(FalseClass) }
elsif type == :bytes
->(v) { v.is_a?(StringIO) || v.is_a?(String) }
elsif type.is_a? MapType
->(v) { v.is_a? Hash }
elsif type.is_a? ArrayType
->(v) { v.is_a? Array }
elsif type.is_a? String
->(v) { validate_subclass(type, v) }
elsif type.ancestors.include?(Arf::RPC::Enum)
->(v) { !type.to_i(v).nil? }
else
raise ArgumentError, "Invalid type #{type.inspect} (#{type.class.name})"
end
if optional
->(v) { v.nil? || validator.call(v) }
else
validator
end
end
|
Instance Method Details
#==(other) ⇒ Object
208
209
210
211
212
213
214
|
# File 'lib/arf/rpc/struct.rb', line 208
def ==(other)
return false unless other.is_a? self.class
return eq_union(other) if union?
eq_struct(other)
end
|
#_coerce(value, type) ⇒ Object
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
|
# File 'lib/arf/rpc/struct.rb', line 162
def _coerce(value, type)
cls = self.class
case type
when String
type = cls.find_type(type)
if type.ancestors.include?(Arf::RPC::Enum)
type.to_sym(value)
else
case value
when Hash then type.new(**value)
when type then value
else
raise ArgumentError,
"Cannot initialize #{type} with #{value.inspect} (#{value.class}). Did you mean to use a Hash?"
end
end
when ArrayType
type.coerce(value)
when MapType
type.coerce(value)
else
::Arf::Types.coerce_value(value, type)
end
end
|
#arf_struct_id ⇒ Object
131
|
# File 'lib/arf/rpc/struct.rb', line 131
def arf_struct_id = self.class.arf_struct_id
|
#decode_fields(fields) ⇒ Object
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
|
# File 'lib/arf/rpc/struct.rb', line 187
def decode_fields(fields)
fields.each_pair do |id, value|
meta = self.class.all_fields.find { _1[:id] == id }
unless meta
puts "WARNING: Skipping field ID #{id} without matching ID on target type #{self.class.name}"
next
end
if meta[:via]
union_field = self.class.all_fields.find { _1[:id] == value[:id] }
instance_variable_get("@#{meta[:via]}")
.send("#{union_field[:name]}=", value[:value])
next
end
next if meta[:optional] && value.nil?
instance_variable_set("@#{meta[:name]}", _coerce(value, meta[:type]))
end
self
end
|
#eq_struct(other) ⇒ Object
216
217
218
219
220
221
|
# File 'lib/arf/rpc/struct.rb', line 216
def eq_struct(other)
self.class.fields.each do |v|
return false unless send(v[:name]) == other.send(v[:name])
end
true
end
|
#eq_union(other) ⇒ Object
223
224
225
226
227
228
229
230
231
|
# File 'lib/arf/rpc/struct.rb', line 223
def eq_union(other)
return false unless other.__arf_union_set_id == __arf_union_set_id
self.class.fields.each do |v|
return false if instance_variable_get("@#{v[:name]}") != other.instance_variable_get("@#{v[:name]}")
end
true
end
|
#hashify(value) ⇒ Object
233
234
235
236
237
238
239
240
241
|
# File 'lib/arf/rpc/struct.rb', line 233
def hashify(value)
case value
when nil then nil
when Arf::RPC::Struct then value.to_h
when Array then value.map { hashify(_1) }
when Hash then value.transform_values { |v| hashify(v) }
else value
end
end
|
#to_h ⇒ Object
243
244
245
246
247
248
249
250
251
252
|
# File 'lib/arf/rpc/struct.rb', line 243
def to_h
if union?
set = self.class.fields.find { _1[:id] == __arf_union_set_id }
return { set[:name] => hashify(instance_variable_get("@#{set[:name]}")) }
end
self.class.fields
.to_h { |f| [f[:name], hashify(instance_variable_get("@#{f[:name]}"))] }
.compact
end
|
#union? ⇒ Boolean
132
|
# File 'lib/arf/rpc/struct.rb', line 132
def union? = self.class.union?
|