map each item in list with a format in make - makefile

So I've got this variable:
ITEMS = item1 item2
and I want a function to do something like this:
ITEMS_AFTER := $(fmt prefix{}suffix, $(ITEMS))
so it will look like:
prefixitem1suffix prefixitem2suffix
how would I do it?
I hope i have a shorter way than using addprefix combined with addsuffix.

You can use foreach:
ITEMS_AFTER := $(foreach I,$(ITEMS),prefix$Isuffix)
or you can use patsubst:
ITEMS_AFTER := $(patsubst %,prefix%suffix,$(ITEMS))

Related

Go template - syntax for range

In Go template, I have a map setup like this:
{{$key}}map := make(map[string]interface{})
And I want to iterate through the map using this:
{{ range $mapKey, $mapValue := {{$key}}map}}
And I am getting this error:
unexpected "{" in range
Looks like it does not allow nested {{}} inside another {{}}. Is there anyway I can solve this issue ???
You cannot generate variable names to be used in templates using the template engine itself. You seem to be in need of having multiple maps, one for each $key. So, use a map of maps:
m := make(map[string]map[string]interface{})
where m[key] gives the map for the key.
Then you can do:
{{ range $mapKey, $mapValue := (index $.m $.key)}}
...
{{end}}

Substitute a variable in regexp.MatchString method

I have a golang script that needs to create and look for a particular regex. The string to look for id defined as a constant.
const nameRegex = "service-route"
I can use this variable in some places.
rb := &compute.Route{
Name: fmt.Sprintf("%s-%s", nameRegex, generateCode(host))
I would like to use the same string to find aswell.
Basically I have something like
matched, _ := regexp.MatchString("^service-route-.*", route.Name)
if matched {
Doing something like
matched, _ := regexp.MatchString("^%s-.*" , nameRegex, route.Name)
does not work as the function MatchString requires only 1 argument.
I tried something like
myRegex , err := regexp.Compile("%s", nameRegex)
myRegex.MatchString(route.Name)
that too does not work.
Is it even possible to use a variable to match a regex ?
The 1st parameter to MatchString is a string. So use Sprintf (as you did earlier) to generate the pattern string, something like this:
regexp.MatchString(fmt.Sprintf("^%s-.*", nameRegex), route.Name)
or construct the string using concatentation:
regexp.MatchString("^" + nameRegex + "-.*", route.Name)
This seems to be a one-off check, so there is not need to pre-compile the regex.
It is possible. Here is go playground : https://play.golang.org/p/hc9eMcSzGQC

Insert -l to each variable of set in makefile

I write makefile.
I have a variable set, which is the library I want to link to:
LIBS = core highgui
I want to make LIBS a VAR in some way:
#...some way, input is LIBS, output is VAR
VAR = -lcore -lhighgui
What is the some way?
Nothing is easier, just use addprefix:
VAR = $(addprefix -l,$(LIBS))

Kubernetes go client: list events

I am trying to get a list of events in the namespace, but with or without FieldSelector I get an empty list. Is this the right way to do it?
eventListOptions := metav1.ListOptions{FieldSelector: fields.OneTermEqualSelector("involvedObject.name", job.Name).String()}
jobEvents, _ := clientset.EventsV1beta1().Events(GetNamespace()).List(eventListOptions)
If you print error return by List, you should get error something like "involvedObject.name" is not a known field selector: only "metadata.name", "metadata.namespace"
use CoreV1 instead of EventsV1beta1
The line will be something like below:
jobEvents, _ := clientset.CoreV1().Events(GetNamespace()).List(eventListOptions)
"involvedObject.name", job.Name isn't supported by EventsV1beta1
Hope it'll help.

xs:string as element() in XQuery

Let's assume I have a XML files, one like this:
<SampleF>
<FirstNode AAA="Something" BBB="Something"></FirstNode>
<SecondNode CCC="Random" DDD="Random"></SecondNode>
</SampleF>
And second one like this:
<SampleF2>
<FirstNode>
<AAA>Something</AAA>
<BBB>Random</BBB>
</FirstNode>
</SampleF2>
And I would like to obtain from both (AAA="Something"/Something) of them as element(). How do I convert it? When in first case I get xs:string and in the second document-node().
I made something like this for the first example but I'm 100% certain there is a better way of doing this
declare function getElementFirstExample($message) as element() {
let $name := "AAA"
let $value := $message/*:SampleF1/*:FirstNode/#AAA
return element {$name} {"$value"}
};
Thank you in advance for your help and advices.
As I understand, you want the value of <FirstNodes/>s AAA attribute or child element, no matter whether it is in the element or attribute.
Use an alternative axis step for accessing both the attribute and element, and data(...) to retrieve the string value.
data(//FirstNode/(#AAA, AAA))
Putting this together for your function and explicit use case:
declare function getElementFirstExample($message) as element() {
let $name := "AAA"
let $value := data($message/*:SampleF1/*:FirstNode/(#AAA, *:AAA))
return element {$name} {"$value"}
};

Resources