ruby mappers: Unwrap wrapper types in Hashes and Arrays

Boolean types (and possibly a few others) are returned as wrapper
classes when coming out from proto mapping; these need to be unwrapped
otherwise the caller who is expecting a nice clean boolean value ends up
with an icky protobuf class.

This fixes the shell provisioner, which relies on a communicator
receiving a settings hash `{error_check: false}` for a command that
usually fails but it sent just in case before provisioning starts.
This commit is contained in:
Paul Hinze 2022-01-28 14:01:32 -06:00
parent 896cdea21f
commit a86965c340
No known key found for this signature in database
GPG Key ID: B69DEDF2D55501C0
2 changed files with 45 additions and 1 deletions

View File

@ -77,7 +77,10 @@ module VagrantPlugins
def converter(proto, mapper)
begin
proto.list.map do |v|
mapper.map(v)
r = mapper.map(v)
# unwrap any wrapper classes here before assigning
r = r.value if r.is_a?(Type)
r
end
rescue => err
logger.error("proto mapping to array failed: #{err}")
@ -169,6 +172,8 @@ module VagrantPlugins
Hash.new.tap do |result|
proto.fields.each do |k, v|
r = mapper.map(v)
# unwrap any wrapper classes here before assigning
r = r.value if r.is_a?(Type)
result[k.to_sym] = r
end
end

View File

@ -0,0 +1,39 @@
require File.expand_path("../../../base", __dir__)
require Vagrant.source_root.join("plugins/commands/serve/command")
describe VagrantPlugins::CommandServe::Mappers do
include_context "unit"
subject { described_class.new }
context "Hash" do
it "unwraps wrapper types when they show up in the Hash" do
input = Hashicorp::Vagrant::Sdk::Args::Hash.new(
fields: {
"error_check" => Google::Protobuf::Any.pack(
Google::Protobuf::BoolValue.new(value: false)
)
}
)
output = subject.map(input, to: Hash)
expect(output).to eq({error_check: false})
end
end
context "Array" do
it "unwraps wrapper types when they show up in the Array" do
input = Hashicorp::Vagrant::Sdk::Args::Array.new(
list: [
Google::Protobuf::Any.pack(
Google::Protobuf::BoolValue.new(value: false)
),
],
)
output = subject.map(input, to: Array)
expect(output).to eq([false])
end
end
end