-
Notifications
You must be signed in to change notification settings - Fork 6
/
ctx.go
156 lines (142 loc) · 3.7 KB
/
ctx.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
146
147
148
149
150
151
152
153
154
155
156
package main
import (
"bytes"
_ "embed"
"os"
"strings"
"text/template"
"google.golang.org/protobuf/compiler/protogen"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
"github.com/ml444/gkit/cmd/protoc-gen-go-http/pluck"
)
//go:embed http.tmpl
var httpTemplate string
type serviceCtx struct {
ServiceType string // Greeter
ServiceName string // helloworld.Greeter
Metadata string // api/helloworld/helloworld.proto
Methods []*methodCtx
MethodSets map[string]*methodCtx
}
func (s *serviceCtx) execute() string {
s.MethodSets = make(map[string]*methodCtx)
for _, m := range s.Methods {
s.MethodSets[m.Name] = m
}
buf := new(bytes.Buffer)
tmpl, err := template.New("http").Parse(strings.TrimSpace(httpTemplate))
if err != nil {
panic(err)
}
if err := tmpl.Execute(buf, s); err != nil {
panic(err)
}
return strings.Trim(buf.String(), "\r\n")
}
type methodCtx struct {
pluckDesc
// method
Name string
OriginalName string // The parsed original name
Num int
Input string
Output string
Comment string
// http_rule
Path string
Method string
Body string
ResponseBody string
HasVars bool
HasBody bool
BodyIsBytes bool
}
type pluckDesc struct {
ReqHeadersToField string
ReqBodyToField string
FieldToRspHeaders string
FieldToRspBody string
HasPluck bool
ReqHeaders map[string]string
RspHeaders map[string]string
}
func (md *methodCtx) BodyFieldIsBytes(m *protogen.Message, field string) {
fields := m.Desc.Fields()
for _, f := range strings.Split(field, ".") {
fd := fields.ByName(protoreflect.Name(f))
if fd == nil {
return
}
if fd.Kind() == protoreflect.BytesKind {
md.BodyIsBytes = true
return
}
if fd.Kind() == protoreflect.MessageKind {
fields = fd.Message().Fields()
}
}
return
}
func (md *methodCtx) isExistField(m *protogen.Message, field string) bool {
fields := m.Desc.Fields()
for _, f := range strings.Split(field, ".") {
fd := fields.ByName(protoreflect.Name(f))
if fd == nil {
break
}
if fd.Name() == protoreflect.Name(f) {
return true
}
if fd.Kind() == protoreflect.MessageKind {
fields = fd.Message().Fields()
}
}
println(string(m.Desc.FullName()) + " not found the field: " + field)
return false
}
func (md *methodCtx) ParsePluck(method *protogen.Method) {
pluckReqOpt, ok := proto.GetExtension(method.Desc.Options(), pluck.E_Request).(*pluck.PluckRequest)
if ok && pluckReqOpt != nil {
if pluckReqOpt.DefaultHeaders != nil {
md.HasPluck = true
md.ReqHeaders = pluckFields(pluckReqOpt.DefaultHeaders)
}
if pluckReqOpt.HeadersTo != "" {
md.HasPluck = true
md.ReqHeadersToField = camelCase(pluckReqOpt.HeadersTo)
if !md.isExistField(method.Input, pluckReqOpt.HeadersTo) {
os.Exit(2)
}
}
if pluckReqOpt.BodyTo != "" {
md.HasPluck = true
md.ReqBodyToField = camelCase(pluckReqOpt.BodyTo)
md.BodyFieldIsBytes(method.Input, pluckReqOpt.BodyTo)
if !md.isExistField(method.Input, pluckReqOpt.BodyTo) {
os.Exit(2)
}
}
}
pluckRspOpt, ok := proto.GetExtension(method.Desc.Options(), pluck.E_Response).(*pluck.PluckResponse)
if ok && pluckRspOpt != nil {
if pluckRspOpt.DefaultHeaders != nil {
md.HasPluck = true
md.RspHeaders = pluckFields(pluckRspOpt.DefaultHeaders)
}
if pluckRspOpt.HeadersFrom != "" {
md.HasPluck = true
md.FieldToRspHeaders = camelCase(pluckRspOpt.HeadersFrom)
if !md.isExistField(method.Output, pluckRspOpt.HeadersFrom) {
os.Exit(2)
}
}
if pluckRspOpt.BodyFrom != "" {
md.HasPluck = true
md.FieldToRspBody = camelCase(pluckRspOpt.BodyFrom)
if !md.isExistField(method.Output, pluckRspOpt.BodyFrom) {
os.Exit(2)
}
}
}
}