forked from aerospike/aerospike-management-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
70 lines (55 loc) · 1.2 KB
/
parser.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package lib
import (
"bufio"
"fmt"
"strconv"
"strings"
)
type InfoParser struct {
*bufio.Reader
}
func NewInfoParser(s string) *InfoParser {
return &InfoParser{bufio.NewReader(strings.NewReader(s))}
}
// PeekAndExpect checks if the expected value is present without advancing the reader
func (ip *InfoParser) PeekAndExpect(s string) error {
bytes, err := ip.Peek(len(s))
if err != nil {
return err
}
v := string(bytes)
if v != s {
return fmt.Errorf("InfoParser: Wrong value. Peek expected %s, but found %s", s, v)
}
return nil
}
func (ip *InfoParser) Expect(s string) error {
buf := make([]byte, len(s))
if _, err := ip.Read(buf); err != nil {
return err
}
if sbuf := string(buf); sbuf != s {
return fmt.Errorf("expected value %q found %q", s, sbuf)
}
return nil
}
func (ip *InfoParser) ReadUntil(delim byte) (string, error) {
v, err := ip.ReadBytes(delim)
switch len(v) {
case 0:
return string(v), err
case 1:
if v[0] == delim {
return "", err
}
return string(v), err
}
return string(v[:len(v)-1]), err
}
func (ip *InfoParser) ReadFloat(delim byte) (float64, error) {
s, err := ip.ReadUntil(delim)
if err != nil {
return 0, err
}
return strconv.ParseFloat(s, 64)
}