Skip to content

Commit 81b4224

Browse files
lvan100lianghuan
authored andcommitted
111
1 parent 5bd9969 commit 81b4224

File tree

5 files changed

+74
-60
lines changed

5 files changed

+74
-60
lines changed

gen/generator/golang/client.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gen/generator/golang/spec.go

Lines changed: 26 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -102,16 +102,17 @@ func (t *Type) QueryCount() int {
102102

103103
// RPC represents a single remote procedure call with HTTP metadata.
104104
type RPC struct {
105-
Name string // Method name
106-
Request string // Request type name
107-
Response string // Response type name
108-
Stream bool // Whether this RPC is a streaming RPC
109-
Path string // HTTP path
110-
FormatPath string // Formatted HTTP path
111-
PathParams []string // HTTP path parameters
112-
Method string // HTTP method (GET, POST, etc.)
113-
ContentType string // HTTP Content-Type
114-
Comment string // Comment of the RPC
105+
Name string // Method name
106+
Request string // Request type name
107+
Response string // Response type name
108+
Stream bool // Whether this RPC is a streaming RPC
109+
Path string // HTTP path
110+
FormatPath string // Formatted HTTP path
111+
PathSegments []pathidl.Segment // HTTP path segments
112+
PathParams map[string]string // HTTP path parameters
113+
Method string // HTTP method (GET, POST, etc.)
114+
ContentType string // HTTP Content-Type
115+
Comment string // Comment of the RPC
115116
}
116117

117118
type ReqIndex struct {
@@ -150,15 +151,17 @@ func Convert(dir string) (GoCode, error) {
150151
for _, doc := range project.Files {
151152
for _, r := range doc.RPCs {
152153
rpc := RPC{
153-
Name: r.Name,
154-
Request: r.Request.Name,
155-
Response: r.Response.UserType.Name,
156-
Stream: r.Response.Stream,
157-
Path: r.Path,
158-
FormatPath: r.Path, // 假设是普通路径
159-
Method: r.Method,
160-
ContentType: r.ContentType,
161-
Comment: formatComment(r.Comments),
154+
Name: r.Name,
155+
Request: r.Request.Name,
156+
Response: r.Response.UserType.Name,
157+
Stream: r.Response.Stream,
158+
Path: r.Path,
159+
FormatPath: r.Path, // 假设是普通路径
160+
PathSegments: r.PathSegments,
161+
PathParams: r.PathParams,
162+
Method: r.Method,
163+
ContentType: r.ContentType,
164+
Comment: formatComment(r.Comments),
162165
}
163166
code.RPCs = append(code.RPCs, rpc)
164167
code.Reqs[rpc.Request] = ReqIndex{}
@@ -200,54 +203,19 @@ func Convert(dir string) (GoCode, error) {
200203
}
201204

202205
for rpcIndex, rpc := range code.RPCs {
203-
segments, err := pathidl.Parse(rpc.Path)
204-
if err != nil {
205-
return GoCode{}, errutil.Explain(err, `failed to parse path %s`, rpc.Path)
206+
for k, s := range rpc.PathParams {
207+
rpc.PathParams[k] = httpidl.ToPascal(s)
206208
}
207-
208-
var (
209-
params = make(map[string]string)
210-
formatPath strings.Builder
211-
)
212-
213-
for _, seg := range segments {
209+
var formatPath strings.Builder
210+
for _, seg := range rpc.PathSegments {
214211
formatPath.WriteString("/")
215212
if seg.Type == pathidl.Static {
216213
formatPath.WriteString(seg.Value)
217214
continue
218215
}
219216
formatPath.WriteString("%s")
220-
params[seg.Value] = ""
221-
}
222-
223-
if len(params) == 0 {
224-
continue
225217
}
226-
227-
reqIndex := code.Reqs[rpc.Request]
228-
t := code.Types[reqIndex.File][reqIndex.Index]
229-
for _, f := range t.Fields {
230-
if f.Binding == nil || f.Binding.From != "path" {
231-
continue
232-
}
233-
if _, ok := params[f.Binding.Name]; !ok {
234-
err = errutil.Explain(nil, "path parameter %s not found in request type %s", f.Binding.Name, rpc.Request)
235-
return GoCode{}, err
236-
}
237-
params[f.Binding.Name] = f.Name
238-
}
239-
240-
var paramNames []string
241-
for k, s := range params {
242-
if s == "" {
243-
err = errutil.Explain(nil, "path parameter %s not found in request type %s", k, rpc.Request)
244-
return GoCode{}, err
245-
}
246-
paramNames = append(paramNames, s)
247-
}
248-
249218
rpc.FormatPath = formatPath.String()
250-
rpc.PathParams = paramNames
251219
code.RPCs[rpcIndex] = rpc
252220
}
253221
return code, nil

lib/httpidl/parser.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"strings"
3030

3131
"github.com/antlr4-go/antlr/v4"
32+
"github.com/go-spring/gs-http-gen/lib/pathidl"
3233
"github.com/go-spring/gs-http-gen/lib/validate"
3334
"github.com/lvan100/golib/errutil"
3435
)
@@ -152,6 +153,45 @@ func ParseDir(dir string) (Project, error) {
152153
}
153154
}
154155

156+
for _, doc := range files {
157+
for rpcIndex, rpc := range doc.RPCs {
158+
segments, err := pathidl.Parse(rpc.Path)
159+
if err != nil {
160+
return Project{}, errutil.Explain(err, `failed to parse path %s`, rpc.Path)
161+
}
162+
params := make(map[string]string)
163+
for _, seg := range segments {
164+
if seg.Type == pathidl.Static {
165+
continue
166+
}
167+
params[seg.Value] = ""
168+
}
169+
srcType, ok := GetType(files, rpc.Request.Name)
170+
if !ok {
171+
return Project{}, errutil.Explain(nil, "type %s is used but not defined", rpc.Request.Name)
172+
}
173+
for _, f := range srcType.Fields {
174+
if f.Binding == nil || f.Binding.From != "path" {
175+
continue
176+
}
177+
if _, ok = params[f.Binding.Name]; !ok {
178+
err = errutil.Explain(nil, "path parameter %s not found in request type %s", f.Binding.Name, rpc.Request)
179+
return Project{}, err
180+
}
181+
params[f.Binding.Name] = f.Name
182+
}
183+
for k, s := range params {
184+
if s == "" {
185+
err = errutil.Explain(nil, "path parameter %s not found in request type %s", k, rpc.Request)
186+
return Project{}, err
187+
}
188+
}
189+
rpc.PathSegments = segments
190+
rpc.PathParams = params
191+
doc.RPCs[rpcIndex] = rpc
192+
}
193+
}
194+
155195
return Project{
156196
Meta: meta,
157197
Files: files,

lib/httpidl/testdata/success/http.idl.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1072,7 +1072,9 @@
10721072
"ContentType": "application/x-www-form-urlencoded",
10731073
"ConnTimeout": 100,
10741074
"ReadTimeout": 100,
1075-
"WriteTimeout": 100
1075+
"WriteTimeout": 100,
1076+
"PathSegments": null,
1077+
"PathParams": null
10761078
}
10771079
],
10781080
"EnumTypes": {

lib/httpidl/types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package httpidl
1818

1919
import (
20+
"github.com/go-spring/gs-http-gen/lib/pathidl"
2021
"github.com/go-spring/gs-http-gen/lib/validate"
2122
)
2223

@@ -248,6 +249,9 @@ type RPC struct {
248249
ConnTimeout int // Connection timeout, ms
249250
ReadTimeout int // Read timeout, ms
250251
WriteTimeout int // Write timeout, ms
252+
253+
PathSegments []pathidl.Segment
254+
PathParams map[string]string
251255
}
252256

253257
// RespType represents the response type of an RPC.

0 commit comments

Comments
 (0)