add flatten to manipulate arrays of arrays

This commit is contained in:
bui 2023-07-20 17:10:01 +02:00
parent 54fd2e4e70
commit b33ba277bf
4 changed files with 29 additions and 2 deletions

View file

@ -20,6 +20,11 @@ var exprFuncs = []exprCustomFunc{
new(func(string) (*cticlient.SmokeItem, error)),
},
},
{
name: "Flatten",
function: Flatten,
signature: []interface{}{},
},
{
name: "Distance",
function: Distance,

View file

@ -8,6 +8,7 @@ import (
"net/url"
"os"
"path"
"reflect"
"regexp"
"strconv"
"strings"
@ -170,7 +171,27 @@ func FileInit(fileFolder string, filename string, fileType string) error {
return nil
}
//Expr helpers
// Expr helpers
func Flatten(params ...any) (any, error) {
return flatten(nil, reflect.ValueOf(params)), nil
}
func flatten(args []interface{}, v reflect.Value) []interface{} {
if v.Kind() == reflect.Interface {
v = v.Elem()
}
if v.Kind() == reflect.Array || v.Kind() == reflect.Slice {
for i := 0; i < v.Len(); i++ {
args = flatten(args, v.Index(i))
}
} else {
args = append(args, v.Interface())
}
return args
}
// func Get(arr []string, index int) string {
func Get(params ...any) (any, error) {

View file

@ -9,7 +9,7 @@ const PAPIVersion = "v1"
const PAPIPollUrl = "/decisions/stream/poll"
const PAPIPermissionsUrl = "/permissions"
const CAPIBaseURL = "https://api.crowdsec.net/"
const CAPIBaseURL = "https://api.dev.crowdsec.net/"
const CscliOrigin = "cscli"
const CrowdSecOrigin = "crowdsec"

View file

@ -155,6 +155,7 @@ func (w WaapEvent) ByTagRx(rx string) WaapEvent {
}
for _, rule := range w {
for _, tag := range rule["tags"].([]string) {
log.Infof("ByTagRx: %s = %s -> %t", rx, tag, re.MatchString(tag))
if re.MatchString(tag) {
waap = append(waap, rule)
break