[PUP-4438] Add required_repeated_param to 4.x function API Created: 2015/04/20  Updated: 2015/05/20  Resolved: 2015/05/20

Status: Closed
Project: Puppet
Component/s: Compiler, Docs
Affects Version/s: None
Fix Version/s: PUP 3.8.1, PUP 4.1.0

Type: Improvement Priority: Normal
Reporter: Thomas Hallgren Assignee: Unassigned
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Blocks
blocks PUP-4178 defined() function returns true for u... Closed
blocks PUP-2315 function call error message about mis... Closed
Template:
Story Points: 1
Sprint: Language 2015-04-29
Release Notes: New Feature

 Description   

It's currently not possible to specify a 'repeated_param' that is required in the 4.x. function API. An example of when this is needed is the defined() function. It requires at least one argument but may take several. Without the possibility to specify that at least one parameter must be present, this must be encoded as a required parameter followed by a 0:M related parameter. This does not really matter from an implementation standpoint (you have to write two parameters instead of one), but it matters from a UX perspective if there is an error; a user should perceive this as "one or more" rather than "one parameter, then zero or more additional parameters".

The function API should add two new parameter declarations:

optional_repeated_param  # alias for the current repeated_param
required_repeated_param  # will set the minimum argument count to 1

risk: medium
probability: low (custom functions with optional repeated params)
severity: medium (work arounds exist, UX issue)
test layer: unit



 Comments   
Comment by Henrik Lindberg [ 2015/04/20 ]

Specification updated at: b338f8d3c40800284cf11b22e8f3ad619eeaaef9

Comment by Henrik Lindberg [ 2015/04/20 ]

Once this is merged, PUP-4178 can be updated to make use of this feature.

Comment by Henrik Lindberg [ 2015/04/20 ]

Merged to 3.x at: c55b69a

Comment by Henrik Lindberg [ 2015/04/20 ]

Merged to master at: 0102311 (https://github.com/puppetlabs/puppet/pull/3852)

Comment by Thomas Hallgren [ 2015/04/21 ]

Found a problem with this fix. We cannot allow a required_repeated_param to follow an optional_param.

Comment by Henrik Lindberg [ 2015/04/21 ]

I found another problem that I fixed in the PR for PUP-4178 (that commit is marked with PUP-4438 in that PR).

Comment by Thomas Hallgren [ 2015/04/21 ]

PR-3855 addresses the problem with required_repeated_param declared after optional ones.

Comment by Henrik Lindberg [ 2015/04/21 ]

merged to 3.x at: 5badbff

Comment by Henrik Lindberg [ 2015/04/21 ]

merged to master at: ffaffb9

Comment by Eric Thompson [ 2015/04/21 ]

validated on ubuntu14 at master SHA: ffaffb9

root@plgf5s85ojlj8m1 functions]# cat t1.rb
f = Puppet::Functions.create_function('t1') do
  dispatch :t1 do
    optional_param 'Numeric', :x
    required_repeated_param 'Numeric', :y
  end
  def t1(x, *y)
    x
  end
end
[root@plgf5s85ojlj8m1 functions]# puppet apply -e "t1(a,b)"
Error: Evaluation Error: Error while evaluating a Function Call, A required repeated parameter cannot be added after an optional parameter  at line 1:1 on node plgf5s85ojlj8m1.delivery.puppetlabs.net
 
[root@plgf5s85ojlj8m1 functions]# vi t1.rb
[root@plgf5s85ojlj8m1 functions]# cat t1.rb
f = Puppet::Functions.create_function('t1') do
  dispatch :t1 do
    param 'Numeric', :y
    optional_param 'Numeric', :x
  end
  def t1(y, *x)
    puts "#{x}"
  end
end
[root@plgf5s85ojlj8m1 functions]# puppet apply -e "t1(1,2)"
[2]
Notice: Compiled catalog for plgf5s85ojlj8m1.delivery.puppetlabs.net in environment production in 0.38 seconds
Notice: Applied catalog in 0.01 seconds
[root@plgf5s85ojlj8m1 functions]# puppet apply -e "t1(1)"
[]
Notice: Compiled catalog for plgf5s85ojlj8m1.delivery.puppetlabs.net in environment production in 0.40 seconds
Notice: Applied catalog in 0.01 seconds

Comment by Eric Thompson [ 2015/04/21 ]

