Skip to content

Commit 00d0414

Browse files
committed
(PUP-11981) Syntactically incorrect types cause nil
This commit adds logic to validate class parameters are literal and provides a descriptive warning if parameter(s) are not literal, adds QualifiedReference and AccessExpression as a valid literal values & updates spec tests to check for more cases of invalid class parameters and any that broke as a result of the changes. The logic to validate class parameters is done in internal_check_parameter_type_literal method where the appropriate literal method is called on each parameter. There are different literal methods for each literal object/type [1]. For example if the parameter is String type, then literal_LiteralString is called. If a parameter is found to be not literal, then a :not_literal will be thrown & warning message with the invalid class parameter and its type will be printed. QualifiedReference is considered literal since it is a literal reference, however, there is a possibility it could be a faulty dangling reference that doesn't reference anything. AccessExpresions are anything with the Access Operator ([ ]) such as Optional[String] or Variant[Boolean, Float] [2]. Since these are valid class parameters, AccessExpressions are also considered literal. The logic to add QualifiedReference & AccessExpression as valid literal values is done in the literal_QualifiedReference & literal_AccessExpression methods. The literal_AccessExpression works by validating each key in the expression is literal. The literal_QualifiedReference method looks at o.value, similar to other existing literal methods like literal_LiteralString. [1] http://puppet-on-the-edge.blogspot.com/2014/02/puppet-internals-polymorphic-dispatch.html [2] https://github.com/puppetlabs/puppet-specifications/blob/master/language/expressions.md#assignment-operator For more information on this issue, see #9140
1 parent 775592c commit 00d0414

File tree

4 files changed

+32
-5
lines changed

4 files changed

+32
-5
lines changed

lib/puppet/pops/evaluator/literal_evaluator.rb

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@ module Evaluator
1111
# QualifiedName
1212
# Default (produced :default)
1313
# Regular Expression (produces ruby regular expression)
14-
#
15-
# Not considered literal
16-
# QualifiedReference # i.e. File, FooBar
14+
# QualifiedReference
15+
# AccessExpresion
1716
#
1817
class LiteralEvaluator
1918

@@ -67,6 +66,15 @@ def literal_LiteralRegularExpression(o)
6766
o.value
6867
end
6968

69+
def literal_QualifiedReference(o)
70+
o.value
71+
end
72+
73+
def literal_AccessExpression(o)
74+
throw :not_literal if o.keys.size == 1 && o.keys[0].is_a?(Model::LiteralList)
75+
o.keys.map {|v| literal(v) }
76+
end
77+
7078
def literal_ConcatenatedString(o)
7179
# use double quoted string value if there is no interpolation
7280
throw :not_literal unless o.segments.size == 1 && o.segments[0].is_a?(Model::LiteralString)

lib/puppet/pops/issues.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,10 @@ def self.hard_issue(issue_code, *args, &block)
207207
_("The numeric parameter name '$%{name}' cannot be used (clashes with numeric match result variables)") % { name: name }
208208
end
209209

210+
ILLEGAL_NONLITERAL_PARAMETER_TYPE = issue :ILLEGAL_NONLITERAL_PARAMETER_TYPE, :name, :type_class do
211+
_("The parameter '$%{name}' must be a literal type, not %{type_class}") % { name: name, type_class: label.a_an(type_class) }
212+
end
213+
210214
# In certain versions of Puppet it may be allowed to assign to a not already assigned key
211215
# in an array or a hash. This is an optional validation that may be turned on to prevent accidental
212216
# mutation.

lib/puppet/pops/validation/checker4_0.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,7 @@ def check_HostClassDefinition(o)
471471
internal_check_parameter_name_uniqueness(o)
472472
internal_check_reserved_params(o)
473473
internal_check_no_idem_last(o)
474+
internal_check_parameter_type_literal(o)
474475
end
475476

476477
def check_ResourceTypeDefinition(o)
@@ -481,6 +482,18 @@ def check_ResourceTypeDefinition(o)
481482
internal_check_no_idem_last(o)
482483
end
483484

485+
def internal_check_parameter_type_literal(o)
486+
type = nil
487+
o.parameters.each do |p|
488+
next unless p.type_expr
489+
catch :not_literal do
490+
491+
type = literal(p.type_expr)
492+
end
493+
acceptor.accept(Issues::ILLEGAL_NONLITERAL_PARAMETER_TYPE, p, {name: p.name, type_class: p.type_expr.class}) if type.nil?
494+
end
495+
end
496+
484497
def internal_check_return_type(o)
485498
r = o.return_type
486499
internal_check_type_ref(o, r) unless r.nil?

spec/unit/pops/evaluator/literal_evaluator_spec.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
'"a"' => 'a',
1717
'a' => 'a',
1818
'a::b' => 'a::b',
19+
'Integer[1]' => [1],
20+
'File' => "file",
1921

2022
# special values
2123
'default' => :default,
@@ -35,9 +37,9 @@
3537
expect(leval.literal(parser.parse_string('undef'))).to be_nil
3638
end
3739

38-
['1+1', 'File', '[1,2, 1+2]', '{a=>1+1}', 'Integer[1]', '"x$y"', '"x${y}z"'].each do |source|
40+
['1+1', '[1,2, 1+2]', '{a=>1+1}', '"x$y"', '"x${y}z"', 'Integer[1-3]', 'Optional[[String]]'].each do |source|
3941
it "throws :not_literal for non literal expression '#{source}'" do
40-
expect{leval.literal(parser.parse_string('1+1'))}.to throw_symbol(:not_literal)
42+
expect{leval.literal(parser.parse_string(source))}.to throw_symbol(:not_literal)
4143
end
4244
end
4345
end

0 commit comments

Comments
 (0)