Merge "Add INFO.yaml"
[iec.git] / src / foundation / api / revel / invoker.go
1 // Copyright (c) 2012-2016 The Revel Framework Authors, All rights reserved.
2 // Revel Framework source code and usage is governed by a MIT style
3 // license that can be found in the LICENSE file.
4
5 package revel
6
7 import (
8         "io"
9         "reflect"
10 )
11
12 var (
13         controllerPtrType = reflect.TypeOf(&Controller{})
14         websocketType     = reflect.TypeOf((*ServerWebSocket)(nil)).Elem()
15 )
16
17 func ActionInvoker(c *Controller, _ []Filter) {
18         // Instantiate the method.
19         methodValue := reflect.ValueOf(c.AppController).MethodByName(c.MethodType.Name)
20
21         // Collect the values for the method's arguments.
22         var methodArgs []reflect.Value
23         for _, arg := range c.MethodType.Args {
24                 // If they accept a websocket connection, treat that arg specially.
25                 var boundArg reflect.Value
26                 if arg.Type.Implements(websocketType) {
27                         boundArg = reflect.ValueOf(c.Request.WebSocket)
28                 } else {
29                         boundArg = Bind(c.Params, arg.Name, arg.Type)
30                         // #756 - If the argument is a closer, defer a Close call,
31                         // so we don't risk on leaks.
32                         if closer, ok := boundArg.Interface().(io.Closer); ok {
33                                 defer func() {
34                                         _ = closer.Close()
35                                 }()
36                         }
37                 }
38                 methodArgs = append(methodArgs, boundArg)
39         }
40
41         var resultValue reflect.Value
42         if methodValue.Type().IsVariadic() {
43                 resultValue = methodValue.CallSlice(methodArgs)[0]
44         } else {
45                 resultValue = methodValue.Call(methodArgs)[0]
46         }
47         if resultValue.Kind() == reflect.Interface && !resultValue.IsNil() {
48                 c.Result = resultValue.Interface().(Result)
49         }
50 }