On 2015-24-08 14:38, Martin Alfke wrote:
Hi,
I am playing around with the Puppet 4 Type system and nested hashes.
Is it possible to use the Type system on a hash and check sub hashes as Struct?
Yes, certainly.
# nested hashes
$hash = {
‘ben’ => {
uid => 2204,
home => ‘/home/ben’,
},
‘jones’ => {
uid => 2205,
home => ‘home/jones’,
}
}
# class definition
class users (
Hash[String, Struct[{ uid => Integer, home => Pattern[/^\/.*/]}]] $hash,
){
…
}
# class declaration
class { ‘users’:
hash => $hash,
}
In this case I receive the following error:
Error: Expected parameter 'hash' of 'Class[Users]' to have type Hash[String, Struct[{'uid'=>Integer,
'home'=>Pattern[/^\/.*/]}]], got Struct[{'ben'=>Struct[{'uid'=>Integer, 'home'=>String}],
'jones'=>Struct[{'uid'=>Integer, 'home'=>String}]}] at /root/hash.pp:16 on node
puppetmaster.example.net
That is because you cannot match integer values (such as 2204) with a
Pattern (it only matches strings).
When using the Struct one needs to know the keys. But the keys may be any
arbitrary value.
If you want to support any arbitrary value of any type (including
Undef), use the Any type. If you do not want Undef, you can use
NotUndef[Any]. If you want to be explicit about values of integers and
strings (not being empty strings say), you can use a Variant type - e.g.
Variant[Integer, String] (any string or integer value),
Variant[Integer[0, default], String[1]] (any 0 or positive integer, or
any non empty string). etc.
You can also use the Scalar type if you want to accept non undef single
values (matches any Integer, Float, String, and Regular expression).
Plenty of options depending on how stringent you want the check to be.
Within class users I could use the keys function and a define and check for the
data types within the define.
But I thought that the mentioned approach is valid (at least I haven’t found a
hint that my approach is not valid).
Your approach is valid, you just got tripped up by Integer values not
matching with a Pattern.
Regards
- henrik
--
Visit my Blog "Puppet on the Edge"
http://puppet-on-the-edge.blogspot.se/
--
You received this message because you are subscribed to the Google Groups "Puppet
Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to puppet-dev+unsubscr...@googlegroups.com.
To view this discussion on the web visit
https://groups.google.com/d/msgid/puppet-dev/mrkn62%246jp%241%40ger.gmane.org.
For more options, visit https://groups.google.com/d/optout.