How to print the last element of a slice in a EMQX Kuiper Rules with Go template? [duplicate] - go

This question already has answers here:
How do I get the last element of a slice in a golang template
(3 answers)
Get the last element of an array within a struct in a Golang template [duplicate]
(2 answers)
Closed 1 year ago.
I am using EMQX Kuiper.
https://docs.emqx.io/en/kuiper/latest/rules/overview.html#sinks-actions
Where in my rule settings, I am using dataTemplate property to fetch my data. Where I have to use Go template. No way to write go script only template.
Using this template I able to print first item of my slice.
"dataTemplate": "{{json (index . 0)}}"
But I need to print last item of my slice. Can't print this.
Tried to achieve that with the Go template language like this,
"dataTemplate": "{{$lenMyList := len .}} {{json (index . $lenMyList -1)}}"
But to no avail.

Related

How can I "compile" an HTML template but not "execute" it? [duplicate]

This question already has answers here:
Get output of template to a variable instead to STDOUT [duplicate]
(1 answer)
Assign result of executing text/template template into a variable [duplicate]
(1 answer)
Closed 5 months ago.
I'm using Go HTML templates and when you run this function it sends the data to the client, but I would like to store that data in a variable and send it to the client later. How can this be achieved?
func (t *Template) ExecuteTemplate(wr io.Writer, name string, data any) error
This is for use in an AJAX response. I know on the client side I could simply parse the xhr.responseText, but I need to send some other variables with it.
Use a buffer:
buf:=bytes.Buffer{}
t.ExecuteTemplate(&buf,"name",data)
Then you can use buf.Bytes(), or buf.String().

Xpath subscript returning all nodes, not just the requested one [duplicate]

This question already has answers here:
How to select first element via XPath?
(2 answers)
How to select the first element with a specific attribute using XPath
(9 answers)
Closed 3 years ago.
I'm trying the following XPath:
//*[local-name()='SN102'][1]
Using XPathTester, I saved my scenario
http://www.xpathtester.com/xpath/94ee37e08960247a7bf0619d38c52bee
Not every HL1Loop has a SN102.
Otherwise, I could this:
//*[local-name()='HLLoop1'][1]//*[local-name()='SN102']
I have simplified the sample data down to the following:
<ns0:X12_00401_856 xmlns:ns0="http://schemas.microsoft.com/BizTalk/EDI/X12/2006">
<ns0:HLLoop1>
<ns0:SN1>
<SN102>1</SN102>
<SN103>EA</SN103>
<SN108>AC</SN108>
</ns0:SN1>
</ns0:HLLoop1>
<ns0:HLLoop1>
<ns0:SN1>
<SN102>2</SN102>
<SN103>EA</SN103>
<SN108>AC</SN108>
</ns0:SN1>
</ns0:HLLoop1>
</ns0:X12_00401_856>
The result is coming back with all nodes, not just the first one:
<?xml version="1.0" encoding="UTF-8"?>
<result>
<SN102>1</SN102>
<SN102>2</SN102>
</result>
How do I select the first node only. Seems simply, and I'm sure I've done it before, but not working today.
I have a "Vendor Simulator" program that is building fake 856 data to send back, and I want to increase the first quantity only to force some error handling logic.
Just select the first element of the whole nodelist
(//*[local-name()='SN102'])[1]
The original query //*[local-name()='SN102'][1] would have selected the first SN102 if there had been several siblings of the same name.

Expanding IP ranges into single IP addresses using Linux bash command? [duplicate]

This question already has answers here:
bash - for loop for IP range excluding certain IPs
(2 answers)
IP range generator script [closed]
(1 answer)
Closed 5 years ago.
Which is the best method for me to handle this? I'm thinking awk might be the way to go perhaps, but not sure where to start even.
I have a text file, which contains IP addresses and ranges as per this example:
10.10.115.69
10.10.128.6 - 10.10.128.7
10.10.128.20
10.10.128.28
10.10.128.38 - 10.10.128.53
10.10.128.70 - 10.10.128.71
10.10.128.130 - 10.10.128.144
10.10.128.232 - 10.10.128.233
10.10.130.5
10.10.132.5
I'm trying to get them all into their own individual address. So as per above on the line '10.10.128.38 - 10.10.128.53' .. I would want that to convert to:
Expected output:
10.10.128.20
10.10.128.28
10.10.128.38
10.10.128.39
10.10.128.40
<snip>
10.10.128.52
10.10.128.53
10.10.128.70
10.10.128.71
<etc.>
Keeping of course the single host IPs included into output, just expanding the range parts.
Hope that makes sense.
Edit: Comments are suggesting this is a duplicate of another exact question. Please could someone link to it for me, as I'm not finding it. I am new here so not quite up to speed with things.
My expected output is as above, I'm not after a generator, rather a way to expand the ranges, the lines with '-' in them <start IP> - <end_IP> while keeping the single ones that are there still.

gob not decoding as expected [duplicate]

This question already has an answer here:
Unable to decode gob data
(1 answer)
Closed 5 years ago.
Here is my playground where I am trying to serialize a list of structures and read back from the file.
https://play.golang.org/p/8x0uciOd1Sq
I was expecting the object to be decoded successfully. What am I doing wrong?
Export the field names by capitalizing the first letter.
type REntry struct {
Key string
Value []string
}
The gob package and other similar packages ignore unexported fields.

VBScript readline from file and check equal to [duplicate]

This question already has an answer here:
VBScript equals problems
(1 answer)
Closed 8 years ago.
I am working on a VBScript login/signup program I already have the signup part done but then while logging in it has to read a line from a file with ReadLine() but the file must see if the line read and the text typed are equal variables and I don't know how to do this
For simple cases, the = operator
If sInput = sRead Then
...
Else
...
End If
works well; if you have to care for case(in)sensitivity, use StrComp().
The comparison is not affected by the way you obtained the strings. If your file justs contains the string that has to be matched,
sRead = tsIn.ReadLine()
before the comparisons will 'work'; if your file contains more than that, you'll have to publish (relevant parts of) its content and how the relevant data can be identified.

Resources