Rely on proto mappers to convert configs

This commit is contained in:
sophia 2022-04-05 09:52:52 -05:00 committed by Paul Hinze
parent 436518e445
commit 9bf59a0d42
No known key found for this signature in database
GPG Key ID: B69DEDF2D55501C0

View File

@ -198,55 +198,9 @@ module Vagrant
protoize
end
def build_proto_array(a)
out = Hashicorp::Vagrant::Sdk::Args::Array.new
a.each do |e|
if e.is_a?(Hash)
out.list << Google::Protobuf::Any.pack(build_proto_hash(e))
next
end
if e.is_a?(Array)
out.list << Google::Protobuf::Any.pack(build_proto_array(e))
next
end
if e.class.ancestors.include?(Google::Protobuf::MessageExts)
out.list << Google::Protobuf::Any.pack(e)
else
val = Google::Protobuf::Value.new
val.from_ruby(e)
out.list << Google::Protobuf::Any.pack(val)
end
end
return out
end
def build_proto_hash(h)
out = Hashicorp::Vagrant::Sdk::Args::Hash.new
h.each do |k, v|
if v.is_a?(Hash)
out.fields[k] = Google::Protobuf::Any.pack(build_proto_hash(v))
next
end
if v.is_a?(Array)
out.fields[k] = Google::Protobuf::Any.pack(build_proto_array(v))
next
end
if v.class.ancestors.include?(Google::Protobuf::MessageExts)
out.fields[k] = Google::Protobuf::Any.pack(v)
else
val = Google::Protobuf::Value.new
val.from_ruby(v)
out.fields[k] = Google::Protobuf::Any.pack(val)
end
end
return out
end
def to_proto(type)
mapper = VagrantPlugins::CommandServe::Mappers.new
protoize = self.instance_variables_hash
protoize.map do |k,v|
# Get embedded default struct
@ -257,7 +211,7 @@ module Vagrant
end
end
protoize = clean_up_config_object(protoize)
config_struct = build_proto_hash(protoize)
config_struct = mapper.map(protoize, to: Hashicorp::Vagrant::Sdk::Args::Hash)
config_any = Google::Protobuf::Any.pack(config_struct)
GENERAL_CONFIG_CLS.new(type: type, config: config_any)
end