Skip to content

Commit 7bf757b

Browse files
committed
Simplification start
1 parent f570535 commit 7bf757b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+1403
-1817
lines changed

codegen/example/example_client.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,10 @@ func exampleCLIMain(_ string, root *expr.RootExpr, svr *expr.ServerExpr) *codege
9797
// hasJSONRPCServices checks if the given server has any JSON-RPC services.
9898
func hasJSONRPCServices(root *expr.RootExpr, svr *expr.ServerExpr) bool {
9999
for _, svcName := range svr.Services {
100-
if root.API.JSONRPC.Service(svcName) != nil {
101-
return true
100+
if httpSvc := root.API.HTTP.Service(svcName); httpSvc != nil {
101+
if httpSvc.Meta != nil && len(httpSvc.Meta["jsonrpc:service"]) > 0 && httpSvc.Meta["jsonrpc:service"][0] == "true" {
102+
return true
103+
}
102104
}
103105
}
104106
return false

codegen/example/example_server.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -188,9 +188,21 @@ func filterHTTPRESTServices(root *expr.RootExpr, services []*service.Data) []*se
188188
func filterJSONRPCOnlyServices(root *expr.RootExpr, services []*service.Data) []*service.Data {
189189
var res []*service.Data
190190
for _, svc := range services {
191-
// Check if this service has JSON-RPC but no HTTP endpoints
192-
if root.API.JSONRPC.Service(svc.Name) != nil && root.API.HTTP.Service(svc.Name) == nil {
193-
res = append(res, svc)
191+
// Check if this service has JSON-RPC
192+
if httpSvc := root.API.HTTP.Service(svc.Name); httpSvc != nil {
193+
if httpSvc.Meta != nil && len(httpSvc.Meta["jsonrpc:service"]) > 0 && httpSvc.Meta["jsonrpc:service"][0] == "true" {
194+
// Only include if it doesn't also have regular HTTP endpoints
195+
hasRegularHTTP := false
196+
for _, ep := range httpSvc.HTTPEndpoints {
197+
if ep.Meta == nil || len(ep.Meta["jsonrpc:endpoint"]) == 0 || ep.Meta["jsonrpc:endpoint"][0] != "true" {
198+
hasRegularHTTP = true
199+
break
200+
}
201+
}
202+
if !hasRegularHTTP {
203+
res = append(res, svc)
204+
}
205+
}
194206
}
195207
}
196208
return res
@@ -199,8 +211,10 @@ func filterJSONRPCOnlyServices(root *expr.RootExpr, services []*service.Data) []
199211
// hasAnyJSONRPCService returns true if any service has JSON-RPC endpoints
200212
func hasAnyJSONRPCService(root *expr.RootExpr, services []*service.Data) bool {
201213
for _, svc := range services {
202-
if root.API.JSONRPC.Service(svc.Name) != nil {
203-
return true
214+
if httpSvc := root.API.HTTP.Service(svc.Name); httpSvc != nil {
215+
if httpSvc.Meta != nil && len(httpSvc.Meta["jsonrpc:service"]) > 0 && httpSvc.Meta["jsonrpc:service"][0] == "true" {
216+
return true
217+
}
204218
}
205219
}
206220
return false

codegen/example/server_data.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ func buildServerData(svr *expr.ServerExpr, root *expr.RootExpr) *Data {
210210
for _, svc := range svr.Services {
211211
_, seenHTTP := foundTrans[TransportHTTP]
212212
_, seenGRPC := foundTrans[TransportGRPC]
213-
if root.API.HTTP.Service(svc) != nil || root.API.JSONRPC.Service(svc) != nil {
213+
if httpSvc := root.API.HTTP.Service(svc); httpSvc != nil {
214214
httpServices = append(httpServices, svc)
215215
if !seenHTTP {
216216
transports = append(transports, newHTTPTransport())

codegen/generator/example.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,16 @@ func Example(genpkg string, roots []eval.Root) ([]*codegen.File, error) {
6767
}
6868

6969
// JSON-RPC
70-
if len(r.API.JSONRPC.Services) > 0 {
71-
// Use native JSON-RPC services data for proper architectural consistency
72-
jsonrpcServices := jsonrpccodegen.NewServicesData(services)
73-
if fs := jsonrpccodegen.ExampleServerFiles(genpkg, r, jsonrpcServices); len(fs) > 0 {
70+
hasJSONRPC := false
71+
for _, svc := range r.API.HTTP.Services {
72+
if svc.Meta != nil && len(svc.Meta["jsonrpc:service"]) > 0 && svc.Meta["jsonrpc:service"][0] == "true" {
73+
hasJSONRPC = true
74+
break
75+
}
76+
}
77+
if hasJSONRPC {
78+
httpServices := httpcodegen.NewServicesData(services)
79+
if fs := jsonrpccodegen.ExampleServerFiles(genpkg, r, httpServices); len(fs) > 0 {
7480
files = append(files, fs...)
7581
}
7682
if fs := jsonrpccodegen.ExampleCLIFiles(genpkg, r); len(fs) > 0 {

dsl/description.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,6 @@ func Description(d string) {
4343
e.Description = d
4444
case *expr.HTTPFileServerExpr:
4545
e.Description = d
46-
case *expr.JSONRPCResponseExpr:
47-
e.Description = d
48-
case *expr.JSONRPCErrorExpr:
49-
e.Description = d
5046
case *expr.GRPCResponseExpr:
5147
e.Description = d
5248
case *expr.InterceptorExpr:

dsl/http.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,6 @@ func Path(val string) {
280280
}
281281
}
282282
def.Paths = append(def.Paths, val)
283-
case *expr.JSONRPCServiceExpr:
284-
def.Paths = append(def.Paths, val)
285283
default:
286284
eval.IncompatibleDSL()
287285
}
@@ -364,8 +362,6 @@ func route(method, path string) *expr.RouteExpr {
364362
case *expr.HTTPEndpointExpr:
365363
r.Endpoint = e
366364
e.Routes = append(e.Routes, r)
367-
case *expr.JSONRPCEndpointExpr:
368-
e.Routes = append(e.Routes, r)
369365
default:
370366
eval.IncompatibleDSL()
371367
}

dsl/jsonrpc.go

Lines changed: 61 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,20 @@ import (
88
const (
99
// RPCParseError indicates invalid JSON was received by the server.
1010
// An error occurred on the server while parsing the JSON text.
11-
RPCParseError = expr.RPCParseError
11+
RPCParseError = -32700
1212

1313
// RPCInvalidRequest indicates the JSON sent is not a valid Request object.
14-
RPCInvalidRequest = expr.RPCInvalidRequest
14+
RPCInvalidRequest = -32600
1515

1616
// RPCMethodNotFound indicates the method does not exist or is not available.
17-
RPCMethodNotFound = expr.RPCMethodNotFound
17+
RPCMethodNotFound = -32601
1818

1919
// RPCInvalidParams indicates invalid method parameters.
20-
RPCInvalidParams = expr.RPCInvalidParams
20+
RPCInvalidParams = -32602
2121

2222
// RPCInternalError indicates an internal JSON-RPC error occurred.
2323
// This is the default error code for unmapped errors.
24-
RPCInternalError = expr.RPCInternalError
24+
RPCInternalError = -32603
2525
)
2626

2727
// JSONRPC defines JSON-RPC transport specific properties on an API, service or
@@ -152,20 +152,63 @@ func JSONRPC(args ...any) {
152152
eval.InvalidArgError("function", name)
153153
return
154154
}
155-
eval.Execute(fn, expr.Root.API.JSONRPC)
155+
// Mark API as supporting JSON-RPC
156+
if actual.Meta == nil {
157+
actual.Meta = expr.MetaExpr{}
158+
}
159+
actual.Meta["jsonrpc:api"] = []string{"true"}
160+
// Execute DSL in API context to handle Error() mappings
161+
eval.Execute(fn, actual)
156162
case *expr.ServiceExpr:
157163
if name != "" {
158164
eval.InvalidArgError("function", name)
159165
return
160166
}
161-
jsonsvc := expr.Root.API.JSONRPC.ServiceFor(actual)
162-
jsonsvc.DSLFunc = fn
167+
// Create HTTP service if it doesn't exist
168+
httpSvc := expr.Root.API.HTTP.ServiceFor(actual)
169+
// Mark service as JSON-RPC enabled
170+
if httpSvc.Meta == nil {
171+
httpSvc.Meta = expr.MetaExpr{}
172+
}
173+
httpSvc.Meta["jsonrpc:service"] = []string{"true"}
174+
// Wrap DSL in HTTP service context
175+
existing := httpSvc.DSLFunc
176+
httpSvc.DSLFunc = func() {
177+
if existing != nil {
178+
existing()
179+
}
180+
fn()
181+
}
163182
case *expr.MethodExpr:
164-
jsonsvc := expr.Root.API.JSONRPC.ServiceFor(actual.Service)
165-
endpoint := jsonsvc.EndpointFor(actual.Name, actual)
166-
endpoint.DSLFunc = fn
183+
// Create HTTP endpoint if it doesn't exist
184+
httpSvc := expr.Root.API.HTTP.ServiceFor(actual.Service)
185+
// Also mark the service as JSON-RPC when method uses JSON-RPC
186+
if httpSvc.Meta == nil {
187+
httpSvc.Meta = expr.MetaExpr{}
188+
}
189+
httpSvc.Meta["jsonrpc:service"] = []string{"true"}
190+
191+
httpEndpoint := httpSvc.EndpointFor(actual.Name, actual)
192+
// Mark endpoint as JSON-RPC
193+
if httpEndpoint.Meta == nil {
194+
httpEndpoint.Meta = expr.MetaExpr{}
195+
}
196+
httpEndpoint.Meta["jsonrpc:endpoint"] = []string{"true"}
167197
if name != "" {
168-
endpoint.MethodName = name
198+
httpEndpoint.Meta["jsonrpc:method"] = []string{name}
199+
} else {
200+
// Default method name: Service.Method
201+
httpEndpoint.Meta["jsonrpc:method"] = []string{actual.Service.Name + "." + actual.Name}
202+
}
203+
// Default ID attribute
204+
httpEndpoint.Meta["jsonrpc:id-attribute"] = []string{"id"}
205+
// Wrap DSL in HTTP endpoint context
206+
existing := httpEndpoint.DSLFunc
207+
httpEndpoint.DSLFunc = func() {
208+
if existing != nil {
209+
existing()
210+
}
211+
fn()
169212
}
170213
default:
171214
eval.IncompatibleDSL()
@@ -207,10 +250,14 @@ func JSONRPC(args ...any) {
207250
// })
208251
// })
209252
func JSONRPCID(name string) {
210-
endpoint, ok := eval.Current().(*expr.JSONRPCEndpointExpr)
253+
endpoint, ok := eval.Current().(*expr.HTTPEndpointExpr)
211254
if !ok {
212255
eval.IncompatibleDSL()
213256
return
214257
}
215-
endpoint.IDAttribute = name
258+
// Store ID attribute name in metadata
259+
if endpoint.Meta == nil {
260+
endpoint.Meta = expr.MetaExpr{}
261+
}
262+
endpoint.Meta["jsonrpc:id-attribute"] = []string{name}
216263
}

dsl/response.go

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -138,14 +138,6 @@ func Response(val any, args ...any) {
138138
if e := httpError(name, t, args...); e != nil {
139139
t.Errors = append(t.Errors, e)
140140
}
141-
case *expr.JSONRPCExpr:
142-
if !ok {
143-
eval.InvalidArgError("name of error", val)
144-
return
145-
}
146-
if e := jsonrpcError(name, t, args...); e != nil {
147-
t.Errors = append(t.Errors, e)
148-
}
149141
case *expr.GRPCExpr:
150142
if !ok {
151143
eval.InvalidArgError("name of error", val)
@@ -181,27 +173,6 @@ func Response(val any, args ...any) {
181173
eval.Execute(fn, resp)
182174
}
183175
t.Responses = append(t.Responses, resp)
184-
case *expr.JSONRPCServiceExpr:
185-
if !ok {
186-
eval.InvalidArgError("name of error", val)
187-
return
188-
}
189-
if e := jsonrpcError(name, t, args...); e != nil {
190-
t.Errors = append(t.Errors, e)
191-
}
192-
case *expr.JSONRPCEndpointExpr:
193-
if ok {
194-
if e := jsonrpcError(name, t, args...); e != nil {
195-
t.Errors = append(t.Errors, e)
196-
}
197-
return
198-
}
199-
_, fn := parseResponseArgs(val, args...)
200-
resp := &expr.JSONRPCResponseExpr{Parent: t}
201-
if fn != nil {
202-
eval.Execute(fn, resp)
203-
}
204-
t.Response = resp
205176
case *expr.GRPCServiceExpr:
206177
if !ok {
207178
eval.InvalidArgError("name of error", val)
@@ -241,41 +212,13 @@ func Code(code int) {
241212
switch t := eval.Current().(type) {
242213
case *expr.HTTPResponseExpr:
243214
t.StatusCode = code
244-
case *expr.JSONRPCErrorExpr:
245-
t.StatusCode = code
246215
case *expr.GRPCResponseExpr:
247216
t.StatusCode = code
248217
default:
249218
eval.IncompatibleDSL()
250219
}
251220
}
252221

253-
func jsonrpcError(n string, p eval.Expression, args ...any) *expr.JSONRPCErrorExpr {
254-
if len(args) == 0 {
255-
eval.TooFewArgError()
256-
return nil
257-
}
258-
var (
259-
code int
260-
fn func()
261-
val any
262-
)
263-
val = args[0]
264-
args = args[1:]
265-
code, fn = parseResponseArgs(val, args...)
266-
if code == 0 {
267-
code = expr.RPCInternalError
268-
}
269-
err := &expr.JSONRPCErrorExpr{
270-
StatusCode: code,
271-
Name: n,
272-
Parent: p,
273-
}
274-
if fn != nil {
275-
eval.Execute(fn, err)
276-
}
277-
return err
278-
}
279222

280223
func grpcError(n string, p eval.Expression, args ...any) *expr.GRPCErrorExpr {
281224
if len(args) == 0 {

dsl/sse.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ import (
2424
// used as the "data" field and custom mapping for others.
2525
//
2626
// ServerSentEvents can appear in an API HTTP expression (to specify SSE for all
27-
// streaming methods in the API), in a Service HTTP or JSONRPC expression (to
28-
// specify SSE for all streaming methods in the service), or in a Method HTTP or
29-
// JSONRPC expression. When specified at the API or service level, any method
27+
// streaming methods in the API), in a Service HTTP expression (to
28+
// specify SSE for all streaming methods in the service), or in a Method HTTP
29+
// expression. When specified at the API or service level, any method
3030
// with a StreamingPayload will fall back to using WebSockets as SSE only
3131
// supports server-to-client streaming.
3232
//
@@ -127,10 +127,6 @@ func ServerSentEvents(args ...any) {
127127
actual.SSE = sse
128128
case *expr.HTTPEndpointExpr:
129129
actual.SSE = sse
130-
case *expr.JSONRPCServiceExpr:
131-
actual.SSE = sse
132-
case *expr.JSONRPCEndpointExpr:
133-
actual.SSE = sse
134130
default:
135131
eval.IncompatibleDSL()
136132
}

expr/api.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ type (
4444
HTTP *HTTPExpr
4545
// GRPC contains the gRPC specific API level expressions.
4646
GRPC *GRPCExpr
47-
// JSONRPC contains the JSON-RPC specific API level expressions.
48-
JSONRPC *JSONRPCExpr
4947

5048
// random generator used to build examples for the API types.
5149
ExampleGenerator *ExampleGenerator
@@ -84,7 +82,6 @@ func NewAPIExpr(name string, dsl func()) *APIExpr {
8482
Name: name,
8583
HTTP: new(HTTPExpr),
8684
GRPC: new(GRPCExpr),
87-
JSONRPC: new(JSONRPCExpr),
8885
DSLFunc: dsl,
8986
ExampleGenerator: NewRandom(name),
9087
}
@@ -156,9 +153,6 @@ func (a *APIExpr) Finalize() {
156153
if a.HTTP != nil {
157154
a.HTTP.Finalize()
158155
}
159-
if a.JSONRPC != nil {
160-
a.JSONRPC.Finalize()
161-
}
162156
}
163157

164158
// EvalName is the qualified name of the expression.

0 commit comments

Comments
 (0)