Thrash around making to proto for config faster

This commit is contained in:
sophia 2022-04-01 16:52:55 -05:00 committed by Paul Hinze
parent f4811af759
commit d5aacc0bc6
No known key found for this signature in database
GPG Key ID: B69DEDF2D55501C0
5 changed files with 55 additions and 7 deletions

View File

@ -63,7 +63,7 @@ module Vagrant
protoize = self.instance_variables_hash
protoize.delete_if{|k,v| k.start_with?("_") }
config_struct = Google::Protobuf::Struct.from_hash(protoize)
config_struct = mapper.map(protoize, to: Hashicorp::Vagrant::Sdk::Args::Hash)
config_any = Google::Protobuf::Any.pack(config_struct)
Hashicorp::Vagrant::Sdk::Vagrantfile::GeneralConfig.new(type: type, config: config_any)
end

View File

@ -230,9 +230,55 @@ module Vagrant
protoize
end
def to_proto(type)
mapper = VagrantPlugins::CommandServe::Mappers.new
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)
protoize = self.instance_variables_hash
protoize.map do |k,v|
# Get embedded default struct
@ -243,7 +289,7 @@ module Vagrant
end
end
protoize = clean_up_config_object(protoize)
config_struct = mapper.map(protoize, to: Hashicorp::Vagrant::Sdk::Args::Hash)
config_struct = build_proto_hash(protoize)
config_any = Google::Protobuf::Any.pack(config_struct)
GENERAL_CONFIG_CLS.new(type: type, config: config_any)
end

View File

@ -198,7 +198,7 @@ module VagrantPlugins
end
end
class SpecialTypeSymbolToAny < Mapper
class SymbolToAny < Mapper
def initialize
super(
inputs: [Input.new(type: SDK::SpecialTypes::Symbol)],
@ -212,7 +212,7 @@ module VagrantPlugins
end
end
class SpecialTypeSymbolToSymbol < Mapper
class SymbolProtoToSymbol < Mapper
def initialize
super(
inputs: [Input.new(type: SDK::SpecialTypes::Symbol)],

View File

@ -1126,6 +1126,7 @@ module VagrantPlugins
sf_proto.send("#{opt.to_s}=", val)
end
config_struct = @mapper.map(config_opts, to: Hashicorp::Vagrant::Sdk::Args::Hash)
config_any = Google::Protobuf::Any.pack(config_struct)
sf_proto.config = config_any

View File

@ -202,7 +202,8 @@ module VagrantPlugins
begin
if k == "config"
protoize = clean_up_config_object(c.config.instance_variables_hash)
config_struct = mapper.map(protoize, to: Hashicorp::Vagrant::Sdk::Args::Hash)
config_struct = @mapper.map(protoize, to: Hashicorp::Vagrant::Sdk::Args::Hash)
config_any = Google::Protobuf::Any.pack(config_struct)
proto.config = config_any
next