forked from go-kratos/kratos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
145 lines (136 loc) · 3.8 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
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package generator
import (
"fmt"
"net/http"
"strings"
"github.com/inside-the-mirror/kratos/tool/protobuf/pkg/tag"
"github.com/inside-the-mirror/kratos/tool/protobuf/pkg/typemap"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/protoc-gen-go/descriptor"
"google.golang.org/genproto/googleapis/api/annotations"
)
// HTTPInfo http info for method
type HTTPInfo struct {
HttpMethod string
Path string
LegacyPath string
NewPath string
IsLegacyPath bool
Title string
Description string
// is http path added in the google.api.http option ?
HasExplicitHTTPPath bool
}
type googleMethodOptionInfo struct {
Method string
PathPattern string
HTTPRule *annotations.HttpRule
}
// GetHTTPInfo http info of method
func GetHTTPInfo(
file *descriptor.FileDescriptorProto,
service *descriptor.ServiceDescriptorProto,
method *descriptor.MethodDescriptorProto,
reg *typemap.Registry) *HTTPInfo {
var (
title string
desc string
httpMethod string
newPath string
explicitHTTPPath bool
)
comment, _ := reg.MethodComments(file, service, method)
tags := tag.GetTagsInComment(comment.Leading)
cleanComments := tag.GetCommentWithoutTag(comment.Leading)
if len(cleanComments) > 0 {
title = strings.Trim(cleanComments[0], "\n\r ")
if len(cleanComments) > 1 {
descLines := cleanComments[1:]
desc = strings.Trim(strings.Join(descLines, "\n"), "\r\n ")
} else {
desc = ""
}
} else {
title = ""
}
googleOptionInfo, err := ParseBMMethod(method)
if err == nil {
httpMethod = strings.ToUpper(googleOptionInfo.Method)
p := googleOptionInfo.PathPattern
if p != "" {
explicitHTTPPath = true
newPath = p
goto END
}
}
if httpMethod == "" {
// resolve http method
httpMethod = tag.GetTagValue("method", tags)
if httpMethod == "" {
httpMethod = "GET"
} else {
httpMethod = strings.ToUpper(httpMethod)
}
}
newPath = "/" + file.GetPackage() + "." + service.GetName() + "/" + method.GetName()
END:
var p = newPath
param := &HTTPInfo{HttpMethod: httpMethod,
Path: p,
NewPath: newPath,
IsLegacyPath: false,
Title: title,
Description: desc,
HasExplicitHTTPPath: explicitHTTPPath,
}
if title == "" {
param.Title = param.Path
}
return param
}
func (t *Base) GetHttpInfoCached(file *descriptor.FileDescriptorProto,
service *descriptor.ServiceDescriptorProto,
method *descriptor.MethodDescriptorProto) *HTTPInfo {
key := file.GetPackage() + service.GetName() + method.GetName()
httpInfo, ok := t.httpInfoCache[key]
if !ok {
httpInfo = GetHTTPInfo(file, service, method, t.Reg)
t.httpInfoCache[key] = httpInfo
}
return httpInfo
}
// ParseBMMethod parse BMMethodDescriptor form method descriptor proto
func ParseBMMethod(method *descriptor.MethodDescriptorProto) (*googleMethodOptionInfo, error) {
ext, err := proto.GetExtension(method.GetOptions(), annotations.E_Http)
if err != nil {
return nil, fmt.Errorf("get extension error: %s", err)
}
rule := ext.(*annotations.HttpRule)
var httpMethod string
var pathPattern string
switch pattern := rule.Pattern.(type) {
case *annotations.HttpRule_Get:
pathPattern = pattern.Get
httpMethod = http.MethodGet
case *annotations.HttpRule_Put:
pathPattern = pattern.Put
httpMethod = http.MethodPut
case *annotations.HttpRule_Post:
pathPattern = pattern.Post
httpMethod = http.MethodPost
case *annotations.HttpRule_Patch:
pathPattern = pattern.Patch
httpMethod = http.MethodPatch
case *annotations.HttpRule_Delete:
pathPattern = pattern.Delete
httpMethod = http.MethodDelete
default:
return nil, fmt.Errorf("unsupport http pattern %s", rule.Pattern)
}
bmMethod := &googleMethodOptionInfo{
Method: httpMethod,
PathPattern: pathPattern,
HTTPRule: rule,
}
return bmMethod, nil
}