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
|
package linter
import (
"fmt"
"regexp"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/protoc-gen-go/descriptor"
praefect "gitlab.com/gitlab-org/gitaly/internal/praefect/pb"
)
var (
requestRegex = regexp.MustCompile(".*Request")
)
// ensureMsgOpType will ensure that message includes the op_type option.
// See proto example below:
//
// message ExampleRequest {
// option (op_type).op = ACCESSOR;
// }
func ensureMsgOpType(file string, msg *descriptor.DescriptorProto) error {
options := msg.GetOptions()
if !proto.HasExtension(options, praefect.E_OpType) {
return fmt.Errorf(
"%s: Message %s missing op_type option",
file,
msg.GetName(),
)
}
ext, err := proto.GetExtension(options, praefect.E_OpType)
if err != nil {
return err
}
opMsg, ok := ext.(*praefect.OperationMsg)
if !ok {
return fmt.Errorf("unable to obtain OperationMsg from %#v", ext)
}
switch opCode := opMsg.GetOp(); opCode {
case praefect.OperationMsg_ACCESSOR:
return nil
case praefect.OperationMsg_MUTATOR:
return nil
case praefect.OperationMsg_UNKNOWN:
return fmt.Errorf(
"%s: Message %s has op set to UNKNOWN",
file,
msg.GetName(),
)
default:
return fmt.Errorf(
"%s: Message %s has invalid operation class with int32 value of %d",
file,
msg.GetName(),
opCode,
)
}
return nil
}
// LintFile ensures the file described meets Gitaly required processes.
// Currently, this is limited to validating if request messages contain
// a mandatory operation code.
func LintFile(file *descriptor.FileDescriptorProto) []error {
var errs []error
for _, msg := range file.GetMessageType() {
if !requestRegex.MatchString(msg.GetName()) {
continue
}
err := ensureMsgOpType(file.GetName(), msg)
if err != nil {
errs = append(errs, err)
}
}
return errs
}
|