-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
69 lines (59 loc) · 1.47 KB
/
util.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
package generator
import (
"strconv"
"strings"
"github.com/zhangbao138208/goctls/rpc/parser"
)
func GetGroup(service parser.Service) (data []string) {
groupNames := map[string]struct{}{}
for _, rpc := range service.RPC {
if name := GetGroupName(rpc); name != "" {
groupNames[name] = struct{}{}
}
}
for k, _ := range groupNames {
data = append(data, k)
}
return data
}
func GetGroupName(rpc *parser.RPC) string {
if rpc.Comment != nil && len(rpc.Comment.Lines) > 0 {
for _, v := range rpc.Comment.Lines {
if strings.Contains(v, "group") {
groupData := strings.Split(v, ":")
if len(groupData) == 2 {
return strings.TrimSpace(groupData[1])
}
}
}
}
return ""
}
func HasLock(service parser.Service) bool {
for _, rpc := range service.RPC {
if key, _, _ := GetLockKey(rpc); key != "" {
return true
}
}
return false
}
func GetLockKey(rpc *parser.RPC) (string, string, int) {
if rpc.Comment != nil && len(rpc.Comment.Lines) > 0 {
for _, v := range rpc.Comment.Lines {
if strings.Contains(v, "lock") {
groupData := strings.Split(v, ":")
if len(groupData) == 2 {
return strings.TrimSpace(groupData[1]), "", 0
}
if len(groupData) == 3 {
return strings.TrimSpace(groupData[1]), strings.TrimSpace(groupData[2]), 0
}
if len(groupData) == 4 {
exceed, _ := strconv.Atoi(groupData[3])
return strings.TrimSpace(groupData[1]), strings.TrimSpace(groupData[2]), exceed
}
}
}
}
return "", "", 0
}