-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproto.go
63 lines (56 loc) · 1.09 KB
/
proto.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
package main
import (
"bytes"
"fmt"
"io"
"log"
"github.com/tidwall/resp"
)
const (
CommandSet = "SET"
CommandGet = "GET"
)
type Command interface {
}
type SetCommand struct {
key, val []byte
}
type GetCommand struct {
key, val []byte
}
func parseCommand(raw string) (Command, error) {
rd := resp.NewReader(bytes.NewBufferString(raw))
for {
v, _, err := rd.ReadValue()
if err == io.EOF {
break
}
if err != nil {
log.Fatal(err)
}
if v.Type() == resp.Array {
for _, val := range v.Array() {
switch val.String() {
case CommandGet:
if len(v.Array()) != 2 {
return nil, fmt.Errorf("invalid number of variables for GET command")
}
cmd := GetCommand{
key: v.Array()[1].Bytes(),
}
return cmd, nil
case CommandSet:
if len(v.Array()) != 3 {
return nil, fmt.Errorf("invalid number of variables for SET command")
}
cmd := SetCommand{
key: v.Array()[1].Bytes(),
val: v.Array()[2].Bytes(),
}
return cmd, nil
}
}
}
}
return nil, fmt.Errorf("invalid or unknown command received: %s", raw)
}