@@ -26,7 +26,8 @@ const (
26
26
27
27
// JSONRPC configures a service or method to use JSON-RPC 2.0 transport.
28
28
// The generated code handles JSON-RPC protocol details: request parsing, method dispatch,
29
- // response formatting, and batch processing. All methods share a single HTTP endpoint.
29
+ // response formatting, and batch processing. All service JSON-RPC methods share
30
+ // a single HTTP endpoint.
30
31
//
31
32
// At API level, JSONRPC maps global errors to JSON-RPC error codes.
32
33
// At service level, it configures the HTTP endpoint and common settings.
@@ -55,84 +56,83 @@ const (
55
56
// Example - Complete service with request/notification handling and streaming:
56
57
//
57
58
// Service("calc", func() {
58
- // Error("div_zero ", ErrorResult)
59
+ // Error("timeout ", ErrTimeout, "Request timed out") // ErrTimeout must have a limit attribute
59
60
//
60
61
// JSONRPC(func() {
61
- // POST("/rpc") // All methods use this endpoint
62
- // Response("div_zero", RPCInvalidParams) // Map service error to JSON-RPC code
63
- // Headers(func() { // Common request headers
64
- // Attribute("X-API-Version", String)
62
+ // POST("/rpc") // All methods use this endpoint
63
+ // Response("timeout", func() { // Custom error response
64
+ // Code(5001) // Application error code
65
+ // Headers(func() { // Error response headers
66
+ // Attribute("limit:X-Rate-Limit", String) // Map "limit" attribute to header
67
+ // })
68
+ // })
69
+ // Headers(func() { // Common request headers
70
+ // Attribute("version:X-API-Version", String)
65
71
// })
66
72
// })
67
73
//
68
- // Method("add", func() { // Notification method (no ID mapping)
74
+ // Method("add", func() { // Notification method (no ID mapping)
69
75
// Payload(func() {
70
- // Attribute("a", Int)
71
- // Attribute("b", Int)
76
+ // Attribute("a", Int, "First operand" )
77
+ // Attribute("b", Int, "Second operand" )
72
78
// Required("a", "b")
73
79
// })
74
80
// Result(Int)
75
81
// })
76
82
//
77
- // Method("divide", func() { // Request/response method
83
+ // Method("divide", func() { // Request/response method
78
84
// Payload(func() {
79
- // Attribute("req_id", String) // Will contain JSON-RPC request ID
80
- // Attribute("dividend", Int)
81
- // Attribute("divisor", Int)
85
+ // Attribute("req_id", String, "Request ID") // Will contain JSON-RPC request ID
86
+ // Attribute("dividend", Int, "Dividend" )
87
+ // Attribute("divisor", Int, "Divisor" )
82
88
// Required("dividend", "divisor")
83
89
// })
84
90
// Result(Float64)
85
- // Error("div_zero")
91
+ // Error("div_zero", ErrorResult, "Division by zero" )
86
92
//
87
93
// JSONRPC(func() {
88
- // ID("req_id") // Map request ID to payload field
89
- // Response("div_zero", func() { // Custom error response
90
- // Code(5001) // Application error code
91
- // Headers(func() { // Error response headers
92
- // Attribute("X-Rate-Limit", String)
93
- // })
94
- // })
94
+ // ID("req_id") // Map request ID to payload field
95
+ // Response("div_zero", RPCInvalidParams) // Map div_zero error to JSON-RPC code
95
96
// })
96
97
// })
97
98
//
98
99
// Method("updates", func() { // SSE streaming method
99
100
// Payload(func() {
100
- // Attribute("client_id ", String)
101
- // Attribute("last_event_id", String)
101
+ // Attribute("req_id ", String, "Request ID" )
102
+ // Attribute("last_event_id", String, "ID of last event received by client" )
102
103
// })
103
104
// StreamingResult(func() {
104
- // Attribute("event_id", String)
105
- // Attribute("data", Any )
105
+ // Attribute("event_id", String, "Event ID" )
106
+ // Attribute("data", Data, "Event data" )
106
107
// })
107
108
//
108
109
// JSONRPC(func() {
109
- // ID("client_id")
110
- // ServerSentEvents(func() { // Use SSE instead of WebSocket
111
- // SSERequestID("last_event_id") // Map SSE Last-Event-ID header to payload
112
- // SSEEventID("event_id") // Use result field as SSE event ID
110
+ // ID("req_id") // Map JSON-RPC request ID to "req_id" payload attribute
111
+ // ServerSentEvents(func() { // Use SSE instead of WebSocket
112
+ // SSERequestID("last_event_id") // Map SSE Last-Event-ID header to payload "last_event_id" attribute
113
+ // SSEEventID("event_id") // Use "event_id" result attribute as SSE event ID
113
114
// })
114
115
// })
115
116
// })
116
117
// })
117
118
func JSONRPC (dsl func ()) {
118
- var fn func ()
119
119
switch actual := eval .Current ().(type ) {
120
120
case * expr.APIExpr :
121
- eval .Execute (fn , actual )
121
+ eval .Execute (dsl , actual . JSONRPC )
122
122
case * expr.ServiceExpr :
123
123
svc := expr .Root .API .JSONRPC .ServiceFor (actual )
124
124
if existing := svc .DSLFunc ; existing != nil {
125
- svc .DSLFunc = func () { existing (); fn () }
125
+ svc .DSLFunc = func () { existing (); dsl () }
126
126
} else {
127
- svc .DSLFunc = fn
127
+ svc .DSLFunc = dsl
128
128
}
129
129
case * expr.MethodExpr :
130
130
svc := expr .Root .API .JSONRPC .ServiceFor (actual .Service )
131
131
e := svc .EndpointFor (actual )
132
132
if existing := e .DSLFunc ; existing != nil {
133
- e .DSLFunc = func () { existing (); fn () }
133
+ e .DSLFunc = func () { existing (); dsl () }
134
134
} else {
135
- e .DSLFunc = fn
135
+ e .DSLFunc = dsl
136
136
}
137
137
default :
138
138
eval .IncompatibleDSL ()
0 commit comments