9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
# File 'lib/krpc/encoder.rb', line 9
def encode(obj, type)
if type.is_a?(Types::MessageType) then type.ruby_type.encode(obj)
elsif type.is_a?(Types::ValueType) then encode_value(obj, type)
elsif type.is_a?(Types::EnumType)
enum_value = type.ruby_type[obj]
encode_value(enum_value, 'sint32')
elsif type.is_a?(Types::ClassType)
remote_oid = obj.nil? ? 0 : obj.remote_oid
encode_value(remote_oid, 'uint64')
elsif type.is_a?(Types::ListType)
PB::List.encode(PB::List.new(
items: obj.map{|x| encode(TypeStore.coerce_to(x, type.value_type), type.value_type)}.to_a
))
elsif type.is_a?(Types::DictionaryType)
entries = obj.map do |k,v|
PB::DictionaryEntry.new(
key: encode(TypeStore.coerce_to(k, type.key_type), type.key_type),
value: encode(TypeStore.coerce_to(v, type.value_type), type.value_type)
)
end
PB::Dictionary.encode(PB::Dictionary.new(entries: entries))
elsif type.is_a?(Types::SetType)
PB::Set.encode(PB::Set.new(
items: obj.map{|x| encode( TypeStore.coerce_to(x, type.value_type), type.value_type )}.to_a
))
elsif type.is_a?(Types::TupleType)
PB::Tuple.encode(PB::Tuple.new(
items: obj.zip(type.value_types).map{|x,t| encode( TypeStore.coerce_to(x, t), t )}.to_a
))
else raise(RuntimeError, "Cannot encode object #{obj} of type #{type}")
end
end
|