feat(extraction): add support to parse VName rules from any io.Reader
@@ -23,7 +23,7 @@ import (
"encoding/json"
"fmt"
"io"
- "io/ioutil"
+ "os"
"regexp"
"strings"
@@ -127,7 +127,10 @@ func expectDelim(de *json.Decoder, expected json.Delim) error {
return nil
}
-// ParseRules parses Rules from JSON-encoded data in the following format:
+// ParseRules reads Rules data from a byte array.
+func ParseRules(data []byte) (Rules, error) { return ReadRules(bytes.NewReader(data)) }
+
+// ReadRules parses Rules from JSON-encoded data in the following format:
//
// [
// {
@@ -143,8 +146,8 @@ func expectDelim(de *json.Decoder, expected json.Delim) error {
// Each pattern is an RE2 regexp pattern. Patterns are implicitly anchored at
// both ends. The template strings may contain markers of the form @n@, that
// will be replaced by the n'th regexp group on a successful input match.
-func ParseRules(data []byte) (Rules, error) {
- de := json.NewDecoder(bytes.NewReader(data))
+func ReadRules(r io.Reader) (Rules, error) {
+ de := json.NewDecoder(r)
// Check for start of array.
if err := expectDelim(de, '['); err != nil {
@@ -187,13 +190,10 @@ func LoadRules(path string) (Rules, error) {
if path == "" {
return nil, nil
}
- data, err := ioutil.ReadFile(path)
- if err != nil {
- return nil, fmt.Errorf("reading vname rules: %v", err)
- }
- rules, err := ParseRules(data)
+ f, err := os.Open(path)
if err != nil {
- return nil, fmt.Errorf("parsing vname rules: %v", err)
+ return nil, fmt.Errorf("opening vname rules file: %v", err)
}
- return rules, nil
+ defer f.Close()
+ return ReadRules(f)
}
