2020-04-04 02:50:32 +05:30
|
|
|
package templates
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"os"
|
|
|
|
|
|
|
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// ParseTemplate parses a yaml request template file
|
|
|
|
|
func ParseTemplate(file string) (*Template, error) {
|
|
|
|
|
template := &Template{}
|
|
|
|
|
|
|
|
|
|
f, err := os.Open(file)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = yaml.NewDecoder(f).Decode(template)
|
|
|
|
|
if err != nil {
|
|
|
|
|
f.Close()
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
f.Close()
|
|
|
|
|
|
2020-04-06 00:44:45 +05:30
|
|
|
// Compile the matchers and the extractors
|
2020-04-04 02:50:32 +05:30
|
|
|
for _, request := range template.Requests {
|
|
|
|
|
for _, matcher := range request.Matchers {
|
|
|
|
|
if err = matcher.CompileMatchers(); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-04-06 00:44:45 +05:30
|
|
|
|
|
|
|
|
for _, extractor := range request.Extractors {
|
|
|
|
|
if err := extractor.CompileExtractors(); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-04-04 02:50:32 +05:30
|
|
|
}
|
|
|
|
|
return template, nil
|
|
|
|
|
}
|