Skip to content

Fix given_or_derived behavior in object initialization #38

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/puppet/pops/types/p_object_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,8 @@ def initialize(name, container, init_hash)

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

Expand All @@ -341,7 +343,7 @@ def _pcore_init_hash
hash[KEY_KIND] = @kind
hash.delete(KEY_FINAL) if @kind == ATTRIBUTE_KIND_CONSTANT # final is implied
end
hash[KEY_VALUE] = @value unless @value == :undef
hash[KEY_VALUE] = @value unless @value == :undef || @kind == ATTRIBUTE_KIND_GIVEN_OR_DERIVED
hash
end

Expand Down
14 changes: 7 additions & 7 deletions spec/unit/pops/types/p_object_type_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -697,31 +697,31 @@ def parse_object(name, body_string)
end

context 'when producing an init_hash_type' do
it 'produces a struct of all attributes that are not derived or constant' do
it 'produces a struct of all attributes excepting derived or constant' do
t = parse_object('MyObject', <<-OBJECT)
attributes => {
a => { type => Integer },
b => { type => Integer, kind => given_or_derived },
c => { type => Integer, kind => derived },
d => { type => Integer, kind => constant, value => 4 }
b => { type => Integer, kind => derived },
c => { type => Integer, kind => constant, value => 4 }
}
OBJECT
expect(t.init_hash_type).to eql(factory.struct({
'a' => factory.integer,
'b' => factory.integer
}))
end

it 'produces a struct where optional entires are denoted with an optional key' do
t = parse_object('MyObject', <<-OBJECT)
attributes => {
a => { type => Integer },
b => { type => Integer, value => 4 }
b => { type => Integer, value => 4 },
c => { type => Integer, kind => given_or_derived },
}
OBJECT
expect(t.init_hash_type).to eql(factory.struct({
'a' => factory.integer,
factory.optional('b') => factory.integer
factory.optional('b') => factory.integer,
factory.optional('c') => factory.integer,
}))
end

Expand Down