-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage.go
72 lines (62 loc) · 1.08 KB
/
message.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
package main
import (
"fmt"
"regexp"
"strings"
)
type command int
type Command interface {
Command() command
}
const (
INDEX command = iota
QUERY
REMOVE
ERROR
)
func (c command) Command() command {
return c
}
type Message struct {
cmd Command
pkg string
dep []string
}
func NewMessage(message string) Message {
r, err := regexp.Compile(`^(INDEX|REMOVE|QUERY)\|([\w\-+]+)\|([\w,\-+]*)`)
if err != nil {
fmt.Println("ERROR", err)
}
s := r.FindStringSubmatch(message)
deps := make([]string, 0)
if len(s) == 0 {
return Message{ERROR, "", deps}
}
if len(s) == 4 && s[3] != "" {
deps = strings.Split(s[3], ",")
}
switch s[1] {
case "INDEX":
return Message{INDEX, s[2], deps}
case "REMOVE":
return Message{REMOVE, s[2], deps}
case "QUERY":
return Message{QUERY, s[2], deps}
default:
return Message{ERROR, "", deps}
}
}
func (m Message) String() string {
var cmd string
switch m.cmd {
case INDEX:
cmd = "INDEX"
case QUERY:
cmd = "QUERY"
case REMOVE:
cmd = "REMOVE"
default:
cmd = "ERROR"
}
return fmt.Sprintf("[%s|%s|%v]", cmd, m.pkg, m.dep)
}