Skip to content

Commit 6cc11b2

Browse files
Duplicate config defaults before setting
For scalar types used as config parameter default values, the existing logic is fine. But if a config default value is something like an array or hash, or any mutable object, we should not return the same instance every time the default value is set for that attribute on a config
1 parent 773d9da commit 6cc11b2

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

lib/roast/dsl/cog/config.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,12 @@ def [](key)
4545
class << self
4646
#: [T] (Symbol, T) ?{(T) -> T} -> void
4747
def field(key, default, &validator)
48+
default = default #: as untyped
49+
4850
define_method(key) do |*args|
4951
if args.empty?
5052
# with no args, return the configured value, or the default
51-
@values[key] || default
53+
@values[key] || default.deep_dup
5254
else
5355
# with an argument, set the configured value
5456
new_value = args.first
@@ -58,7 +60,7 @@ def field(key, default, &validator)
5860

5961
define_method("use_default_#{key}!".to_sym) do
6062
# explicitly set the configured value to the default
61-
@values[key] = default
63+
@values[key] = default.deep_dup
6264
end
6365
end
6466
end

0 commit comments

Comments
 (0)