Skip to content

Commit a516476

Browse files
committed
Fix given_or_derived behavior in Pops object init_hash
In create_init_hash_type the type specification for an init hash is built. Currently a given_or_derived type is always defined as a required init_hash attribute, which defeats the ability to leave it unspecified and use the derived value defaults. By defining given_or_derived types always as optional it allows passing a value in the init_hash or leaving it out and using the derived value.
1 parent de1f7e4 commit a516476

File tree

2 files changed

+8
-8
lines changed

2 files changed

+8
-8
lines changed

lib/puppet/pops/types/p_object_type.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -876,7 +876,7 @@ def create_init_hash_type
876876
struct_elems = {}
877877
attributes(true).values.each do |attr|
878878
unless attr.kind == ATTRIBUTE_KIND_CONSTANT || attr.kind == ATTRIBUTE_KIND_DERIVED
879-
if attr.value?
879+
if attr.value? || attr.kind == ATTRIBUTE_KIND_GIVEN_OR_DERIVED
880880
struct_elems[TypeFactory.optional(attr.name)] = attr.type
881881
else
882882
struct_elems[attr.name] = attr.type

spec/unit/pops/types/p_object_type_spec.rb

+7-7
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)