this should be validated against 3.x as well.

Comment by Eric Thompson [ 2015/04/21 ]

validated on windows2012r2-rubyx64 at SHA: 39bd7a7

Administrator@f67onhgjleryc6t ~
$ cat /lib/ruby/site_ruby/2.1.0/puppet/functions/t1.rb
f = Puppet::Functions.create_function('t1') do
  dispatch :t1 do
    optional_param 'Numeric', :x
    required_repeated_param 'Numeric', :y
  end
  def t1(x, *y)
    puts "#{x}"
  end
end
 
Administrator@f67onhgjleryc6t ~
$ cmd /c puppet apply -e 't1(a,b)'
Error: Evaluation Error: Error while evaluating a Function Call, A required repeated parameter cannot be added after an optional parameter  at line 1:1 on node f67onhgjleryc6t.delivery.puppetlabs.net
Error: Evaluation Error: Error while evaluating a Function Call, A required repeated parameter cannot be added after an optional parameter  at line 1:1 on node f67onhgjleryc6t.delivery.puppetlabs.net
 
dministrator@f67onhgjleryc6t ~
$ cat /lib/ruby/site_ruby/2.1.0/puppet/functions/t1.rb
f = Puppet::Functions.create_function('t1') do
  dispatch :t1 do
    param 'Numeric', :y
    optional_param 'Numeric', :x
  end
  def t1(y, *x)
    puts "#{x}"
  end
end
 
Administrator@f67onhgjleryc6t ~
$ cmd /c puppet apply -e 't1(1,2)'
[2]
Notice: Compiled catalog for f67onhgjleryc6t.delivery.puppetlabs.net in environment production in 0.44 seconds
Notice: Applied catalog in 0.02 seconds
 
Administrator@f67onhgjleryc6t ~
$ cmd /c puppet apply -e 't1(2)'
[]
Notice: Compiled catalog for f67onhgjleryc6t.delivery.puppetlabs.net in environment production in 0.42 seconds
Notice: Applied catalog in 0.02 seconds

Comment by Eric Thompson [ 2015/04/21 ]

validated on unbuntu14 at 3.x SHA: 5badbff

[root@gxoy1yaj7qlciuz ~]# cat /usr/local/share/ruby/site_ruby/puppet/functions/t1.rb
f = Puppet::Functions.create_function('t1') do
  dispatch :t1 do
    optional_param 'Numeric', :x
    required_repeated_param 'Numeric', :y
  end
  def t1(y, *x)
    puts "#{x}"
  end
end
[root@gxoy1yaj7qlciuz ~]# puppet apply -e 't1(a,b)' --parser future
Error: Evaluation Error: Error while evaluating a Function Call, A required repeated parameter cannot be added after an optional parameter  at line 1:1 on node gxoy1yaj7qlciuz.delivery.puppetlabs.net
Error: Evaluation Error: Error while evaluating a Function Call, A required repeated parameter cannot be added after an optional parameter  at line 1:1 on node gxoy1yaj7qlciuz.delivery.puppetlabs.net
[root@gxoy1yaj7qlciuz ~]# vi /usr/local/share/ruby/site_ruby/puppet/functions/t1.rb
[root@gxoy1yaj7qlciuz ~]# cat /usr/local/share/ruby/site_ruby/puppet/functions/t1.rb
f = Puppet::Functions.create_function('t1') do
  dispatch :t1 do
    param 'Numeric', :y
    optional_param 'Numeric', :x
  end
  def t1(y, *x)
    puts "#{x}"
  end
end
[root@gxoy1yaj7qlciuz ~]# puppet apply -e 't1(1,2)' --parser future
[2]
Notice: Compiled catalog for gxoy1yaj7qlciuz.delivery.puppetlabs.net in environment production in 0.45 seconds
Notice: Finished catalog run in 0.01 seconds
[root@gxoy1yaj7qlciuz ~]# puppet apply -e 't1(2)' --parser future
[]
Notice: Compiled catalog for gxoy1yaj7qlciuz.delivery.puppetlabs.net in environment production in 0.45 seconds
Notice: Finished catalog run in 0.01 seconds

Generated at Wed Oct 16 12:10:10 PDT 2019 using JIRA 7.7.1#77002-sha1:e75ca93d5574d9409c0630b81c894d9065296414.