56 lines
1.2 KiB
Go
Raw Normal View History

package extractors
import (
"fmt"
"regexp"
2021-01-12 11:21:32 +05:30
"strings"
2021-08-01 14:42:04 +02:00
"github.com/itchyny/gojq"
)
2021-09-07 17:31:46 +03:00
// CompileExtractors performs the initial setup operation on an extractor
func (e *Extractor) CompileExtractors() error {
2020-07-15 00:47:01 +02:00
var ok bool
2021-09-07 17:31:46 +03:00
// Set up the extractor type
2020-07-15 00:47:01 +02:00
e.extractorType, ok = ExtractorTypes[e.Type]
if !ok {
return fmt.Errorf("unknown extractor type specified: %s", e.Type)
}
// Compile the regexes
for _, regex := range e.Regex {
compiled, err := regexp.Compile(regex)
if err != nil {
return fmt.Errorf("could not compile regex: %s", regex)
}
e.regexCompiled = append(e.regexCompiled, compiled)
}
2021-01-12 11:21:32 +05:30
for i, kval := range e.KVal {
e.KVal[i] = strings.ToLower(kval)
}
2021-08-01 14:42:04 +02:00
for _, query := range e.JSON {
2021-07-31 22:49:23 +02:00
query, err := gojq.Parse(query)
if err != nil {
return fmt.Errorf("could not parse json: %s", query)
}
compiled, err := gojq.Compile(query)
if err != nil {
return fmt.Errorf("could not compile json: %s", query)
}
e.jsonCompiled = append(e.jsonCompiled, compiled)
}
if e.CaseInsensitive {
if e.Type != "kval" {
return fmt.Errorf("case-insensitive flag is supported only for 'kval' extractors (not '%s')", e.Type)
}
for i := range e.KVal {
e.KVal[i] = strings.ToLower(e.KVal[i])
}
}
return nil
}