-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
91 lines (73 loc) · 1.8 KB
/
client.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package kece
import (
"bytes"
"errors"
"net"
"strings"
)
// Client struct
type Client struct {
ID string
Conn net.Conn
}
// Subscribe client method, this function will used by client to subscribe to specific topic
//TODO
func (client *Client) Subscribe(topic string) {
}
// ClientMessage struct
type ClientMessage struct {
Client *Client
Message []byte
Cmd []byte
Key []byte
Value []byte
}
func isValidValue(val string) (bool, string) {
var pair = map[string]string{"{": "}", `"`: `"`, "'": "'"}
if _, ok := pair[val]; ok {
return false, val
}
lastChar := string(val[len(val)-1])
if res, ok := pair[string(val[0])]; ok {
if res == lastChar {
if res == `"` || res == "'" {
val = val[1 : len(val)-1] // remove prefix & suffix string => ex: "test" -> test
}
return true, val
}
return false, val
}
if len(strings.Fields(val)) <= 1 { // single string without space and list from pair
return true, val
}
return false, val
}
// ValidateMessage function
func (c *ClientMessage) ValidateMessage() error {
message := bytes.TrimSpace(c.Message)
messages := strings.Fields(string(message))
command, ok := commands[messages[0]]
if !ok {
return errors.New(ErrorInvalidCommand)
}
c.Cmd = []byte(messages[0])
c.Key = []byte(messages[1])
if command == "GET" || command == "DEL" || command == "AUTH" {
if len(messages) < 2 || len(messages) > 2 {
return errors.New(ErrorInvalidOperation)
}
}
if command == "SET" {
if len(messages) < 3 {
return errors.New(ErrorInvalidOperation)
}
messValue := strings.TrimSpace(strings.TrimLeft(string(message), strings.Join(messages[:2], " ")))
isValidVal, value := isValidValue(messValue)
if !isValidVal {
return errors.New(ErrorInvalidArgument)
}
c.Value = []byte(value)
}
c.Message = nil // garbage
return nil
}