Skip to content

Commit 74012d4

Browse files
committed
Fix given_or_derived behavior in Pops Object initialization
The PCore specification states "The given_or_derived attribute kind states that if the attribute is given when the object is instantiated this value is used, otherwise it is a computed value." and "It is allowed to give the value when instantiating in which case the given value overrides what would be the computed value." The current implementation treats an attribute with the kind given_or_derived as a required parameter during initialization (in the init_hash used in from_asserted_hash or in the generated `initialize` method). This change enforces an implicit `value` parameter of `nil` which causes the generated init_hash type check and the generated initialize method to treat given_or_derived attributes as optional.
1 parent de1f7e4 commit 74012d4

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

lib/puppet/pops/types/p_object_type.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,8 @@ def initialize(name, container, init_hash)
316316

317317
v = init_hash[KEY_VALUE]
318318
@value = v == :default ? v : TypeAsserter.assert_instance_of(nil, type, v) { "#{label} #{KEY_VALUE}" }
319+
elsif @kind == ATTRIBUTE_KIND_GIVEN_OR_DERIVED
320+
@value = nil
319321
else
320322
raise Puppet::ParseError, _("%{label} of kind 'constant' requires a value") % { label: label } if @kind == ATTRIBUTE_KIND_CONSTANT
321323

@@ -341,7 +343,7 @@ def _pcore_init_hash
341343
hash[KEY_KIND] = @kind
342344
hash.delete(KEY_FINAL) if @kind == ATTRIBUTE_KIND_CONSTANT # final is implied
343345
end
344-
hash[KEY_VALUE] = @value unless @value == :undef
346+
hash[KEY_VALUE] = @value unless @value == :undef || @kind == ATTRIBUTE_KIND_GIVEN_OR_DERIVED
345347
hash
346348
end
347349

spec/unit/pops/types/p_object_type_spec.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -697,31 +697,31 @@ def parse_object(name, body_string)
697697
end
698698

699699
context 'when producing an init_hash_type' do
700-
it 'produces a struct of all attributes that are not derived or constant' do
700+
it 'produces a struct of all attributes excepting derived or constant' do
701701
t = parse_object('MyObject', <<-OBJECT)
702702
attributes => {
703703
a => { type => Integer },
704-
b => { type => Integer, kind => given_or_derived },
705-
c => { type => Integer, kind => derived },
706-
d => { type => Integer, kind => constant, value => 4 }
704+
b => { type => Integer, kind => derived },
705+
c => { type => Integer, kind => constant, value => 4 }
707706
}
708707
OBJECT
709708
expect(t.init_hash_type).to eql(factory.struct({
710709
'a' => factory.integer,
711-
'b' => factory.integer
712710
}))
713711
end
714712

715713
it 'produces a struct where optional entires are denoted with an optional key' do
716714
t = parse_object('MyObject', <<-OBJECT)
717715
attributes => {
718716
a => { type => Integer },
719-
b => { type => Integer, value => 4 }
717+
b => { type => Integer, value => 4 },
718+
c => { type => Integer, kind => given_or_derived },
720719
}
721720
OBJECT
722721
expect(t.init_hash_type).to eql(factory.struct({
723722
'a' => factory.integer,
724-
factory.optional('b') => factory.integer
723+
factory.optional('b') => factory.integer,
724+
factory.optional('c') => factory.integer,
725725
}))
726726
end
727727

0 commit comments

Comments
 (0)