vaguerent/plugins/commands/serve/client/capability_platform.rb
Paul Hinze 75d900c93b
Fix Type::Booleans leaking through capabilities
We had some cases where calling a capability that returned a boolean was
not getting correctly unpacked, so instead of `true` or `false` the
capability was putting out
`VagrantPlugins::CommandServe::Type::Boolean`.

This may have been happening in _all_ cases where a boolean was returned
from a capability and we just didn't notice it yet because the return
value was always truthy.

These tweaks should help ensure that Ruby types make it out where they
are supposed to be in Args::Direct usage.
2022-07-07 11:29:50 -05:00

63 lines
1.8 KiB
Ruby

require "google/protobuf/well_known_types"
module VagrantPlugins
module CommandServe
class Client
module CapabilityPlatform
# Generate callback and spec for required arguments
#
# @return [SDK::FuncSpec, Proc]
def has_capability_func
spec = client.has_capability_spec(Empty.new)
cb = proc do |args|
client.has_capability(args).has_capability
end
[spec, cb]
end
# @param [Symbol] cap_name Capability name
# @return [Boolean]
def has_capability?(cap_name)
run_func(
SDK::Args::NamedCapability.new(
capability: cap_name.to_s
)
)
end
# Generate callback and spec for required arguments
#
# @param cap_name [String] Name of capability
# @return [SDK::FuncSpec, Proc]
def capability_func(cap_name)
spec = client.capability_spec(
SDK::Platform::Capability::NamedRequest.new(
name: cap_name,
)
)
cb = lambda do |name, args|
result = client.capability(
SDK::Platform::Capability::NamedRequest.new(
name: name,
func_args: args,
)
)
return nil if result.nil? || result.result.nil?
mapper.map(SDK::Args::Direct.new(arguments: [result.result])).arguments.first
end
[spec, cb]
end
# @param [Symbol] cap_name Name of the capability
def capability(cap_name, *args)
logger.debug("executing capability #{cap_name}")
spec, cb = capability_func(cap_name)
cb.call(cap_name,
generate_funcspec_args(spec,
Type::Direct.new(value: args), *args))
end
end
end
end
end