Skip to content

Commit

Permalink
Convert True/False/Array to string
Browse files Browse the repository at this point in the history
Cloudformation doesn't take Arrays or any other kind of things that YAML
may consider objects. So let's convert what we can to String safely.

We're not converting `null` to string because that may be a breaking
change.
  • Loading branch information
tigris committed Apr 23, 2024
1 parent 0fa6a4b commit f6ad592
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 8 deletions.
23 changes: 21 additions & 2 deletions lib/stackup/parameters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def to_a
if value == :use_previous_value
record[:use_previous_value] = true
else
record[:parameter_value] = value.to_s
record[:parameter_value] = ParameterValue.new(value).to_param
end
end
end
Expand Down Expand Up @@ -75,7 +75,26 @@ def value
if use_previous_value
:use_previous_value
else
parameter_value.to_s
ParameterValue.new(parameter_value).to_param
end
end

end

class ParameterValue

def initialize(value)
@value = value
end

def to_param
case @value
when TrueClass, FalseClass, Integer then
@value.to_s
when Array
@value.map { |v| ParameterValue.new(v).to_param }.join(",")
else
@value
end
end

Expand Down
30 changes: 24 additions & 6 deletions spec/stackup/parameters_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,24 +146,42 @@

end

context "with Integer values" do
context "with non-string values" do

let(:input_records) do
[{ :parameter_key => "SomeInteger", :parameter_value => 123_456_789 }]
[
{ :parameter_key => "Integer", :parameter_value => 123_456_789 },
{ :parameter_key => "Truthy", :parameter_value => true },
{ :parameter_key => "Falsey", :parameter_value => false },
{ :parameter_key => "Null", :parameter_value => nil },
{ :parameter_key => "String", :parameter_value => "string" }
]
end

subject(:parameters) { Stackup::Parameters.new(input_records) }

describe "#to_hash" do
it "converts the Integer to a String" do
expected = { "SomeInteger" => "123456789" }
it "converts the to strings where appropriate" do
expected = {
"Integer" => "123456789",
"Truthy" => "true",
"Falsey" => "false",
"Null" => nil,
"String" => "string"
}
expect(parameters.to_hash).to eql(expected)
end
end

describe "#to_a" do
it "converts the Integer to a String" do
expected = [{ :parameter_key => "SomeInteger", :parameter_value => "123456789" }]
it "converts the to strings where appropriate" do
expected = [
{ :parameter_key => "Integer", :parameter_value => "123456789" },
{ :parameter_key => "Truthy", :parameter_value => "true" },
{ :parameter_key => "Falsey", :parameter_value => "false" },
{ :parameter_key => "Null", :parameter_value => nil },
{ :parameter_key => "String", :parameter_value => "string" }
]
expect(parameters.to_a).to eql(expected)
end
end
Expand Down

0 comments on commit f6ad592

Please sign in to comment.