2020-05-15 09:39:16 +00:00
|
|
|
package exprhelpers
|
|
|
|
|
|
|
|
import (
|
2020-05-27 14:31:08 +00:00
|
|
|
"bufio"
|
2020-05-28 08:59:43 +00:00
|
|
|
"fmt"
|
2020-05-27 14:31:08 +00:00
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"regexp"
|
2020-05-15 09:39:16 +00:00
|
|
|
"strconv"
|
2020-05-22 11:55:48 +00:00
|
|
|
"strings"
|
2020-05-15 09:39:16 +00:00
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2020-05-27 14:31:08 +00:00
|
|
|
var dataFile map[string][]string
|
|
|
|
var dataFileRegex map[string][]*regexp.Regexp
|
|
|
|
|
2020-05-15 09:39:16 +00:00
|
|
|
func Atof(x string) float64 {
|
|
|
|
log.Debugf("debug atof %s", x)
|
|
|
|
ret, err := strconv.ParseFloat(x, 64)
|
|
|
|
if err != nil {
|
|
|
|
log.Warningf("Atof : can't convert float '%s' : %v", x, err)
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2020-06-01 14:12:48 +00:00
|
|
|
func Upper(s string) string {
|
|
|
|
return strings.ToUpper(s)
|
2020-05-22 11:55:48 +00:00
|
|
|
}
|
|
|
|
|
2020-05-15 09:39:16 +00:00
|
|
|
func GetExprEnv(ctx map[string]interface{}) map[string]interface{} {
|
2020-05-29 15:25:09 +00:00
|
|
|
var ExprLib = map[string]interface{}{
|
2020-06-01 14:12:48 +00:00
|
|
|
"Atof": Atof,
|
|
|
|
"JsonExtract": JsonExtract,
|
2020-05-29 15:25:09 +00:00
|
|
|
"JsonExtractLib": JsonExtractLib,
|
2020-06-01 14:12:48 +00:00
|
|
|
"File": File,
|
|
|
|
"RegexpInFile": RegexpInFile,
|
|
|
|
"Upper": Upper,
|
2020-05-29 15:25:09 +00:00
|
|
|
}
|
2020-05-15 09:39:16 +00:00
|
|
|
for k, v := range ctx {
|
|
|
|
ExprLib[k] = v
|
|
|
|
}
|
|
|
|
return ExprLib
|
|
|
|
}
|
2020-05-27 14:31:08 +00:00
|
|
|
|
|
|
|
func Init() error {
|
|
|
|
dataFile = make(map[string][]string)
|
|
|
|
dataFileRegex = make(map[string][]*regexp.Regexp)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func FileInit(fileFolder string, filename string, fileType string) error {
|
|
|
|
filepath := path.Join(fileFolder, filename)
|
|
|
|
file, err := os.Open(filepath)
|
|
|
|
if err != nil {
|
2020-05-27 15:56:52 +00:00
|
|
|
return err
|
2020-05-27 14:31:08 +00:00
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
|
|
|
|
if _, ok := dataFile[filename]; !ok {
|
|
|
|
dataFile[filename] = []string{}
|
|
|
|
}
|
|
|
|
if fileType == "" {
|
|
|
|
fileType = "string"
|
|
|
|
}
|
|
|
|
scanner := bufio.NewScanner(file)
|
|
|
|
for scanner.Scan() {
|
|
|
|
switch fileType {
|
2020-05-27 15:56:52 +00:00
|
|
|
case "regex", "regexp":
|
2020-05-27 14:31:08 +00:00
|
|
|
dataFileRegex[filename] = append(dataFileRegex[filename], regexp.MustCompile(scanner.Text()))
|
|
|
|
case "string":
|
|
|
|
dataFile[filename] = append(dataFile[filename], scanner.Text())
|
|
|
|
default:
|
2020-05-28 08:59:43 +00:00
|
|
|
return fmt.Errorf("unknown data type '%s' for : '%s'", fileType, filename)
|
2020-05-27 14:31:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := scanner.Err(); err != nil {
|
2020-05-27 15:56:52 +00:00
|
|
|
return err
|
2020-05-27 14:31:08 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func File(filename string) []string {
|
|
|
|
if _, ok := dataFile[filename]; ok {
|
|
|
|
return dataFile[filename]
|
|
|
|
}
|
|
|
|
log.Errorf("file '%s' not found for expr library", filename)
|
|
|
|
return []string{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func RegexpInFile(data string, filename string) bool {
|
|
|
|
if _, ok := dataFileRegex[filename]; ok {
|
|
|
|
for _, re := range dataFileRegex[filename] {
|
|
|
|
if re.Match([]byte(data)) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Errorf("file '%s' not found for expr library", filename)
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|