forked from nilslice/protolock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hints.go
77 lines (62 loc) · 1.48 KB
/
hints.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
package protolock
import (
"errors"
"fmt"
"strings"
"github.com/emicklei/proto"
)
const (
// CommentSkip tells the parse step to skip the comparable entity.
CommentSkip = "@protolock:skip"
// commentInternal is used for tests
commentInternal = "@protolock:internal"
)
var (
// ErrSkipEntry indicates that the CommentSkip hint was found.
ErrSkipEntry = errors.New("protolock: skip entry hint encountered")
// errInternalTest indicates that the internal test hint was found.
errInternalTest = errors.New("protolock: internal hint encountered")
)
func checkComments(v interface{}) []error {
var errs []error
switch v.(type) {
case *proto.Enum:
e := v.(*proto.Enum)
errs = append(errs, hints(e.Comment)...)
case *proto.Message:
m := v.(*proto.Message)
errs = append(errs, hints(m.Comment)...)
case *proto.Service:
s := v.(*proto.Service)
errs = append(errs, hints(s.Comment)...)
}
return errs
}
func hints(c *proto.Comment) []error {
if c == nil {
return nil
}
var errs []error
for _, line := range c.Lines {
if strings.Contains(line, CommentSkip) {
debugHint(c, CommentSkip)
errs = append(errs, ErrSkipEntry)
}
if strings.Contains(line, commentInternal) {
debugHint(c, commentInternal)
errs = append(errs, errInternalTest)
}
}
return errs
}
func debugHint(c *proto.Comment, hint string) {
if debug {
fmt.Println(
"HINT:", hint,
fmt.Sprintf(
"%s:%d:%d",
c.Position.Filename, c.Position.Line, c.Position.Column,
),
)
}
}