getclassmetatags("myclass", "foo") and getvariablemetatags("myvar", "foo")

7 views
Skip to first unread message

Ted Zlatanov

unread,
Jul 12, 2016, 12:08:27 PM7/12/16
to dev-cf...@googlegroups.com
Right now, getclassmetatags("myclass") and getvariablemetatags("myvar")
return an slist of all the tags for their respective class or variable.

In 3.9.0, I found myself needing a specific tag value instead of all of
them, so I was doing

regex_replace(nth(grep("^foo=.*", getclassmetatags("myclass")), 0), "^foo=", "", "T")

to get the value of the "foo=bar" tag of "myclass". So I would love to
have an optional second argument getclassmetatags("myclass", "foo") and
getvariablemetatags("myvar", "foo") that give me a slist of the specific
tag values, removing the key. What do you think? It would be backwards
compatible and fairly easy to implement.

Ted

Kristian Amlie

unread,
Jul 13, 2016, 2:35:08 AM7/13/16
to dev-cf...@googlegroups.com
Would it need to be an slist? If you ask for a specific tag you'll get
just one value, no? Or do you mean that it should be an expression?

I guess this makes sense, but just out of curiosity, can you share some
more about why you needed this, if it's not too secret? :-)

--
Kristian

Ted Zlatanov

unread,
Jul 13, 2016, 6:07:05 AM7/13/16
to dev-cf...@googlegroups.com
On Wed, 13 Jul 2016 08:35:04 +0200 Kristian Amlie <kristia...@cfengine.com> wrote:

KA> On 12/07/16 18:08, Ted Zlatanov wrote:
>> Right now, getclassmetatags("myclass") and getvariablemetatags("myvar")
>> return an slist of all the tags for their respective class or variable.
>>
>> In 3.9.0, I found myself needing a specific tag value instead of all of
>> them, so I was doing
>>
>> regex_replace(nth(grep("^foo=.*", getclassmetatags("myclass")), 0), "^foo=", "", "T")
>>
>> to get the value of the "foo=bar" tag of "myclass". So I would love to
>> have an optional second argument getclassmetatags("myclass", "foo") and
>> getvariablemetatags("myvar", "foo") that give me a slist of the specific
>> tag values, removing the key. What do you think? It would be backwards
>> compatible and fairly easy to implement.

KA> Would it need to be an slist? If you ask for a specific tag you'll get
KA> just one value, no? Or do you mean that it should be an expression?

You'd get an slist, because you can have meta => { "foo=1", "foo=2" } so
the function signature won't change.

KA> I guess this makes sense, but just out of curiosity, can you share some
KA> more about why you needed this, if it's not too secret? :-)

It was for https://github.com/cfengine/masterfiles/pull/766 but I think
it's a generally useful extension.

Ted

Nick Anderson

unread,
Jul 13, 2016, 12:03:10 PM7/13/16
to dev-cfengine
What would you expect to get when you have a tag without = in it?

"myvar" string => "value", meta => { "inventory", "attribute_name=my special inventory thing" };

"inventory_tags" slist => getvariablemetatags("myvar", "inventory");

An empty list?
A list with a single element of empty string? { "" }
Skip defining the variable? <- my preference


I can see this being useful.

Ted Zlatanov

unread,
Jul 13, 2016, 12:27:35 PM7/13/16
to dev-cf...@googlegroups.com
On Wed, 13 Jul 2016 09:03:09 -0700 (PDT) Nick Anderson <nick.a...@cfengine.com> wrote:

NA> What would you expect to get when you have a tag without = in it?
NA> "myvar" string => "value", meta => { "inventory", "attribute_name=my special inventory thing" };

NA> "inventory_tags" slist => getvariablemetatags("myvar", "inventory");

NA> An empty list?
NA> A list with a single element of empty string? { "" }
NA> Skip defining the variable? <- my preference

The function will fail if the variable doesn't exist. I don't think it
should fail otherwise, since `meta` attributes only get set once.

If you request key "inventory" but there is no tag starting with
"inventory=" then I think you should get an empty list.

Ted

Nick Anderson

unread,
Jul 13, 2016, 12:30:46 PM7/13/16
to dev-cf...@googlegroups.com
On 07/13/2016 11:27 AM, Ted Zlatanov wrote:
> If you request key "inventory" but there is no tag starting with
> "inventory=" then I think you should get an empty list.

Why do you prefer an empty list to not defining the variable?




signature.asc

Ted Zlatanov

unread,
Jul 13, 2016, 12:32:55 PM7/13/16
to dev-cf...@googlegroups.com
On Wed, 13 Jul 2016 11:30:39 -0500 Nick Anderson <nick.a...@cfengine.com> wrote:

NA> On 07/13/2016 11:27 AM, Ted Zlatanov wrote:
>> If you request key "inventory" but there is no tag starting with
>> "inventory=" then I think you should get an empty list.

NA> Why do you prefer an empty list to not defining the variable?

If you fail the function call, it will keep getting retried, and `meta`
tags don't change after the variable is created.

Why does it matter? Maybe you have a specific case in mind?

Ted

Nick Anderson

unread,
Jul 13, 2016, 12:34:30 PM7/13/16
to dev-cf...@googlegroups.com
On 07/13/2016 11:32 AM, Ted Zlatanov wrote:
> If you fail the function call, it will keep getting retried, and `meta`
> tags don't change after the variable is created.

Makes sense.

> Why does it matter? Maybe you have a specific case in mind?

I was mostly thinking about avoiding empty lists. They have been
problematic before.

signature.asc

Ted Zlatanov

unread,
Jul 13, 2016, 12:56:07 PM7/13/16
to dev-cf...@googlegroups.com
On Wed, 13 Jul 2016 11:34:29 -0500 Nick Anderson <nick.a...@cfengine.com> wrote:

NA> On 07/13/2016 11:32 AM, Ted Zlatanov wrote:

>> Why does it matter? Maybe you have a specific case in mind?

NA> I was mostly thinking about avoiding empty lists. They have been
NA> problematic before.

Yeah, I know what you mean. I think it will be OK. Do I have your vote
and Kristian's? Implementing this should be fairly simple.

Ted

Nick Anderson

unread,
Jul 13, 2016, 1:16:39 PM7/13/16
to dev-cf...@googlegroups.com
On 07/13/2016 11:55 AM, Ted Zlatanov wrote:
> Yeah, I know what you mean. I think it will be OK. Do I have your vote
> and Kristian's? Implementing this should be fairly simple.

Yes you have mine.

signature.asc

Ted Zlatanov

unread,
Jul 13, 2016, 2:37:58 PM7/13/16
to dev-cf...@googlegroups.com
On Wed, 13 Jul 2016 12:16:33 -0500 Nick Anderson <nick.a...@cfengine.com> wrote:

NA> On 07/13/2016 11:55 AM, Ted Zlatanov wrote:
>> Yeah, I know what you mean. I think it will be OK. Do I have your vote
>> and Kristian's? Implementing this should be fairly simple.

NA> Yes you have mine.

https://github.com/cfengine/core/pull/2647

Kristian Amlie

unread,
Jul 14, 2016, 4:09:47 AM7/14/16
to Nick Anderson, dev-cf...@googlegroups.com
You have mine as well. FTR I think empty list is better, it's not a
failed function call, it's a successful one that returned no results.

As usual in my recent schedule, I will have to defer the review to
Dimitrios, but I can help if there are specific questions.

--
Kristian
Reply all
Reply to author
Forward
0 new messages