/
template_qspec.go
58 lines (53 loc) · 1.77 KB
/
template_qspec.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
package gengorums
import (
"github.com/relab/gorums"
"google.golang.org/protobuf/compiler/protogen"
)
var qspecInterface = `
{{$genFile := .GenFile}}
{{$configOpt := use "gorums.ConfigOption" .GenFile}}
{{- range qspecServices .Services}}
// QuorumSpec is the interface of quorum functions for {{.GoName}}.
type QuorumSpec interface {
{{$configOpt}}
{{range qspecMethods .Methods -}}
{{/* Below . is the method object */}}
{{$method := .GoName}}
{{$in := in $genFile .}}
{{$out := out $genFile .}}
{{$customOut := customOut $genFile .}}
// {{$method}}QF is the quorum function for the {{$method}}
// {{docName .}} call method. The in parameter is the request object
// supplied to the {{$method}} method at call time, and may or may not
// be used by the quorum function. If the in parameter is not needed
// you should implement your quorum function with '_ *{{$in}}'.
{{$method}}QF(in *{{$in}}, replies map[uint32]*{{$out}}) (*{{$customOut}}{{withCorrectable . ", int"}}, bool)
{{end}}
}
{{end}}
`
// qspecMethods returns all Gorums methods that require
// a quorum function; that is, all except multicast and plain gRPC methods.
func qspecMethods(methods []*protogen.Method) (s []*protogen.Method) {
for _, method := range methods {
if hasMethodOption(method, gorums.E_Multicast, gorums.E_Unicast) || !hasGorumsCallType(method) {
// ignore multicast and non-Gorums methods
continue
}
s = append(s, method)
}
return s
}
// qspecServices returns all services that have Gorums methods.
func qspecServices(services []*protogen.Service) (s []*protogen.Service) {
for _, service := range services {
for _, method := range service.Methods {
if !hasGorumsCallType(method) {
// ignore services without Gorums methods
continue
}
}
s = append(s, service)
}
return s
}