Skip to content

Commit 14fbe66

Browse files
author
Raphaël Simon
authored
Store exact design action name in request context ContextAction (goadesign#1192)
Instead of the goified version. This is so that the names appearing in the logs reflect the design. This also helps with other types of instrumentation (e.g. Prometheus graph names).
1 parent 1c621ea commit 14fbe66

File tree

4 files changed

+19
-17
lines changed

4 files changed

+19
-17
lines changed

goagen/gen_app/generator.go

+1
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ func (g *Generator) generateControllers() error {
275275
unmarshal := fmt.Sprintf("unmarshal%s%sPayload", codegen.Goify(a.Name, true), codegen.Goify(r.Name, true))
276276
action := map[string]interface{}{
277277
"Name": codegen.Goify(a.Name, true),
278+
"DesignName": a.Name,
278279
"Routes": a.Routes,
279280
"Context": context,
280281
"Unmarshal": unmarshal,

goagen/gen_app/generator_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ func MountWidgetController(service *goa.Service, ctrl WidgetController) {
383383
}
384384
return ctrl.Get(rctx)
385385
}
386-
service.Mux.Handle("GET", "/:id", ctrl.MuxHandler("Get", h, nil))
386+
service.Mux.Handle("GET", "/:id", ctrl.MuxHandler("get", h, nil))
387387
service.LogInfo("mount", "ctrl", "Widget", "action", "Get", "route", "GET /:id")
388388
}
389389
`
@@ -449,7 +449,7 @@ func MountWidgetController(service *goa.Service, ctrl WidgetController) {
449449
}
450450
return ctrl.Get(rctx)
451451
}
452-
service.Mux.Handle("GET", "/:id", ctrl.MuxHandler("Get", h, unmarshalGetWidgetPayload))
452+
service.Mux.Handle("GET", "/:id", ctrl.MuxHandler("get", h, unmarshalGetWidgetPayload))
453453
service.LogInfo("mount", "ctrl", "Widget", "action", "Get", "route", "GET /:id")
454454
}
455455
@@ -486,7 +486,7 @@ func MountWidgetController(service *goa.Service, ctrl WidgetController) {
486486
}
487487
return ctrl.Get(rctx)
488488
}
489-
service.Mux.Handle("GET", "/:id", ctrl.MuxHandler("Get", h, unmarshalGetWidgetPayload))
489+
service.Mux.Handle("GET", "/:id", ctrl.MuxHandler("get", h, unmarshalGetWidgetPayload))
490490
service.LogInfo("mount", "ctrl", "Widget", "action", "Get", "route", "GET /:id")
491491
}
492492

goagen/gen_app/writers.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ type (
9191
ControllerTemplateData struct {
9292
API *design.APIDefinition // API definition
9393
Resource string // Lower case plural resource name, e.g. "bottles"
94-
Actions []map[string]interface{} // Array of actions, each action has keys "Name", "Routes", "Context" and "Unmarshal"
94+
Actions []map[string]interface{} // Array of actions, each action has keys "Name", "DesignName", "Routes", "Context" and "Unmarshal"
9595
FileServers []*design.FileServerDefinition // File servers
9696
Encoders []*EncoderTemplateData // Encoder data
9797
Decoders []*EncoderTemplateData // Decoder data
@@ -721,7 +721,7 @@ func Mount{{ .Resource }}Controller(service *goa.Service, ctrl {{ .Resource }}Co
721721
}
722722
{{ if .Security }} h = handleSecurity({{ printf "%q" .Security.Scheme.SchemeName }}, h{{ range .Security.Scopes }}, {{ printf "%q" . }}{{ end }})
723723
{{ end }}{{ if $.Origins }} h = handle{{ $res }}Origin(h)
724-
{{ end }}{{ range .Routes }} service.Mux.Handle("{{ .Verb }}", {{ printf "%q" .FullPath }}, ctrl.MuxHandler({{ printf "%q" $action.Name }}, h, {{ if $action.Payload }}{{ $action.Unmarshal }}{{ else }}nil{{ end }}))
724+
{{ end }}{{ range .Routes }} service.Mux.Handle("{{ .Verb }}", {{ printf "%q" .FullPath }}, ctrl.MuxHandler({{ printf "%q" $action.DesignName }}, h, {{ if $action.Payload }}{{ $action.Unmarshal }}{{ else }}nil{{ end }}))
725725
service.LogInfo("mount", "ctrl", {{ printf "%q" $res }}, "action", {{ printf "%q" $action.Name }}, "route", {{ printf "%q" (printf "%s %s" .Verb .FullPath) }}{{ with $action.Security }}, "security", {{ printf "%q" .Scheme.SchemeName }}{{ end }})
726726
{{ end }}{{ end }}{{ range .FileServers }}
727727
h = ctrl.FileHandler({{ printf "%q" .RequestPath }}, {{ printf "%q" .FilePath }})

goagen/gen_app/writers_test.go

+13-12
Original file line numberDiff line numberDiff line change
@@ -1007,7 +1007,8 @@ var _ = Describe("ControllersWriter", func() {
10071007
payload = payloads[i]
10081008
}
10091009
as[i] = map[string]interface{}{
1010-
"Name": a,
1010+
"Name": codegen.Goify(a, true),
1011+
"DesignName": a,
10111012
"Routes": []*design.RouteDefinition{
10121013
{
10131014
Verb: verbs[i],
@@ -1042,7 +1043,7 @@ var _ = Describe("ControllersWriter", func() {
10421043

10431044
Context("with a simple controller", func() {
10441045
BeforeEach(func() {
1045-
actions = []string{"List"}
1046+
actions = []string{"list"}
10461047
verbs = []string{"GET"}
10471048
paths = []string{"/accounts/:accountID/bottles"}
10481049
contexts = []string{"ListBottleContext"}
@@ -1062,7 +1063,7 @@ var _ = Describe("ControllersWriter", func() {
10621063

10631064
Context("with actions that take a payload", func() {
10641065
BeforeEach(func() {
1065-
actions = []string{"List"}
1066+
actions = []string{"list"}
10661067
verbs = []string{"GET"}
10671068
paths = []string{"/accounts/:accountID/bottles"}
10681069
contexts = []string{"ListBottleContext"}
@@ -1092,7 +1093,7 @@ var _ = Describe("ControllersWriter", func() {
10921093
})
10931094
Context("with actions that take a payload with a required validation", func() {
10941095
BeforeEach(func() {
1095-
actions = []string{"List"}
1096+
actions = []string{"list"}
10961097
required := &dslengine.ValidationDefinition{
10971098
Required: []string{"id"},
10981099
}
@@ -1127,7 +1128,7 @@ var _ = Describe("ControllersWriter", func() {
11271128

11281129
Context("with multiple controllers", func() {
11291130
BeforeEach(func() {
1130-
actions = []string{"List", "Show"}
1131+
actions = []string{"list", "show"}
11311132
verbs = []string{"GET", "GET"}
11321133
paths = []string{"/accounts/:accountID/bottles", "/accounts/:accountID/bottles/:id"}
11331134
contexts = []string{"ListBottleContext", "ShowBottleContext"}
@@ -1147,7 +1148,7 @@ var _ = Describe("ControllersWriter", func() {
11471148

11481149
Context("with encoder and decoder maps", func() {
11491150
BeforeEach(func() {
1150-
actions = []string{"List"}
1151+
actions = []string{"list"}
11511152
verbs = []string{"GET"}
11521153
paths = []string{"/accounts/:accountID/bottles"}
11531154
contexts = []string{"ListBottleContext"}
@@ -1180,7 +1181,7 @@ var _ = Describe("ControllersWriter", func() {
11801181

11811182
Context("with multiple origins", func() {
11821183
BeforeEach(func() {
1183-
actions = []string{"List"}
1184+
actions = []string{"list"}
11841185
verbs = []string{"GET"}
11851186
paths = []string{"/accounts"}
11861187
contexts = []string{"ListBottleContext"}
@@ -1215,7 +1216,7 @@ var _ = Describe("ControllersWriter", func() {
12151216

12161217
Context("with regexp origins", func() {
12171218
BeforeEach(func() {
1218-
actions = []string{"List"}
1219+
actions = []string{"list"}
12191220
verbs = []string{"GET"}
12201221
paths = []string{"/accounts"}
12211222
contexts = []string{"ListBottleContext"}
@@ -2358,7 +2359,7 @@ func MountBottlesController(service *goa.Service, ctrl BottlesController) {
23582359
}
23592360
return ctrl.List(rctx)
23602361
}
2361-
service.Mux.Handle("GET", "/accounts/:accountID/bottles", ctrl.MuxHandler("List", h, nil))
2362+
service.Mux.Handle("GET", "/accounts/:accountID/bottles", ctrl.MuxHandler("list", h, nil))
23622363
service.LogInfo("mount", "ctrl", "Bottles", "action", "List", "route", "GET /accounts/:accountID/bottles")
23632364
}
23642365
`
@@ -2379,7 +2380,7 @@ func MountBottlesController(service *goa.Service, ctrl BottlesController) {
23792380
}
23802381
return ctrl.List(rctx)
23812382
}
2382-
service.Mux.Handle("GET", "/accounts/:accountID/bottles", ctrl.MuxHandler("List", h, nil))
2383+
service.Mux.Handle("GET", "/accounts/:accountID/bottles", ctrl.MuxHandler("list", h, nil))
23832384
service.LogInfo("mount", "ctrl", "Bottles", "action", "List", "route", "GET /accounts/:accountID/bottles")
23842385
}
23852386
`
@@ -2408,7 +2409,7 @@ type BottlesController interface {
24082409
}
24092410
return ctrl.List(rctx)
24102411
}
2411-
service.Mux.Handle("GET", "/accounts/:accountID/bottles", ctrl.MuxHandler("List", h, nil))
2412+
service.Mux.Handle("GET", "/accounts/:accountID/bottles", ctrl.MuxHandler("list", h, nil))
24122413
service.LogInfo("mount", "ctrl", "Bottles", "action", "List", "route", "GET /accounts/:accountID/bottles")
24132414
24142415
h = func(ctx context.Context, rw http.ResponseWriter, req *http.Request) error {
@@ -2423,7 +2424,7 @@ type BottlesController interface {
24232424
}
24242425
return ctrl.Show(rctx)
24252426
}
2426-
service.Mux.Handle("GET", "/accounts/:accountID/bottles/:id", ctrl.MuxHandler("Show", h, nil))
2427+
service.Mux.Handle("GET", "/accounts/:accountID/bottles/:id", ctrl.MuxHandler("show", h, nil))
24272428
service.LogInfo("mount", "ctrl", "Bottles", "action", "Show", "route", "GET /accounts/:accountID/bottles/:id")
24282429
}
24292430
`

0 commit comments

Comments
 (0)