Allow name lookup overrides for proto integration

This commit is contained in:
Chris Roberts 2021-10-01 12:10:54 -07:00 committed by Paul Hinze
parent 4420feb9cf
commit 71ce491197
No known key found for this signature in database
GPG Key ID: B69DEDF2D55501C0
2 changed files with 22 additions and 6 deletions

View File

@ -10,14 +10,22 @@ module VagrantPlugins
end
def initialize(conn, proto, broker=nil)
srv = self.class.name.split('::').last
n = self.class.respond_to?(:sdk_alias) ? self.class.sdk_alias : self.class.name
lookup = n.split("::")
idx = lookup.index("Client")
if idx
lookup.slice!(0, idx+1)
end
srv = "#{lookup.join}Service"
logger.debug("connecting to #{srv.downcase} service on #{conn}")
@broker = broker
@proto = proto
srv_klass = SDK.const_get("#{srv}Service")&.const_get(:Stub)
srv_klass = SDK.const_get(srv)&.const_get(:Stub)
if !srv_klass
raise NameError,
"failed to locate required protobuf constant `SDK::#{srv}Service'"
"failed to locate required protobuf constant `SDK::#{srv}'"
end
@client = srv_klass.new(conn, :this_channel_is_insecure)
end

View File

@ -17,11 +17,19 @@ module VagrantPlugins
def load(raw, broker:)
if raw.is_a?(String)
srv = self.class.name.split('::').last
klass = SDK::Args.const_get(srv)
n = self.respond_to?(:sdk_alias) ? self.sdk_alias : self.name
lookup = n.split("::")
idx = lookup.index("Client")
if idx
lookup.slice!(0, idx+1)
end
klass = lookup.inject(SDK::Args) do |const, name|
const&.const_get(name)
end
if !klass
raise NameError,
"failed to locate required protobuf constant `SDK::Args::#{srv}'"
"failed to locate required protobuf constant `SDK::Args::#{n}'\n\nArgs: #{SDK::Args.constants.inspect}"
end
raw = klass.decode(raw)
end