Add new mappers

This commit is contained in:
Chris Roberts 2021-10-06 08:17:26 -07:00 committed by Paul Hinze
parent 908135c21e
commit eb5969dac0
No known key found for this signature in database
GPG Key ID: B69DEDF2D55501C0
3 changed files with 100 additions and 0 deletions

View 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

View 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

View 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