0

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.

Saiful Islam
  • 186
  • 1
  • 3
  • 13
  • 1
    Why not just use a `template.FuncMap` that includes a `lastElement` or similar? – sberry Oct 04 '21 at 17:24
  • 1
    Here is an example showing a couple ways to go about using a function for this. https://play.golang.org/p/opm9NS4BYYi – sberry Oct 04 '21 at 17:42
  • Hi @sberry thanks for reply, as I know there is no option to write any script only have to use template. – Saiful Islam Oct 04 '21 at 18:10
  • 1
    @SaifulIslam It is not possible to get the last element of a slice without custom functions. According to the [EMQX Kuiper documentation](https://docs.emqx.io/en/kuiper/latest/rules/overview.html#sinks-actions), the [sprig add function](http://masterminds.github.io/sprig/math.html) is available in the template. Use it like this: `index . (add $lenMyList -1)`. – Charlie Tumahai Oct 04 '21 at 18:18
  • Thanks a lot @CeriseLimón. It works perfectly. – Saiful Islam Oct 05 '21 at 04:43

0 Answers0