Skip to content

Commit

Permalink
fix(dispatch): Fix code style and get tests passing
Browse files Browse the repository at this point in the history
  • Loading branch information
dustmop committed Mar 5, 2021
1 parent 304f7c5 commit 4cdb3f5
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 22 deletions.
8 changes: 4 additions & 4 deletions api/fsi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down
26 changes: 17 additions & 9 deletions lib/dispatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
Expand Down Expand Up @@ -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)
Expand Down
9 changes: 0 additions & 9 deletions lib/dispatch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ func expectToPanic(t *testing.T, regFunc func(), expectMessage string) {
}

// Test data: methodSet and implementation

type animalMethods struct {
d dispatcher
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -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) {
Expand All @@ -187,15 +181,13 @@ 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) {
return fmt.Sprintf("%s says meow", p.Name), nil
}

// MethodSet with variadic return values

type fruitMethods struct {
d dispatcher
}
Expand All @@ -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 {
Expand Down

0 comments on commit 4cdb3f5

Please sign in to comment.