From 4cdb3f5c1b07edf76aed08fa3724dddc2fca3d4a Mon Sep 17 00:00:00 2001 From: Dustin Long Date: Fri, 5 Mar 2021 09:44:47 -0500 Subject: [PATCH] fix(dispatch): Fix code style and get tests passing --- api/fsi_test.go | 8 ++++---- lib/dispatch.go | 26 +++++++++++++++++--------- lib/dispatch_test.go | 9 --------- 3 files changed, 21 insertions(+), 22 deletions(-) diff --git a/api/fsi_test.go b/api/fsi_test.go index 0a936fa08..e31ffa0b3 100644 --- a/api/fsi_test.go +++ b/api/fsi_test.go @@ -266,7 +266,7 @@ func TestFSIWrite(t *testing.T) { if actualStatusCode != 200 { t.Errorf("expected status code 200, got %d", actualStatusCode) } - expectBody := `{"data":"","meta":{"code":200}}` + expectBody := `{"meta":{"code":200}}` if expectBody != actualBody { t.Errorf("expected body %s, got %s", expectBody, actualBody) } @@ -377,7 +377,7 @@ func TestCheckoutAndRestore(t *testing.T) { if actualStatusCode != 200 { t.Errorf("expected status code 200, got %d", actualStatusCode) } - expectBody := `{"data":"","meta":{"code":200}}` + expectBody := `{"meta":{"code":200}}` if expectBody != actualBody { t.Errorf("expected body %s, got %s", expectBody, actualBody) } @@ -429,7 +429,7 @@ func TestCheckoutAndRestore(t *testing.T) { if actualStatusCode != 200 { t.Errorf("expected status code 200, got %d", actualStatusCode) } - expectBody = `{"data":"","meta":{"code":200}}` + expectBody = `{"meta":{"code":200}}` if expectBody != actualBody { t.Errorf("expected body %s, got %s", expectBody, actualBody) } @@ -458,7 +458,7 @@ func TestCheckoutAndRestore(t *testing.T) { if actualStatusCode != 200 { t.Errorf("expected status code 200, got %d", actualStatusCode) } - expectBody = `{"data":"","meta":{"code":200}}` + expectBody = `{"meta":{"code":200}}` if expectBody != actualBody { t.Errorf("expected body %s, got %s", expectBody, actualBody) } diff --git a/lib/dispatch.go b/lib/dispatch.go index 06c0d5175..b491a4ed3 100644 --- a/lib/dispatch.go +++ b/lib/dispatch.go @@ -16,6 +16,21 @@ type dispatcher interface { Dispatch(ctx context.Context, method string, param interface{}) (interface{}, Cursor, error) } +// Cursor is used to paginate results for methods that support it +type Cursor interface{} + +// MethodSet represents a set of methods to be registered +// Each registered method should have 2 input parameters and 1-3 output values +// Input: (context.Context, input struct) +// Output, 1: (error) +// 2: (output, error) +// 3: (output, Cursor, error) +// The implementation should have the same input and output as the method, except +// with the context.Context replaced by a scope. +type MethodSet interface { + Name() string +} + // Dispatch is a system for handling calls to lib. Should only be called by top-level lib methods. // // When programs are using qri as a library (such as the `cmd` package), calls to `lib` will @@ -30,7 +45,8 @@ type dispatcher interface { // At construction time, the Instance registers all methods that dispatch can access, as well // as the input and output parameters for those methods, and associates a string name for each // method. Dispatch works by looking up that method name, constructing the necessary input, -// then invoking the actual implementation. +// then invoking the actual implementation. Dispatch returns the custom value from the +// implementation, then a non-nil Cursor if the method supports pagination, then an error or nil. func (inst *Instance) Dispatch(ctx context.Context, method string, param interface{}) (res interface{}, cur Cursor, err error) { if inst == nil { return nil, nil, fmt.Errorf("instance is nil, cannot dispatch") @@ -317,14 +333,6 @@ func (inst *Instance) buildMethodMap(impl interface{}) map[string]reflect.Method return result } -// Cursor is used to paginate results for methods that support it -type Cursor interface{} - -// MethodSet represents a set of methods to be registered -type MethodSet interface { - Name() string -} - func dispatchMethodName(m MethodSet, funcName string) string { lowerName := strings.ToLower(funcName) return fmt.Sprintf("%s.%s", m.Name(), lowerName) diff --git a/lib/dispatch_test.go b/lib/dispatch_test.go index 660955f3e..33f9b1438 100644 --- a/lib/dispatch_test.go +++ b/lib/dispatch_test.go @@ -97,7 +97,6 @@ func expectToPanic(t *testing.T, regFunc func(), expectMessage string) { } // Test data: methodSet and implementation - type animalMethods struct { d dispatcher } @@ -127,7 +126,6 @@ func (m *animalMethods) Dog(ctx context.Context, p *animalParams) (string, error } // Good implementation - type animalImpl struct{} func (animalImpl) Cat(scp scope, p *animalParams) (string, error) { @@ -139,7 +137,6 @@ func (animalImpl) Dog(scp scope, p *animalParams) (string, error) { } // Bad implementation #1 (cat doesn't return enough values) - type badAnimalOneImpl struct{} func (badAnimalOneImpl) Cat(scp scope, p *animalParams) { @@ -151,7 +148,6 @@ func (badAnimalOneImpl) Dog(scp scope, p *animalParams) (string, error) { } // Bad implementation #2 (dog method name doesn't match) - type badAnimalTwoImpl struct{} func (badAnimalTwoImpl) Cat(scp scope, p *animalParams) (string, error) { @@ -163,7 +159,6 @@ func (badAnimalTwoImpl) Doggie(scp scope, p *animalParams) (string, error) { } // Bad implementation #3 (cat doesn't accept a scope) - type badAnimalThreeImpl struct{} func (badAnimalThreeImpl) Cat(ctx context.Context, p *animalParams) (string, error) { @@ -175,7 +170,6 @@ func (badAnimalThreeImpl) Dog(scp scope, p *animalParams) (string, error) { } // Bad implementation #4 (dog input struct doesn't match) - type badAnimalFourImpl struct{} func (badAnimalFourImpl) Cat(scp scope, p *animalParams) (string, error) { @@ -187,7 +181,6 @@ func (badAnimalFourImpl) Dog(scp scope, name string) (string, error) { } // Bad implementation #5 (dog method is missing) - type badAnimalFiveImpl struct{} func (badAnimalFiveImpl) Cat(scp scope, p *animalParams) (string, error) { @@ -195,7 +188,6 @@ func (badAnimalFiveImpl) Cat(scp scope, p *animalParams) (string, error) { } // MethodSet with variadic return values - type fruitMethods struct { d dispatcher } @@ -222,7 +214,6 @@ func (m *fruitMethods) Banana(ctx context.Context, p *fruitParams) (string, Curs } // Implementation for fruit - type fruitImpl struct{} func (fruitImpl) Apple(scp scope, p *fruitParams) error {