Add new mappers
This commit is contained in:
parent
908135c21e
commit
eb5969dac0
44
plugins/commands/serve/mappers/direct.rb
Normal file
44
plugins/commands/serve/mappers/direct.rb
Normal file
@ -0,0 +1,44 @@
|
||||
require "google/protobuf/well_known_types"
|
||||
require "google/protobuf/wrappers_pb"
|
||||
|
||||
module VagrantPlugins
|
||||
module CommandServe
|
||||
class Mappers
|
||||
class Direct < Mapper
|
||||
def initialize
|
||||
inputs = [].tap do |i|
|
||||
i << Input.new(type: SDK::FuncSpec::Value) { |arg|
|
||||
arg.type == "hashicorp.vagrant.sdk.Args.Direct" &&
|
||||
!arg&.value&.value.nil?
|
||||
}
|
||||
i << Input.new(type: Mappers)
|
||||
end
|
||||
super(inputs: inputs, output: Array, func: method(:converter))
|
||||
end
|
||||
|
||||
def converter(proto, mappers)
|
||||
SDK::Args::Direct.decode(proto.value.value).list.map do |v|
|
||||
namespace = v.type_name.split(".")
|
||||
klass_name = namespace.pop
|
||||
|
||||
ns = namespace.inject(Object) { |memo, n|
|
||||
memo.const_get(n.split("_").map(&:capitalize).join.to_sym) if memo
|
||||
}
|
||||
klass = ns.const_get(klass_name) if ns
|
||||
v = v.unpack(klass) if klass
|
||||
|
||||
new_v = true
|
||||
while new_v
|
||||
begin
|
||||
v = mappers.map(v)
|
||||
rescue
|
||||
new_v = false
|
||||
end
|
||||
end
|
||||
v
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
20
plugins/commands/serve/mappers/known_types.rb
Normal file
20
plugins/commands/serve/mappers/known_types.rb
Normal file
@ -0,0 +1,20 @@
|
||||
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
|
||||
end
|
||||
end
|
||||
end
|
||||
36
plugins/commands/serve/mappers/wrappers.rb
Normal file
36
plugins/commands/serve/mappers/wrappers.rb
Normal file
@ -0,0 +1,36 @@
|
||||
require "google/protobuf/wrappers_pb"
|
||||
|
||||
module VagrantPlugins
|
||||
module CommandServe
|
||||
class Mappers
|
||||
Google::Protobuf.constants.grep(/.Value$/).each do |name|
|
||||
value = Google::Protobuf.const_get(name)
|
||||
next if !value.is_a?(Class)
|
||||
type = value.new.respond_to?(:value) ?
|
||||
value.new.value.class :
|
||||
value.new.values.class
|
||||
klass = Class.new(Mapper).class_eval "
|
||||
def initialize
|
||||
super(
|
||||
inputs: [Input.new(type:#{value.name})],
|
||||
output: #{type.name},
|
||||
func: method(:converter)
|
||||
)
|
||||
end
|
||||
|
||||
def self.name
|
||||
\"#{name}\"
|
||||
end
|
||||
|
||||
def converter(proto)
|
||||
proto.value
|
||||
end
|
||||
"
|
||||
self.const_set(
|
||||
name,
|
||||
klass
|
||||
)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
x
Reference in New Issue
Block a user