Ask Your Question

Optional Keys in Structs?

asked 2017-07-19 06:31:51 -0500

Michael Kluge gravatar image

updated 2017-07-20 03:24:14 -0500

Hi there,

is there any way to define that a Struct[] function parameter that can have more than the defined fields? I'm thinking of something like this:

    'channel' => Array,
    'context' => String,
    'switchtype' => Enum['euroisdn'],
    'signalling' => Enum['pri_cpe', 'pri_net'],
    String => Optional[String]

My use case is to strictly ask for these parameters but leave it open to the user to define more (which, at the end, will be put into the config file I'm managing).

Greetings, Michael

edit retag flag offensive close merge delete


You're writing a function? Perhaps: Maybe you can use a Hash[] instead, where String[]s are keys and Any[] are acceptable values?

Kai Burghardt gravatar imageKai Burghardt ( 2017-07-20 06:43:30 -0500 )edit

2 Answers

Sort by » oldest newest most voted

answered 2017-07-20 02:28:15 -0500

updated 2017-07-20 02:30:14 -0500

I believe so yes - have a look at the documentation -> struct doc

Struct[{mode            => Enum[read, write, update],
        path            => Optional[String[1]],
        Optional[owner] => String[1]}]

In this data type, the owner key can be absent, but if it’s present, it must be a string; a value of undef isn’t allowed.

Struct[{mode            => Enum[read, write, update],
        path            => Optional[String[1]],
        NotUndef[owner] => Optional[String[1]]}]

In this data type, the owner key is mandatory, but it allows an explicit undef value.

edit flag offensive delete link more

answered 2017-07-20 03:30:04 -0500

Michael Kluge gravatar image

The documentation points out, that specific, named keys can be optional by using 'key' => Optional[...], but thats not what I'm asking. I'm asking for optional unnamed keys. For example:


class 'asterisk' (
        'channel' => Array,
        String => Optional[String]
    }] $pri_config = $asterisk::params::pri_config


class {'asterisk':
    pri_config => {
        'channel' => ['1-2', '4-5'],
        'switchtype' => 'euroisdn',
         '...' => '...'

So, at the end the 'channel' is mandatory but the user can add more values as needed.

edit flag offensive delete link more



so you just want the user to have like a variable amount of extra options to give? like say you give in mode: '755'. It would be added as a key for you pri_config?I guess in that case I would gues a hash variable would be more in place and you'd have to process the variables in it to match keys

negast gravatar imagenegast ( 2017-07-20 04:03:42 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2017-07-19 06:31:51 -0500

Seen: 48 times

Last updated: Jul 20