2020-04-06 00:44:45 +05:30
|
|
|
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"
|
2020-04-06 00:44:45 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// CompileExtractors performs the initial setup operation on a extractor
|
|
|
|
|
func (e *Extractor) CompileExtractors() error {
|
2020-07-15 00:47:01 +02:00
|
|
|
var ok bool
|
|
|
|
|
// Setup the extractor type
|
|
|
|
|
e.extractorType, ok = ExtractorTypes[e.Type]
|
2020-04-06 00:44:45 +05:30
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-06 00:44:45 +05:30
|
|
|
// Setup the part of the request to match, if any.
|
2020-12-24 12:13:18 +05:30
|
|
|
if e.Part == "" {
|
|
|
|
|
e.Part = "body"
|
2020-04-06 00:44:45 +05:30
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|