On 2015-26-08 17:56, Martin Alfke wrote:
Hi Henrik,
On 26 Aug 2015, at 17:49, Henrik Lindberg <henrik.lindb...@cloudsmith.com> 
wrote:

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,
){
…
}

That is because you cannot match integer values (such as 2204) with a Pattern 
(it only matches strings).

??

Doh, I misread.

Hash[String, Struct[{ uid => Integer, home => Pattern[/^\/.*/]}]] $hash

Am I missing something?
Within the Struct I provide the key name (uid) and Type (Integer) and the 
second key (home) with Type (Pattern).

Yes, you are indeed. Your regular expression is however not correct for the home directories that you have given. The regexp /^\/.*/ requires that the string starts with a slash. If you change your input to say

home => '/home/jones'

it should work.

Or change the regular expression. The ^ means anchor at start of string, and \/ means a slash is required, then 0 or more characters. If you accept any String, simply use String. If you want to ensure that it is a valid path you need a way more complex regular expression.

Were you attempting to specify "not allowed to start with a slash"?
If so, try Pattern[/^[^\/]+.*/]


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.

Good to know…

Sorry that I went of on a tangent after misreading the error message.
Hope the suggestions above will work out better for you.

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/mrlkee%244st%241%40ger.gmane.org.
For more options, visit https://groups.google.com/d/optout.

Reply via email to