forked from infobloxopen/protoc-gen-atlas-validate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
60 lines (50 loc) · 1.13 KB
/
http.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
package plugin
import (
"github.com/gogo/protobuf/proto"
"github.com/gogo/protobuf/protoc-gen-gogo/descriptor"
http_opts "github.com/gogo/googleapis/google/api"
)
type httpOpt struct {
body string
method string
}
func getHttpMethod(r *http_opts.HttpRule) string {
switch r.GetPattern().(type) {
case *http_opts.HttpRule_Get:
return "GET"
case *http_opts.HttpRule_Post:
return "POST"
case *http_opts.HttpRule_Put:
return "PUT"
case *http_opts.HttpRule_Delete:
return "DELETE"
case *http_opts.HttpRule_Patch:
return "PATCH"
}
return ""
}
func extractHTTPOpts(m *descriptor.MethodDescriptorProto) []httpOpt {
r := []httpOpt{}
if !proto.HasExtension(m.GetOptions(), http_opts.E_Http) {
return nil
}
ext, err := proto.GetExtension(m.Options, http_opts.E_Http)
if err != nil {
return nil
}
if httpRule, ok := ext.(*http_opts.HttpRule); ok {
r = append(r, httpOpt{
body: httpRule.Body,
method: getHttpMethod(httpRule),
})
for _, b := range httpRule.GetAdditionalBindings() {
r = append(r, httpOpt{
body: b.Body,
method: getHttpMethod(b),
})
}
} else {
return nil
}
return r
}