Skip to content

Commit 0455705

Browse files
committed
wip
1 parent 7bf757b commit 0455705

File tree

103 files changed

+1238
-11154
lines changed

Some content is hidden

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

103 files changed

+1238
-11154
lines changed

codegen/example/example_client.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ func exampleCLIMain(_ string, root *expr.RootExpr, svr *expr.ServerExpr) *codege
7070
FuncMap: map[string]any{
7171
"join": strings.Join,
7272
"toUpper": strings.ToUpper,
73-
"hasJSONRPC": func() bool { return hasJSONRPCServices(root, svr) },
74-
"hasHTTP": func() bool { return hasHTTPServices(root, svr) },
73+
"hasJSONRPC": hasJSONRPCServices(root, svr),
74+
"hasHTTP": hasHTTPServices(root, svr),
7575
},
7676
}, {
7777
Name: "cli-main-end",
@@ -86,8 +86,8 @@ func exampleCLIMain(_ string, root *expr.RootExpr, svr *expr.ServerExpr) *codege
8686
FuncMap: map[string]any{
8787
"toUpper": strings.ToUpper,
8888
"join": strings.Join,
89-
"hasJSONRPC": func() bool { return hasJSONRPCServices(root, svr) },
90-
"hasHTTP": func() bool { return hasHTTPServices(root, svr) },
89+
"hasJSONRPC": hasJSONRPCServices(root, svr),
90+
"hasHTTP": hasHTTPServices(root, svr),
9191
},
9292
},
9393
}
@@ -98,7 +98,7 @@ func exampleCLIMain(_ string, root *expr.RootExpr, svr *expr.ServerExpr) *codege
9898
func hasJSONRPCServices(root *expr.RootExpr, svr *expr.ServerExpr) bool {
9999
for _, svcName := range svr.Services {
100100
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" {
101+
if httpSvc.HasJSONRPC {
102102
return true
103103
}
104104
}

codegen/example/example_server.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,11 +190,10 @@ func filterJSONRPCOnlyServices(root *expr.RootExpr, services []*service.Data) []
190190
for _, svc := range services {
191191
// Check if this service has JSON-RPC
192192
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
193+
if httpSvc.HasJSONRPC {
195194
hasRegularHTTP := false
196195
for _, ep := range httpSvc.HTTPEndpoints {
197-
if ep.Meta == nil || len(ep.Meta["jsonrpc:endpoint"]) == 0 || ep.Meta["jsonrpc:endpoint"][0] != "true" {
196+
if !ep.IsJSONRPC {
198197
hasRegularHTTP = true
199198
break
200199
}
@@ -212,7 +211,7 @@ func filterJSONRPCOnlyServices(root *expr.RootExpr, services []*service.Data) []
212211
func hasAnyJSONRPCService(root *expr.RootExpr, services []*service.Data) bool {
213212
for _, svc := range services {
214213
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" {
214+
if httpSvc.HasJSONRPC {
216215
return true
217216
}
218217
}

codegen/generator/example.go

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"goa.design/goa/v3/expr"
99
grpccodegen "goa.design/goa/v3/grpc/codegen"
1010
httpcodegen "goa.design/goa/v3/http/codegen"
11-
jsonrpccodegen "goa.design/goa/v3/jsonrpc/codegen"
1211
)
1312

1413
// Example iterates through the roots and returns files that implement an
@@ -66,24 +65,6 @@ func Example(genpkg string, roots []eval.Root) ([]*codegen.File, error) {
6665
}
6766
}
6867

69-
// JSON-RPC
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 {
80-
files = append(files, fs...)
81-
}
82-
if fs := jsonrpccodegen.ExampleCLIFiles(genpkg, r); len(fs) > 0 {
83-
files = append(files, fs...)
84-
}
85-
}
86-
8768
// Add imports defined via struct:field:type
8869
for _, f := range files {
8970
if len(f.SectionTemplates) > 0 {

codegen/generator/transport.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,7 @@ func Transport(genpkg string, roots []eval.Root) ([]*codegen.File, error) {
4242
files = append(files, grpccodegen.ClientCLIFiles(genpkg, grpcServices)...)
4343

4444
// JSON-RPC
45-
files = append(files, jsonrpccodegen.ServerFiles(genpkg, r)...)
46-
files = append(files, jsonrpccodegen.ServerTypeFiles(genpkg, r)...)
47-
files = append(files, jsonrpccodegen.ClientFiles(genpkg, r)...)
48-
files = append(files, jsonrpccodegen.ClientTypeFiles(genpkg, r)...)
49-
files = append(files, jsonrpccodegen.ClientCLIFiles(genpkg, r)...)
45+
files = append(files, jsonrpccodegen.ServerFiles(genpkg, httpServices)...)
5046

5147
// Add service data meta type imports
5248
for _, f := range files {

dsl/http.go

Lines changed: 62 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -156,12 +156,20 @@ func HTTP(fns ...func()) {
156156
case *expr.APIExpr:
157157
eval.Execute(fn, expr.Root)
158158
case *expr.ServiceExpr:
159-
res := expr.Root.API.HTTP.ServiceFor(actual)
160-
res.DSLFunc = fn
159+
svc := expr.Root.API.HTTP.ServiceFor(actual)
160+
if existing := svc.DSLFunc; existing != nil {
161+
svc.DSLFunc = func() { existing(); fn() }
162+
} else {
163+
svc.DSLFunc = fn
164+
}
161165
case *expr.MethodExpr:
162-
res := expr.Root.API.HTTP.ServiceFor(actual.Service)
163-
act := res.EndpointFor(actual.Name, actual)
164-
act.DSLFunc = fn
166+
svc := expr.Root.API.HTTP.ServiceFor(actual.Service)
167+
e := svc.EndpointFor(actual)
168+
if existing := e.DSLFunc; existing != nil {
169+
e.DSLFunc = func() { existing(); fn() }
170+
} else {
171+
e.DSLFunc = fn
172+
}
165173
default:
166174
eval.IncompatibleDSL()
167175
}
@@ -235,8 +243,7 @@ func Produces(args ...string) {
235243
// As a special case, if you want to generate a path with a trailing slash, you can use
236244
// GET("/./") to generate a path such as '/foo/'.
237245
//
238-
// Path must appear in an API HTTP expression, a Service HTTP expression, or
239-
// a Service JSONRPC expression.
246+
// Path must appear in an API HTTP expression or a Service HTTP expression.
240247
//
241248
// Path accepts one argument: the HTTP path prefix.
242249
//
@@ -253,12 +260,6 @@ func Produces(args ...string) {
253260
// Path("/{accountID}")
254261
// })
255262
// })
256-
//
257-
// var _ = Service("rpc", func() {
258-
// JSONRPC(func() {
259-
// Path("/rpc") // JSON-RPC endpoint path
260-
// })
261-
// })
262263
func Path(val string) {
263264
switch def := eval.Current().(type) {
264265
case *expr.RootExpr:
@@ -357,30 +358,32 @@ func PATCH(path string) *expr.RouteExpr {
357358

358359
func route(method, path string) *expr.RouteExpr {
359360
r := &expr.RouteExpr{Method: method, Path: path}
360-
361361
switch e := eval.Current().(type) {
362362
case *expr.HTTPEndpointExpr:
363-
r.Endpoint = e
363+
r.Parent = e
364+
e.Routes = append(e.Routes, r)
365+
case *expr.JSONRPCServiceExpr:
366+
r.Parent = e
364367
e.Routes = append(e.Routes, r)
365368
default:
366369
eval.IncompatibleDSL()
367370
}
368-
371+
369372
return r
370373
}
371374

372375
// Header describes a single HTTP header or gRPC metadata header. The properties
373376
// (description, type, validation etc.) of a header are inherited from the
374377
// request or response type attribute with the same name by default.
375378
//
376-
// Header must appear in the API HTTP expression (to define request headers
377-
// common to all the API endpoints), a service HTTP expression (to define
378-
// request headers common to all the service endpoints) a specific method HTTP
379-
// expression (to define request headers) or a Response expression (to define
380-
// the response headers). Header may also appear in a method GRPC expression (to
381-
// define headers sent in message metadata), or in a Response expression (to
382-
// define headers sent in result metadata). Finally Header may also appear in a
383-
// Headers expression.
379+
// Header must appear in the API HTTP or JSONRPC expression (to define request
380+
// headers common to all the API endpoints), a service HTTP or JSONRPC
381+
// expression (to define request headers common to all the service endpoints) a
382+
// specific method HTTP or JSONRPC expression (to define request headers) or a
383+
// Response expression (to define the response headers). Header may also appear
384+
// in a method GRPC expression (to define headers sent in message metadata), or
385+
// in a Response expression (to define headers sent in result metadata). Finally
386+
// Header may also appear in a Headers expression.
384387
//
385388
// Header accepts the same arguments as the Attribute function. The header name
386389
// may define a mapping between the attribute name and the HTTP header name when
@@ -419,11 +422,11 @@ func Header(name string, args ...any) {
419422
// Cookie identifies a HTTP cookie. When used within a Response the Cookie DSL
420423
// also makes it possible to define the cookie attributes.
421424
//
422-
// Cookie must appear in the API HTTP expression (to define request cookies
423-
// common to all the API endpoints), a service HTTP expression (to define
424-
// request cookies common to all the service endpoints) a specific method HTTP
425-
// expression (to define request cookies) or a Response expression (to define
426-
// the response cookies).
425+
// Cookie must appear in the API HTTP or JSONRPC expression (to define request
426+
// cookies common to all the API endpoints), a service HTTP or JSONRPC
427+
// expression (to define request cookies common to all the service endpoints) a
428+
// specific method HTTP or JSONRPC expression (to define request cookies) or a
429+
// Response expression (to define the response cookies).
427430
//
428431
// Cookie accepts the same arguments as the Attribute function. The cookie name
429432
// may define a mapping between the attribute name and the cookie name. The
@@ -1154,6 +1157,21 @@ func headers(exp eval.Expression) *expr.MappedAttributeExpr {
11541157
e.Headers = expr.NewEmptyMappedAttributeExpr()
11551158
}
11561159
return e.Headers
1160+
case *expr.JSONRPCServiceExpr:
1161+
if e.Headers == nil {
1162+
e.Headers = expr.NewEmptyMappedAttributeExpr()
1163+
}
1164+
return e.Headers
1165+
case *expr.JSONRPCEndpointExpr:
1166+
if e.Headers == nil {
1167+
e.Headers = expr.NewEmptyMappedAttributeExpr()
1168+
}
1169+
return e.Headers
1170+
case *expr.JSONRPCResponseExpr:
1171+
if e.Headers == nil {
1172+
e.Headers = expr.NewEmptyMappedAttributeExpr()
1173+
}
1174+
return e.Headers
11571175
case *expr.MappedAttributeExpr:
11581176
return e
11591177
default:
@@ -1185,6 +1203,21 @@ func cookies(exp eval.Expression) *expr.MappedAttributeExpr {
11851203
e.Cookies = expr.NewEmptyMappedAttributeExpr()
11861204
}
11871205
return e.Cookies
1206+
case *expr.JSONRPCServiceExpr:
1207+
if e.Cookies == nil {
1208+
e.Cookies = expr.NewEmptyMappedAttributeExpr()
1209+
}
1210+
return e.Cookies
1211+
case *expr.JSONRPCEndpointExpr:
1212+
if e.Cookies == nil {
1213+
e.Cookies = expr.NewEmptyMappedAttributeExpr()
1214+
}
1215+
return e.Cookies
1216+
case *expr.JSONRPCResponseExpr:
1217+
if e.Cookies == nil {
1218+
e.Cookies = expr.NewEmptyMappedAttributeExpr()
1219+
}
1220+
return e.Cookies
11881221
case *expr.MappedAttributeExpr:
11891222
return e
11901223
default:

0 commit comments

Comments
 (0)