-
Notifications
You must be signed in to change notification settings - Fork 69
/
detail.go
42 lines (37 loc) · 851 Bytes
/
detail.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
package parse
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"regexp"
)
type Detail struct {
Key string
Value *Value
}
func parseDetail(b []byte, detailregex *regexp.Regexp) (*Detail, error) {
detail, err := getok(detailregex.FindSubmatch(b), 1)
if err != nil {
panic("silk: failed to parse detail: " + err.Error())
}
sep := bytes.IndexAny(detail, ":=")
if sep == -1 || sep > len(detail)-1 {
return nil, errors.New("malformed detail")
}
key := clean(detail[0:sep])
return &Detail{
Key: string(bytes.TrimSpace(key)),
Value: ParseValue(detail[sep+1:]),
}, nil
}
func (d *Detail) String() string {
valbytes, err := json.Marshal(d.Value)
if err != nil {
return d.Key + ": " + fmt.Sprint(d.Value)
}
return d.Key + ": " + string(valbytes)
}
func clean(b []byte) []byte {
return bytes.Trim(bytes.TrimSpace(b), "`")
}