Skip to content

Commit 1b3a5f7

Browse files
lvan100lianghuan
authored andcommitted
111
1 parent c831790 commit 1b3a5f7

File tree

8 files changed

+68
-116
lines changed

8 files changed

+68
-116
lines changed

gen/generator/golang/server.go

Lines changed: 2 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gen/generator/golang/type.go

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -379,47 +379,9 @@ func convertTypes(ctx Context, doc tidl.Document) (_ []Type, err error) {
379379
ret = append(ret, r)
380380
}
381381

382-
// Handle RPC response types with generics
383-
for _, c := range doc.RPCs {
384-
if c.Response.Stream || c.Response.UserType == nil {
385-
continue
386-
}
387-
r, err := convertResponseType(ctx, c.Name, c.Response)
388-
if err != nil {
389-
return nil, err
390-
}
391-
ret = append(ret, r)
392-
}
393382
return ret, nil
394383
}
395384

396-
// convertResponseType instantiates a generic response type for RPC
397-
func convertResponseType(ctx Context, rpcName string, respType tidl.RespType) (Type, error) {
398-
t, ok := tidl.GetType(ctx.files, respType.TypeName)
399-
if !ok {
400-
return Type{}, fmt.Errorf("type %s not found", respType.TypeName)
401-
}
402-
403-
typeName := rpcName + t.Name + respType.UserType.Name
404-
405-
r := tidl.Type{
406-
Name: typeName,
407-
Position: t.Position,
408-
Comments: t.Comments,
409-
}
410-
411-
// Replace generic type fields
412-
for _, f := range t.Fields {
413-
u, ok := f.FieldType.(tidl.UserType)
414-
if ok && u.Name == *t.GenericName {
415-
f.FieldType = *respType.UserType
416-
}
417-
r.Fields = append(r.Fields, f)
418-
}
419-
420-
return convertType(ctx, r)
421-
}
422-
423385
// convertRedefinedType instantiates a redefined generic struct type
424386
func convertRedefinedType(ctx Context, r tidl.Type) (Type, error) {
425387
t, ok := tidl.GetType(ctx.files, r.Redefined.Name)

lib/tidl/TParser.g4

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,12 @@ rpc_def
123123

124124
// RPC request type: always an identifier
125125
rpc_req
126-
: IDENTIFIER
126+
: user_type
127127
;
128128

129-
// RPC response type: identifier, generic form (Type<T>), or stream<T>
129+
// RPC response type: identifier, stream<T>
130130
rpc_resp
131-
: IDENTIFIER
131+
: user_type
132132
| TYPE_STREAM LESS_THAN user_type GREATER_THAN
133133
;
134134

lib/tidl/format.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ func dumpRPC(r RPC) string {
312312
sb.WriteString("rpc ")
313313
sb.WriteString(r.Name)
314314
sb.WriteString("(")
315-
sb.WriteString(r.Request)
315+
sb.WriteString(r.Request.Text())
316316
sb.WriteString(") ")
317317
sb.WriteString(r.Response.Text())
318318
sb.WriteString(" {")

lib/tidl/parser.go

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -482,8 +482,7 @@ func (l *ParseTreeListener) parseValueType(ctx interface {
482482
// types and annotations.
483483
func (l *ParseTreeListener) ExitRpc_def(ctx *Rpc_defContext) {
484484
r := RPC{
485-
Name: ctx.IDENTIFIER().GetText(),
486-
Request: ctx.Rpc_req().GetText(),
485+
Name: ctx.IDENTIFIER().GetText(),
487486
Position: Position{
488487
Start: ctx.GetStart().GetLine(),
489488
Stop: ctx.GetStop().GetLine(),
@@ -495,35 +494,31 @@ func (l *ParseTreeListener) ExitRpc_def(ctx *Rpc_defContext) {
495494
if !IsPascal(r.Name) {
496495
panic(fmt.Errorf("RPC name %s is not PascalCase in line %d", r.Name, r.Position.Start))
497496
}
498-
if !IsPascal(r.Request) {
499-
panic(fmt.Errorf("RPC request type %s is not PascalCase in line %d", r.Request, r.Position.Start))
497+
498+
// Request
499+
reqType := ctx.Rpc_req().User_type()
500+
r.Request = UserType{
501+
Name: reqType.IDENTIFIER().GetText(),
502+
Optional: reqType.QUESTION() != nil,
503+
}
504+
if !IsPascal(r.Request.Name) {
505+
panic(fmt.Errorf("RPC request type %s is not PascalCase in line %d", r.Request.Name, r.Position.Start))
500506
}
501-
l.Document.UsedTypes[r.Request] = struct{}{}
507+
l.Document.UsedTypes[r.Request.Name] = struct{}{}
502508

503509
// Response
504-
if resp := ctx.Rpc_resp(); resp.TYPE_STREAM() != nil {
505-
u := resp.User_type()
506-
r.Response = RespType{
507-
Stream: true,
508-
UserType: &UserType{
509-
Name: u.IDENTIFIER().GetText(),
510-
Optional: u.QUESTION() != nil,
511-
},
512-
}
513-
if !IsPascal(r.Response.UserType.Name) {
514-
panic(fmt.Errorf("RPC response type %s is not PascalCase in line %d", r.Response.UserType.Name, r.Position.Start))
515-
}
516-
l.Document.UsedTypes[r.Response.UserType.Name] = struct{}{}
517-
} else {
518-
r.Response = RespType{
519-
Stream: false,
520-
TypeName: resp.IDENTIFIER().GetText(),
521-
}
522-
if !IsPascal(r.Response.TypeName) {
523-
panic(fmt.Errorf("RPC response type %s is not PascalCase in line %d", r.Response.TypeName, r.Position.Start))
524-
}
525-
l.Document.UsedTypes[r.Response.TypeName] = struct{}{}
510+
respType := ctx.Rpc_resp().User_type()
511+
if ctx.Rpc_resp().TYPE_STREAM() != nil {
512+
r.Response.Stream = true
513+
}
514+
r.Response.UserType = UserType{
515+
Name: respType.IDENTIFIER().GetText(),
516+
Optional: respType.QUESTION() != nil,
517+
}
518+
if !IsPascal(r.Response.UserType.Name) {
519+
panic(fmt.Errorf("RPC response type %s is not PascalCase in line %d", r.Response.UserType.Name, r.Position.Start))
526520
}
521+
l.Document.UsedTypes[r.Response.UserType.Name] = struct{}{}
527522

528523
// Annotations
529524
for _, aCtx := range ctx.Rpc_annotations().AllAnnotation() {

lib/tidl/t_parser.go

Lines changed: 28 additions & 29 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -611,11 +611,16 @@
611611
"RPCs": [
612612
{
613613
"Name": "GetUser",
614-
"Request": "UserReq",
614+
"Request": {
615+
"Name": "UserReq",
616+
"Optional": false
617+
},
615618
"Response": {
616619
"Stream": false,
617-
"TypeName": "UserResponse",
618-
"UserType": null
620+
"UserType": {
621+
"Name": "UserResponse",
622+
"Optional": false
623+
}
619624
},
620625
"Annotations": [
621626
{

lib/tidl/types.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -216,28 +216,23 @@ type Annotation struct {
216216
// RPC represents a remote procedure call definition.
217217
type RPC struct {
218218
Name string // Name of the RPC
219-
Request string // Request type
219+
Request UserType // Request type
220220
Response RespType // Response type
221221
Annotations []Annotation // Metadata attached to the RPC
222222
Position Position // Location in source code
223223
Comments Comments // Associated comments
224224
}
225225

226226
// RespType represents the response type of an RPC.
227-
// It supports both streaming and generic forms.
228227
type RespType struct {
229-
Stream bool // Whether the response is a stream
230-
TypeName string // Base type name for non-stream responses
231-
UserType *UserType // User-defined type for the response
228+
Stream bool // Whether the response is a stream
229+
UserType UserType // User-defined type for the response
232230
}
233231

234232
// Text returns a human-readable representation of the response type.
235233
func (t RespType) Text() string {
236234
if t.Stream {
237235
return "stream<" + t.UserType.Text() + ">"
238236
}
239-
if t.UserType != nil {
240-
return t.TypeName + "<" + t.UserType.Text() + ">"
241-
}
242-
return t.TypeName
237+
return t.UserType.Text()
243238
}

0 commit comments

Comments
 (0)