11package kernel
22
33import (
4+ "encoding/json"
45 "fmt"
56 "github.com/RobyFerro/dig"
67 "github.com/RobyFerro/go-web-framework/register"
@@ -14,6 +15,8 @@ import (
1415
1516var SingletonIOC * dig.Container
1617
18+ type Request map [string ]interface {}
19+
1720// WebRouter parses routing structures and set every route.
1821// Return a Gorilla Mux router instance with all routes indicated in router.yml file.
1922func WebRouter (routes []register.HTTPRouter ) * mux.Router {
@@ -53,7 +56,7 @@ func HandleSingleRoute(routes []register.Route, router *mux.Router) {
5356 return
5457 }
5558
56- executeControllerDirective (directive , writer , request )
59+ executeControllerDirective (directive , writer , request , validation )
5760 }).Methods (route .Method )
5861
5962 subRouter .Use (parseMiddleware (route .Middleware )... )
@@ -67,7 +70,7 @@ func HandleSingleRoute(routes []register.Route, router *mux.Router) {
6770 return
6871 }
6972
70- executeControllerDirective (directive , writer , request )
73+ executeControllerDirective (directive , writer , request , validation )
7174 }).Methods (route .Method )
7275 }
7376 }
@@ -92,7 +95,7 @@ func HandleGroups(groups []register.Group, router *mux.Router) {
9295 return
9396 }
9497
95- executeControllerDirective (directive , writer , request )
98+ executeControllerDirective (directive , writer , request , validation )
9699 }).Methods (route .Method )
97100
98101 nestedRouter .Use (parseMiddleware (route .Middleware )... )
@@ -106,7 +109,7 @@ func HandleGroups(groups []register.Group, router *mux.Router) {
106109 return
107110 }
108111
109- executeControllerDirective (directive , writer , request )
112+ executeControllerDirective (directive , writer , request , validation )
110113 }).Methods (route .Method )
111114 }
112115 }
@@ -142,8 +145,18 @@ func GetControllerInterface(directive []string, w http.ResponseWriter, r *http.R
142145// Example: MainController@main
143146// executes the main method from MainController
144147// It build the CUSTOM SERVICE CONTAINER and invoke the selected directive inside them.
145- func executeControllerDirective (d []string , w http.ResponseWriter , r * http.Request ) {
148+ func executeControllerDirective (d []string , w http.ResponseWriter , r * http.Request , validation interface {} ) {
146149 container := BuildCustomContainer ()
150+ payload := structToMap (validation )
151+
152+ err := container .Provide (func () Request {
153+ return payload
154+ })
155+
156+ if err != nil {
157+ log .Fatal (err )
158+ }
159+
147160 cc := GetControllerInterface (d , w , r )
148161 method := reflect .ValueOf (cc ).MethodByName (d [1 ])
149162
@@ -152,6 +165,14 @@ func executeControllerDirective(d []string, w http.ResponseWriter, r *http.Reque
152165 }
153166}
154167
168+ func structToMap (s interface {}) map [string ]interface {} {
169+ m := make (map [string ]interface {})
170+ j , _ := json .Marshal (s )
171+ _ = json .Unmarshal (j , & m )
172+
173+ return m
174+ }
175+
155176func validateRequest (data interface {}, r * http.Request ) error {
156177 if data != nil {
157178 if err := tool .DecodeJsonRequest (r , & data ); err != nil {
0 commit comments