Use custom Array and Hash mapping
This commit is contained in:
parent
0e8ded18aa
commit
74d6dafccf
@ -3,21 +3,8 @@ require "google/protobuf/well_known_types"
|
||||
module VagrantPlugins
|
||||
module CommandServe
|
||||
class Mappers
|
||||
# class KnownTypes < Mapper
|
||||
# def initialize
|
||||
# inputs = [].tap do |i|
|
||||
# i << Input.new(type: Google::Protobuf::Value)
|
||||
# end
|
||||
# super(inputs: inputs, output: Object, func: method(:converter))
|
||||
# end
|
||||
|
||||
# def converter(proto)
|
||||
# proto.to_ruby
|
||||
# end
|
||||
# end
|
||||
|
||||
[NilClass, Numeric, String, TrueClass, FalseClass,
|
||||
Struct, Hash, Google::Protobuf::ListValue, Array].each do |type|
|
||||
Struct, Google::Protobuf::ListValue].each do |type|
|
||||
Class.new(Mapper).class_eval("
|
||||
def self.name
|
||||
'#{type.name}ToProto'
|
||||
@ -40,6 +27,90 @@ module VagrantPlugins
|
||||
end
|
||||
")
|
||||
end
|
||||
|
||||
class HashToProto < Mapper
|
||||
def initialize
|
||||
super(
|
||||
inputs: [
|
||||
Input.new(type: Hash),
|
||||
Input.new(type: Mappers),
|
||||
],
|
||||
output: SDK::Args::Hash,
|
||||
func: method(:converter),
|
||||
)
|
||||
end
|
||||
|
||||
def converter(hash, mapper)
|
||||
fields = Hash.new.tap do |f|
|
||||
hash.each_pair do |k, v|
|
||||
r = mapper.map(v, to: Google::Protobuf::Any)
|
||||
f[k] = r
|
||||
end
|
||||
end
|
||||
SDK::Args::Hash.new(fields: fields)
|
||||
end
|
||||
end
|
||||
|
||||
class HashFromProto < Mapper
|
||||
def initialize
|
||||
super(
|
||||
inputs: [
|
||||
Input.new(type: SDK::Args::Hash),
|
||||
Input.new(type: Mappers)
|
||||
],
|
||||
output: Hash,
|
||||
func: method(:converter),
|
||||
)
|
||||
end
|
||||
|
||||
def converter(proto, mapper)
|
||||
Hash.new.tap do |result|
|
||||
proto.fields.each_pair do |k, v|
|
||||
r = mapper.map(v)
|
||||
result[k.to_s] = r
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
class ArrayToProto < Mapper
|
||||
def initialize
|
||||
super(
|
||||
inputs: [
|
||||
Input.new(type: Array),
|
||||
Input.new(type: Mappers),
|
||||
],
|
||||
output: SDK::Args::Array,
|
||||
func: method(:converter),
|
||||
)
|
||||
end
|
||||
|
||||
def converter(array, mapper)
|
||||
r = array.map do |v|
|
||||
mapper.map(v, to: Google::Protobuf::Any)
|
||||
end
|
||||
SDK::Args::Array.new(list: r)
|
||||
end
|
||||
end
|
||||
|
||||
class ArrayFromProto < Mapper
|
||||
def initialize
|
||||
super(
|
||||
inputs: [
|
||||
Input.new(type: SDK::Args::Array),
|
||||
Input.new(type: Mappers),
|
||||
],
|
||||
output: Array,
|
||||
func: method(:converter),
|
||||
)
|
||||
end
|
||||
|
||||
def converter(proto, mapper)
|
||||
proto.list.map do |v|
|
||||
mapper.map(v)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user