Substitute a variable in regexp.MatchString method - go

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

Related

Non-escaped query parameters for URL

I want to create a query parameter without escaping the query string.
For example, I need to create a query parameter q with the content
"before:{2019-12-24 19:57:34}"
so that the URL is
https://android-review.googlesource.com/changes/?q=before:{2019-12-24 19:57:34}
If I use this code (Golang Playground)
url, _ := url.Parse("https://android-review.googlesource.com/changes/")
q := url.Query()
q.Set("q", "before:{2019-12-24 19:57:34}")
url.RawQuery = q.Encode()
fmt.Println(url)
url is escaping the special characters, spaces, and brackets:
https://android-review.googlesource.com/changes/?q=before%3A%7B2019-12-24+19%3A57%3A34%7D
How can I solve this issue except manually creating the URL (without query parameters then)?
If you don't want your URL query to be encoded, then don't use the Encode() method. Instead, set the RawQuery value directly, yourself:
url, _ := url.Parse("https://android-review.googlesource.com/changes/")
url.RawQuery = "q=before:{2019-12-24 19:57:34}"
fmt.Println(url)
Output:
https://android-review.googlesource.com/changes/?q=before:{2019-12-24 19:57:34}
playground
Keep in mind, however, that this is a recipe for potential disaster, depending on how that url is eventually used. In particular, the space in that URL should be escaped, according to RFC. See more here.
Perhaps you'll want to implement your own minimal escaping, if that's compatible with your use-case.

Ansible : how to pass "{{ABC}}" as a string

Under my jinja file i ve this line
- mockString= "{{ABC}}"
My mockString must have the exacte value "{{ABC}}" where the first letter is " , the second is { the third is also { ect...
Now when converting my template file ; it seems that i sees "{{ABC}}" as a variable , where it tries to interpret and change by its value , what is not my purpose.
How may i pass it as a simple string
Suggestions ??
Try passing it like this {{'"{{ABC}}"'}} this should work
you can try like this :
mockString= \"{{ACB}}\"

Use embedded string as variable name

I have a YAML file that uses the encoding __firstname__ as a placeholder which signifies that an existing method firstname should be used, rather than the literal string in a subsequent process.
I am trying to understand the most ruby way to to do this. Basically, I need to extract the part between the underscores and send it to an object. Here is pseudocode:
variable = '__firstname__'
if variable is prefixed and suffixed with underscores
result = object.send(variable.removeunderscores)
else
result = variable
end
puts result
I was about to write this procedurally like this, but this is the type of thing that I think ruby can less clunkily if only I knew the language better.
What is a clean why to write this?
There's nothing wrong with verbose code if it's clear to read IMO.
I'd do something like this using String#start_with? and String#end_with?:
variable = '__firstname__'
if variable.start_with?("__") && variable.end_with?("__")
result = object.send(variable[2...-2])
else
result = variable
end

GoREST endpoint path

I'm writting a web service with Go and I'd like to have url like :
http://example.com/WEB/service.wfs?param1=2&param2=test.....
I'm using GoREST and my Endpoint url is :
method:"GET" path:"/WEB/service.wfs?{param:string}" output:"string"
My problem is that it never return the "param" but it does if I use the endpoint :
method:"GET" path:"/WEB/service.wfs/{param:string}" output:"string"
Is there a way to handle the "?" ?
You can do this in gorest though it's not as nice as gorest's preferred mechanism.
Don't include your query parameters in your endpoint definition
method:"GET" path:"/WEB/service.wfs" output:"string"
Instead, you can get at the context from your registered end point and get the query parameters using something like
func (serv MyService) HelloWorld() (result string) {
r := serv.Context.Request()
u, _ := url.Parse(r.URL.String())
q := u.Query()
result = "Buono estente " + q["hi"][0]
return
}
I have had a look at the GoREST package you are using and can not see any way of doing this.
I have always used gorillatoolkit pat package.
gorillatoolkit
There is an example of what you want to do about half way down.
category := req.URL.Query().Get(":category")
This way you can get the query parameters on the request URL by the key.
Hope this helps.

XPATH SELENIUM=Cannot add variable

I am trying to add a variable to an xpath but to no avail
This works below for java
assertTrue("Failed", verifyElementPresent("//*[#class='StdLJText' and contains(.,'2 Employees selected')]"));
but when I add a variable , like below it does not
String CountEmp1="2";
assertTrue("Failed", verifyElementPresent("//*[#class='StdLJText' and contains(.,CountEmp1+'Employees selected')]"));
You need to put CountEmp1 as a string addition in the main verifyElePresent func i.e verifyElementPresent("//*[#class='StdLJText' and contains(.,"+CountEmp1+"Employees selected')]"));
Note the replacement of "+CountEmp1+" at contains(.,CountEmp1+'
With your xpath the verifyElementPresent method basically looks for //*[#class='StdLJText' and contains(.,CountEmp1+'Employees selected')] where countEmp1 is taken as string value and not something that needs to be concatenadated.

Resources