Bug #6745

Puppet needs a better undef/way to handle nil's from custom functions

Added by Rich Rauenzahn about 1 year ago. Updated 4 months ago.

Status:Re-opened Start date:03/17/2011
Priority:Normal Due date:
Assignee:Daniel Pittman % Done:

0%

Category:language
Target version:-
Affected Puppet version: Branch:
Keywords:
Votes: 0

Description

It appears to me that undef isn’t really a real type in puppet. If I pass something that is undef to a custom function and ask Ruby for its class, it says it is a String. It almost seems like undef is just an alias for “”. It would be nice if it was a distinct type.

More importantly (see farther below), nil bubbled up from Ruby code ought to be evaluated correctly within puppet.

Let’s look at the type mappings:

define test {

$type1 = vmw_type("") # this func returns foo.class.to_s
notice("string is $type1")

$array = ["a", "b"]
$type2 = vmw_type($array)
notice("array is $type2")

$dict = { "key" => "value" }
$type3 = vmw_type($dict)
notice("dict is $type3")

$bool = true
$type5 = vmw_type($bool)
notice("true is $type5")

$undef = undef
$type4 = vmw_type($undef)
notice("undef is $type4")

}

test { “foo”: }

[rich@rich-fedora tests]$ puppet —modulepath=/Perforce /main/machines/puppet/puppetmaster/modules/ tests/vmw_type.pp notice: Scope(Test[foo]): string is String notice: Scope(Test[foo]): array is Array notice: Scope(Test[foo]): dict is Hash notice: Scope(Test[foo]): true is TrueClass notice: Scope(Test[foo]): undef is String

So either undef is really a string, or it was evaluated to a string before being passed to the custom function.

Related, when a custom function returns a nil, puppet doesn’t know what to do with it at all, and I think that nil ought to be mapped to this undef type:

[rich@rich-fedora tests]$ puppet —modulepath=/Perforce/main/machines/puppet/puppetmaster/modules/ vmw_json_parse.pp

[…]

$foo3 = vmw_json_parse("bad|,'':") # this just calls the PSON parse, returns nil if fails to parse.

$type = vmw_type($foo3)
notice("type=$type")

if $foo3 == "" { notice("foo3 is ''") }
if $foo3 == undef { notice("foo3 is undef") }
if !$foo3 { notice("foo3 is not true") }
if $foo3 { notice("foo3 is true") }

[…]

The output below shows that NilClass is not an empty string, or is it undef, but is in fact true.

notice: Scope(Test[a]): type=NilClass notice: Scope(Test[a]): foo3 is true

Now, we could have puppet walk dictionaries, arrays, and scalars returned from custom functions and transform nil scalars into “” (or undef), but it would seem smarter to change the internal guts of puppet to treat NilClass as undef (is there really any such thing as undef in the internal code? Or is it really just an alias for “”?)

So, in summary:

(a) Puppet should handle nils returned from custom functions (b) Would this be a good time to also map undef to a ruby data type, like nil?


Related issues

duplicates Puppet - Feature #6621: Provide a nil type in the Puppet language Rejected 03/07/2011

History

Updated by konrad rzentarzewski about 1 year ago

same problem with defines and passing undefs to native types.

i work around it with checking if variable is empty and then setting it undef when calling parameters.

ie.

define foo ($owner=undef,$mode=undef) {
 # skipped logic setting variable copies owner_c and mode_c depending on bar

 file { $title:
  # skipped irrelevant file params

  owner => $owner_c ? { /.+/ => $owner_c, default => $owner ? { /.+/ => $owner, default => undef } },
  mode => $mode_c ? { /.+/ => $mode_c, default => $mode ? { /.+/ => $mode, default => undef } },
 }
}

sweet :)

Updated by Ben Hughes about 1 year ago

  • Status changed from Unreviewed to Needs Decision
  • Assignee set to Nigel Kersten

Updated by Nigel Kersten about 1 year ago

  • Status changed from Needs Decision to Duplicate

Have a look at #6621 for more discussion around this.

undef literally isn’t a value.

Updated by Timur Batyrshin 6 months ago

  • Status changed from Duplicate to Re-opened

Rich Rauenzahn wrote:

The output below shows that NilClass is not an empty string, or is it undef, but is in fact true.

notice: Scope(Test[a]): type=NilClass notice: Scope(Test[a]): foo3 is true

Now, we could have puppet walk dictionaries, arrays, and scalars returned from custom functions and transform nil scalars into “” (or undef),

So, in summary:

(a) Puppet should handle nils returned from custom functions

Currently there is no way to detect nil returned by custom function in puppet because it matches correct values and none of undef, “”, false can be used to detect it.

It means that you can not use return value of nil in your custom functions to indicate some error etc which feels rather uncomfortable to anyone familiar with ruby.

Updated by Krzysztof Wilczynski 6 months ago

Hello,

[…]

It means that you can not use return value of nil in your custom functions to indicate some error etc which feels rather uncomfortable to anyone familiar with ruby.

I would rather detect a potential error in the body of the function and raise Puppet::ParseError where appropriate.

You do want to fail and let a potential user know that something is going awry… over returning NilClass value.

To be honest, most of Puppet DSL users are not Ruby developers (emphasis on “most”), plus said DSL has its specific approach to certain things. It is also not a Turing-complete language AFAIK :)

For instance, numeric (integer and float) values are passed as strings into the “newfunction” block unless they are a negative values, etc. And it is how it always was, and you have to live with it :–)

Anyhow, the point I am trying to make is … that it is better to raise Puppet::ParseError when you expect to return NilClass value over simply returning nil for user to handle, which might even not be feasible in Puppet DSL.

My outlook on such things.

P.S. Technically, even Facter has no concept of returning nil, so to speak, from its facts … Am I right? :)

KW

Updated by Nigel Kersten 4 months ago

  • Assignee changed from Nigel Kersten to Daniel Pittman

Updated by Devon Peters 4 months ago

  • Category set to language

Also available in: Atom PDF