forked from zeromicro/go-zero
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kv.go
85 lines (70 loc) · 1.9 KB
/
kv.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
package ast
import (
"strings"
"github.com/estatie/go-zero/tools/goctl/api/parser/g4/gen/api"
)
// KvExpr describes key-value for api
type KvExpr struct {
Key Expr
Value Expr
DocExpr []Expr
CommentExpr Expr
}
// VisitKvLit implements from api.BaseApiParserVisitor
func (v *ApiVisitor) VisitKvLit(ctx *api.KvLitContext) any {
var kvExpr KvExpr
kvExpr.Key = v.newExprWithToken(ctx.GetKey())
commentExpr := v.getComment(ctx)
if ctx.GetValue() != nil {
valueText := ctx.GetValue().GetText()
valueExpr := v.newExprWithToken(ctx.GetValue())
if strings.Contains(valueText, "//") {
if commentExpr == nil {
commentExpr = v.newExprWithToken(ctx.GetValue())
commentExpr.SetText("")
}
index := strings.Index(valueText, "//")
commentExpr.SetText(valueText[index:])
valueExpr.SetText(strings.TrimSpace(valueText[:index]))
} else if strings.Contains(valueText, "/*") {
if commentExpr == nil {
commentExpr = v.newExprWithToken(ctx.GetValue())
commentExpr.SetText("")
}
index := strings.Index(valueText, "/*")
commentExpr.SetText(valueText[index:])
valueExpr.SetText(strings.TrimSpace(valueText[:index]))
}
kvExpr.Value = valueExpr
}
kvExpr.DocExpr = v.getDoc(ctx)
kvExpr.CommentExpr = commentExpr
return &kvExpr
}
// Format provides a formatter for api command, now nothing to do
func (k *KvExpr) Format() error {
// todo
return nil
}
// Equal compares whether the element literals in two KvExpr are equal
func (k *KvExpr) Equal(v any) bool {
if v == nil {
return false
}
kv, ok := v.(*KvExpr)
if !ok {
return false
}
if !EqualDoc(k, kv) {
return false
}
return k.Key.Equal(kv.Key) && k.Value.Equal(kv.Value)
}
// Doc returns the document of KvExpr, like // some text
func (k *KvExpr) Doc() []Expr {
return k.DocExpr
}
// Comment returns the comment of KvExpr, like // some text
func (k *KvExpr) Comment() Expr {
return k.CommentExpr
}