Skip to content

Commit

Permalink
Handle variadic arguments to actions
Browse files Browse the repository at this point in the history
Fixes revel#60
  • Loading branch information
robfig committed Jan 13, 2013
1 parent 2555b88 commit 8e08d09
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
7 changes: 6 additions & 1 deletion controller.go
Expand Up @@ -91,7 +91,12 @@ func (c *Controller) Invoke(appControllerPtr reflect.Value, method reflect.Value

if c.Result == nil {
// Invoke the action.
resultValue := method.Call(methodArgs)[0]
var resultValue reflect.Value
if method.Type().IsVariadic() {
resultValue = method.CallSlice(methodArgs)[0]
} else {
resultValue = method.Call(methodArgs)[0]
}
if resultValue.Kind() == reflect.Interface && !resultValue.IsNil() {
c.Result = resultValue.Interface().(Result)
}
Expand Down
3 changes: 3 additions & 0 deletions harness/reflect.go
Expand Up @@ -672,6 +672,9 @@ func NewTypeExpr(pkgName string, expr ast.Expr) TypeExpr {
case *ast.ArrayType:
e := NewTypeExpr(pkgName, t.Elt)
return TypeExpr{"[]" + e.Expr, e.PkgName, e.pkgIndex + 2}
case *ast.Ellipsis:
e := NewTypeExpr(pkgName, t.Elt)
return TypeExpr{"[]" + e.Expr, e.PkgName, e.pkgIndex + 2}
default:
log.Println("Failed to generate name for field.")
ast.Print(nil, expr)
Expand Down
31 changes: 22 additions & 9 deletions harness/reflect_test.go
Expand Up @@ -86,23 +86,33 @@ func TestGetValidationKeys(t *testing.T) {
}

var TypeExprs = map[string]TypeExpr{
"int": TypeExpr{"int", "", 0},
"*int": TypeExpr{"*int", "", 1},
"[]int": TypeExpr{"[]int", "", 2},
"[]*int": TypeExpr{"[]*int", "", 3},
"MyType": TypeExpr{"MyType", "pkg", 0},
"*MyType": TypeExpr{"*MyType", "pkg", 1},
"[]MyType": TypeExpr{"[]MyType", "pkg", 2},
"[]*MyType": TypeExpr{"[]*MyType", "pkg", 3},
"int": TypeExpr{"int", "", 0},
"*int": TypeExpr{"*int", "", 1},
"[]int": TypeExpr{"[]int", "", 2},
"...int": TypeExpr{"[]int", "", 2},
"[]*int": TypeExpr{"[]*int", "", 3},
"...*int": TypeExpr{"[]*int", "", 3},
"MyType": TypeExpr{"MyType", "pkg", 0},
"*MyType": TypeExpr{"*MyType", "pkg", 1},
"[]MyType": TypeExpr{"[]MyType", "pkg", 2},
"...MyType": TypeExpr{"[]MyType", "pkg", 2},
"[]*MyType": TypeExpr{"[]*MyType", "pkg", 3},
"...*MyType": TypeExpr{"[]*MyType", "pkg", 3},
}

func TestTypeExpr(t *testing.T) {
for typeStr, expected := range TypeExprs {
// Handle arrays myself, since ParseExpr() does not.
// Handle arrays and ... myself, since ParseExpr() does not.
array := strings.HasPrefix(typeStr, "[]")
if array {
typeStr = typeStr[2:]
}

ellipsis := strings.HasPrefix(typeStr, "...")
if ellipsis {
typeStr = typeStr[3:]
}

expr, err := parser.ParseExpr(typeStr)
if err != nil {
t.Error("Failed to parse test expr:", typeStr)
Expand All @@ -112,6 +122,9 @@ func TestTypeExpr(t *testing.T) {
if array {
expr = &ast.ArrayType{expr.Pos(), nil, expr}
}
if ellipsis {
expr = &ast.Ellipsis{expr.Pos(), expr}
}

actual := NewTypeExpr("pkg", expr)
if !reflect.DeepEqual(expected, actual) {
Expand Down

0 comments on commit 8e08d09

Please sign in to comment.