Find type to unany objects when mapping

find_types works fine until you have a module which has the same name but different case. For example, the VagrantVmware package is HashiCorp::VagrantVMwareDesktop. All the protos are at Hashicorp::Vagrant::… So, you end up with this fun

Object.constants.select { |n| n.to_s.downcase == "hashicorp" }
=> [:HashiCorp, :Hashicorp]

So, when trying to walk down the modules to find the right type to unany to, Vagrant sometimes takes the wrong path (eg. Down the HashiCorp module instead of the Hashicorp module).

This change will keep a list of the parent modules when walking down the module list. This way if a dead end is reached then Vagrant can go a level back and keep searching for the correct class.
This commit is contained in:
sophia 2022-07-26 12:22:28 -05:00
parent 0081bb0f9b
commit c2d8f892ac
3 changed files with 37 additions and 0 deletions

View File

@ -168,10 +168,28 @@ module VagrantPlugins
# @param name [String]
# @return [Class]
def find_type(name)
parent_module_options = []
name.to_s.split(".").inject(Object) { |memo, n|
c = memo.constants.detect { |mc| mc.to_s.downcase == n.to_s.downcase }
if c.nil?
parent_module_options.delete(memo)
parent_module_options.each do |pm|
c = pm.constants.detect { |mc| mc.to_s.downcase == n.to_s.downcase }
if !c.nil?
memo = pm
break
end
end
end
raise NameError,
"Failed to find constant for `#{name}'" if c.nil?
parent_module_options = memo.constants.select {
|mc| mc.to_s.downcase == n.to_s.downcase
}.map {
|mc| memo.const_get(mc)
}
memo.const_get(c)
}
end

View File

@ -4,6 +4,7 @@ module VagrantPlugins
autoload :Cacher, Vagrant.source_root.join("plugins/commands/serve/util/cacher").to_s
autoload :ClientSetup, Vagrant.source_root.join("plugins/commands/serve/util/client_setup").to_s
autoload :Connector, Vagrant.source_root.join("plugins/commands/serve/util/connector").to_s
autoload :DirectConversion, Vagrant.source_root.join("plugins/commands/serve/util/direct_conversions").to_s
autoload :ExceptionTransformer, Vagrant.source_root.join("plugins/commands/serve/util/exception_transformer").to_s
autoload :FuncSpec, Vagrant.source_root.join("plugins/commands/serve/util/func_spec").to_s
autoload :HasBroker, Vagrant.source_root.join("plugins/commands/serve/util/has_broker").to_s

View File

@ -271,10 +271,28 @@ module Google::Protobuf::MessageExts
# @param name [String]
# @return [Class]
def _vagrant_find_type(name)
parent_module_options = []
name.to_s.split(".").inject(Object) { |memo, n|
c = memo.constants.detect { |mc| mc.to_s.downcase == n.to_s.downcase }
if c.nil?
parent_module_options.delete(memo)
parent_module_options.each do |pm|
c = pm.constants.detect { |mc| mc.to_s.downcase == n.to_s.downcase }
if !c.nil?
memo = pm
break
end
end
end
raise NameError,
"Failed to find constant for `#{name}'" if c.nil?
parent_module_options = memo.constants.select {
|mc| mc.to_s.downcase == n.to_s.downcase
}.map {
|mc| memo.const_get(mc)
}
memo.const_get(c)
}
end