@@ -102,16 +102,17 @@ func (t *Type) QueryCount() int {
102102
103103// RPC represents a single remote procedure call with HTTP metadata.
104104type 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
117118type 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
0 commit comments