forked from tomnomnom/gron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pkg.go
43 lines (35 loc) · 746 Bytes
/
pkg.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package pkg
import (
"fmt"
"io"
"regexp"
)
var linePattern = regexp.MustCompile("^json\\.([^=]+) = (.+);$")
func Gron(r io.Reader) (map[string]string, error) {
resultMap := make(map[string]string)
ss, err := statementsFromJSON(r, statement{{"json", typBare}})
if err != nil {
goto out
}
for i, s := range ss {
if i == 0 {
continue
}
line := statementToString(s)
matches := linePattern.FindStringSubmatch(line)
if matches == nil {
err = fmt.Errorf("could not parse line: %s", line)
goto out
}
key := matches[1]
value := matches[2]
if value != "{}" && value != "[]" {
resultMap[key] = value
}
}
out:
if err != nil {
return nil, fmt.Errorf("failed to Gron: %s", err)
}
return resultMap, nil
}