From c2d8f892acd63138e827e022f1cf323b3a1a32f6 Mon Sep 17 00:00:00 2001 From: sophia Date: Tue, 26 Jul 2022 12:22:28 -0500 Subject: [PATCH] Find type to unany objects when mapping MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- plugins/commands/serve/mappers.rb | 18 ++++++++++++++++++ plugins/commands/serve/util.rb | 1 + .../commands/serve/util/direct_conversions.rb | 18 ++++++++++++++++++ 3 files changed, 37 insertions(+) diff --git a/plugins/commands/serve/mappers.rb b/plugins/commands/serve/mappers.rb index 9cc3679fb..7083f665a 100644 --- a/plugins/commands/serve/mappers.rb +++ b/plugins/commands/serve/mappers.rb @@ -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 diff --git a/plugins/commands/serve/util.rb b/plugins/commands/serve/util.rb index d94b13f09..a8f963df7 100644 --- a/plugins/commands/serve/util.rb +++ b/plugins/commands/serve/util.rb @@ -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 diff --git a/plugins/commands/serve/util/direct_conversions.rb b/plugins/commands/serve/util/direct_conversions.rb index ff4861b24..d13c9a44f 100644 --- a/plugins/commands/serve/util/direct_conversions.rb +++ b/plugins/commands/serve/util/direct_conversions.rb @@ -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