Skip to content

Commit dd41b40

Browse files
authored
Merge pull request #22401 from Homebrew/fix-cask-choice-zeroes
Preserve cask choice zeroes
2 parents df39236 + e009027 commit dd41b40

3 files changed

Lines changed: 37 additions & 8 deletions

File tree

Library/Homebrew/api/cask_struct.rb

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,15 @@ def serialize
116116
[prop.to_s, send(prop)]
117117
end.to_h
118118

119-
hash["raw_artifacts"] = raw_artifacts.map do |artifact|
119+
hash["raw_artifacts"] = ::Utils.deep_compact_blank(raw_artifacts.map do |artifact|
120120
serialize_artifact_args(artifact)
121-
end
121+
end, compact_zero: false)
122122

123123
hash = ::Utils.deep_stringify_symbols(hash)
124-
::Utils.deep_compact_blank(hash)
124+
raw_artifacts = hash["raw_artifacts"]
125+
hash = ::Utils.deep_compact_blank(hash)
126+
hash["raw_artifacts"] = raw_artifacts if raw_artifacts.present?
127+
hash
125128
end
126129

127130
sig { params(hash: T::Hash[String, T.untyped]).returns(CaskStruct) }

Library/Homebrew/test/api/cask_struct_spec.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,32 @@
138138
.to eq([:preflight, ["foo"], { bar: "baz" }, nil])
139139
end
140140

141+
it "preserves zero values in serialized artifact arguments" do
142+
struct = klass.new(
143+
sha256: "abc123",
144+
version: "1.0.0",
145+
ruby_source_checksum: { sha256: "def456" },
146+
raw_artifacts: [
147+
[
148+
:pkg,
149+
["Test.pkg"],
150+
{ choices: [{ choiceIdentifier: "choice1", choiceAttribute: "selected", attributeSetting: 0 }] },
151+
nil,
152+
],
153+
],
154+
)
155+
156+
expect(struct.serialize.fetch("raw_artifacts"))
157+
.to eq([
158+
[
159+
":pkg",
160+
["Test.pkg"],
161+
{ ":choices" => [{ ":choiceIdentifier" => "choice1", ":choiceAttribute" => "selected",
162+
":attributeSetting" => 0 }] },
163+
],
164+
])
165+
end
166+
141167
specify "::deserialize_artifact_args", :aggregate_failures do
142168
expect(klass.deserialize_artifact_args([:foo]))
143169
.to eq([:foo, [], {}, nil])

Library/Homebrew/utils.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -185,21 +185,21 @@ def self.deep_unstringify_symbols(obj)
185185

186186
sig {
187187
type_parameters(:U)
188-
.params(obj: T.all(T.type_parameter(:U), Object))
188+
.params(obj: T.all(T.type_parameter(:U), Object), compact_zero: T::Boolean)
189189
.returns(T.nilable(T.type_parameter(:U)))
190190
}
191-
def self.deep_compact_blank(obj)
191+
def self.deep_compact_blank(obj, compact_zero: true)
192192
obj = case obj
193193
when Hash
194-
obj.transform_values { |v| deep_compact_blank(v) }
194+
obj.transform_values { |v| deep_compact_blank(v, compact_zero:) }
195195
.compact
196196
when Array
197-
obj.filter_map { |v| deep_compact_blank(v) }
197+
obj.filter_map { |v| deep_compact_blank(v, compact_zero:) }
198198
else
199199
obj
200200
end
201201

202-
return if obj.blank? || (obj.is_a?(Numeric) && obj.zero?)
202+
return if obj.blank? || (compact_zero && obj.is_a?(Numeric) && obj.zero?)
203203

204204
obj
205205
end

0 commit comments

Comments
 (0)