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)), new(func(string) (*cticlient.SmokeItem, error)),
}, },
}, },
{
name: "Flatten",
function: Flatten,
signature: []interface{}{},
},
{ {
name: "Distance", name: "Distance",
function: Distance, function: Distance,

View file

@ -8,6 +8,7 @@ import (
"net/url" "net/url"
"os" "os"
"path" "path"
"reflect"
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
@ -170,7 +171,27 @@ func FileInit(fileFolder string, filename string, fileType string) error {
return nil 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(arr []string, index int) string {
func Get(params ...any) (any, error) { func Get(params ...any) (any, error) {

View file

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

View file

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