@@ -398,6 +398,65 @@ var _ = Describe("ControllersWriter", func() {
398
398
f , _ = os .Create (filename )
399
399
})
400
400
401
+ Context ("with a versioned API" , func () {
402
+ var data []* genapp.ControllerTemplateData
403
+
404
+ BeforeEach (func () {
405
+ version := & design.APIVersionDefinition {Name : "" , BasePath : "/api/:vp" }
406
+ v1 := & design.APIVersionDefinition {Name : "v1" }
407
+ v2 := & design.APIVersionDefinition {Name : "v2" }
408
+ versions := map [string ]* design.APIVersionDefinition {
409
+ "1.0" : v1 ,
410
+ "2.0" : v2 ,
411
+ }
412
+ api := & design.APIDefinition {
413
+ APIVersionDefinition : version ,
414
+ APIVersions : versions ,
415
+ Resources : nil ,
416
+ Types : nil ,
417
+ MediaTypes : nil ,
418
+ VersionParams : []string {"vp" },
419
+ VersionHeaders : []string {"vh" },
420
+ VersionQueries : []string {"vq" },
421
+ }
422
+ encoderMap := map [string ]* genapp.EncoderTemplateData {
423
+ "github.com/goadesign/goa" : & genapp.EncoderTemplateData {
424
+ PackagePath : "github.com/goadesign/goa" ,
425
+ PackageName : "goa" ,
426
+ Factory : "NewEncoder" ,
427
+ MIMETypes : []string {"application/json" },
428
+ Default : true ,
429
+ },
430
+ }
431
+ decoderMap := map [string ]* genapp.EncoderTemplateData {
432
+ "github.com/goadesign/goa" : & genapp.EncoderTemplateData {
433
+ PackagePath : "github.com/goadesign/goa" ,
434
+ PackageName : "goa" ,
435
+ Factory : "NewDecoder" ,
436
+ MIMETypes : []string {"application/json" },
437
+ Default : true ,
438
+ },
439
+ }
440
+ data = []* genapp.ControllerTemplateData {& genapp.ControllerTemplateData {
441
+ API : api ,
442
+ Resource : "resource" ,
443
+ Actions : []map [string ]interface {}{},
444
+ Version : api .APIVersionDefinition ,
445
+ EncoderMap : encoderMap ,
446
+ DecoderMap : decoderMap ,
447
+ }}
448
+ })
449
+
450
+ It ("generates the service initialization code" , func () {
451
+ err := writer .Execute (data )
452
+ Ω (err ).ShouldNot (HaveOccurred ())
453
+ b , err := ioutil .ReadFile (filename )
454
+ Ω (err ).ShouldNot (HaveOccurred ())
455
+ written := string (b )
456
+ Ω (written ).Should (Equal (initController ))
457
+ })
458
+ })
459
+
401
460
Context ("with data" , func () {
402
461
var actions , verbs , paths , contexts , unmarshals []string
403
462
var payloads []* design.UserTypeDefinition
@@ -418,6 +477,7 @@ var _ = Describe("ControllersWriter", func() {
418
477
419
478
JustBeforeEach (func () {
420
479
codegen .TempCount = 0
480
+ api := & design.APIDefinition {}
421
481
d := & genapp.ControllerTemplateData {
422
482
Resource : "Bottles" ,
423
483
Version : & design.APIVersionDefinition {},
@@ -445,6 +505,7 @@ var _ = Describe("ControllersWriter", func() {
445
505
}
446
506
}
447
507
if len (as ) > 0 {
508
+ d .API = api
448
509
d .Actions = as
449
510
d .EncoderMap = encoderMap
450
511
d .DecoderMap = decoderMap
@@ -1008,16 +1069,48 @@ type BottlesController interface {
1008
1069
goa.Muxer
1009
1070
List(*ListBottleContext) error
1010
1071
}
1072
+ `
1073
+
1074
+ initController = `
1075
+ // inited is true if initService has been called
1076
+ var inited = false
1077
+
1078
+ // initService sets up the service encoders, decoders and mux.
1079
+ func initService(service *goa.Service) {
1080
+ if inited {
1081
+ return
1082
+ }
1083
+ inited = true
1084
+ // Setup encoders and decoders
1085
+ service.SetEncoder(goa.NewEncoder(), true, "application/json")
1086
+ service.SetDecoder(goa.NewDecoder(), true, "application/json")
1087
+
1088
+ // Configure mux for versioning.
1089
+ if mux, ok := service.Mux.(*goa.RootMux); ok {
1090
+ func0 := goa.PathSelectVersionFunc("/api/:vp", "vp")
1091
+ func1 := goa.HeaderSelectVersionFunc("vh")
1092
+ func2 := goa.QuerySelectVersionFunc("vq")
1093
+ mux.SelectVersionFunc = goa.CombineSelectVersionFunc(func0, func1, func2, )
1094
+ }
1095
+ }
1096
+
1097
+ // resourceController is the controller interface for the resource actions.
1098
+ type resourceController interface {
1099
+ goa.Muxer
1100
+ }
1101
+
1102
+ // MountresourceController "mounts" a resource resource controller on the given service.
1103
+ func MountresourceController(service *goa.Service, ctrl resourceController) {
1104
+ initService(service)
1105
+ var h goa.Handler
1106
+ mux := service.Mux
1107
+ }
1011
1108
`
1012
1109
1013
1110
encoderController = `
1014
1111
// MountBottlesController "mounts" a Bottles resource controller on the given service.
1015
1112
func MountBottlesController(service *goa.Service, ctrl BottlesController) {
1016
- // Setup encoders and decoders. This is idempotent and is done by each MountXXX function.
1017
- service.SetEncoder(goa.JSONEncoderFactory(), false, "application/json")
1018
- service.SetDecoder(goa.JSONDecoderFactory(), false, "application/json")
1019
-
1020
- // Setup endpoint handler
1113
+ initService(service)
1021
1114
var h goa.Handler
1022
1115
mux := service.Mux
1023
1116
h = func(ctx context.Context, rw http.ResponseWriter, req *http.Request) error {
@@ -1033,9 +1126,7 @@ func MountBottlesController(service *goa.Service, ctrl BottlesController) {
1033
1126
`
1034
1127
1035
1128
simpleMount = `func MountBottlesController(service *goa.Service, ctrl BottlesController) {
1036
- // Setup encoders and decoders. This is idempotent and is done by each MountXXX function.
1037
-
1038
- // Setup endpoint handler
1129
+ initService(service)
1039
1130
var h goa.Handler
1040
1131
mux := service.Mux
1041
1132
h = func(ctx context.Context, rw http.ResponseWriter, req *http.Request) error {
@@ -1059,9 +1150,7 @@ type BottlesController interface {
1059
1150
`
1060
1151
1061
1152
multiMount = `func MountBottlesController(service *goa.Service, ctrl BottlesController) {
1062
- // Setup encoders and decoders. This is idempotent and is done by each MountXXX function.
1063
-
1064
- // Setup endpoint handler
1153
+ initService(service)
1065
1154
var h goa.Handler
1066
1155
mux := service.Mux
1067
1156
h = func(ctx context.Context, rw http.ResponseWriter, req *http.Request) error {
0 commit comments