From 41eeeeea7c923cd27f6ddfb0252cf87cd4423935 Mon Sep 17 00:00:00 2001 From: Tristan Storch Date: Mon, 6 Oct 2014 11:11:07 +0200 Subject: [PATCH] Cheanged if to switch in scope.go Readability improved by changing a long if-else to a switch statement. --- scope.go | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/scope.go b/scope.go index 5fd6e8b9..300054f4 100644 --- a/scope.go +++ b/scope.go @@ -174,21 +174,14 @@ func (scope *Scope) CallMethod(name string) { call := func(value interface{}) { if fm := reflect.ValueOf(value).MethodByName(name); fm.IsValid() { - fi := fm.Interface() - if f, ok := fi.(func()); ok { - f() - } else if f, ok := fi.(func(s *Scope)); ok { - f(scope) - } else if f, ok := fi.(func(s *DB)); ok { - f(scope.db.new()) - } else if f, ok := fi.(func() error); ok { - scope.Err(f()) - } else if f, ok := fi.(func(s *Scope) error); ok { - scope.Err(f(scope)) - } else if f, ok := fi.(func(s *DB) error); ok { - scope.Err(f(scope.db.new())) - } else { - scope.Err(errors.New(fmt.Sprintf("unsupported function %v", name))) + switch f := fm.Interface().(type) { + case func(): f() + case func(s *Scope): f(scope) + case func(s *DB): f(scope.db.new()) + case func() error: scope.Err(f()) + case func(s *Scope) error: scope.Err(f(scope)) + case func(s *DB) error: scope.Err(f(scope.db.new())) + default: scope.Err(errors.New(fmt.Sprintf("unsupported function %v", name))) } } }