Skip to content

Commit

Permalink
add fallback pattern flag to router plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Qiyao Qin committed Dec 8, 2017
1 parent 8417e0a commit a5b4ca8
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 27 deletions.
11 changes: 8 additions & 3 deletions connectors/routing/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ import (
)

// PluginFunc is a plugin function that takes scope, namePrefix and operation name,
// then gives wanted scope and namePrefix
type PluginFunc func(scope, namePrefix, opName string) (string, string, error)
// then gives wanted scope and namePrefix, and whether to fall into default connector
type PluginFunc func(scope, namePrefix, opName string) (string, string, bool, error)

// Connector holds a slice of configured connectors to route to
type Connector struct {
Expand Down Expand Up @@ -62,10 +62,15 @@ func (rc *Connector) getConnector(scope string, namePrefix string, opName string
if rc.PluginFunc != nil {
// plugin operation
// plugin should always be first considered if it exists
scope, namePrefix, err = rc.PluginFunc(scope, namePrefix, opName)
var fallToDefaultConn bool
scope, namePrefix, fallToDefaultConn, err = rc.PluginFunc(scope, namePrefix, opName)
if err != nil {
return nil, errors.Wrap(err, "failed to execute getConnector due to Plugin function error")
}

if !fallToDefaultConn {
return nil, errors.New("requests failed because they are explicitly set to fail")
}
}
return rc._getConnector(scope, namePrefix)
}
Expand Down
56 changes: 32 additions & 24 deletions connectors/routing/connector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,17 +203,25 @@ func TestGetConnector(t *testing.T) {
assert.Equal(t, reflect.TypeOf(conn), reflect.TypeOf(memory.NewConnector()))

// with plugin
rc.PluginFunc = func(scope, namePrefix, opName string) (string, string, error) {
return "ebook", "ebook-store", nil
rc.PluginFunc = func(scope, namePrefix, opName string) (string, string, bool, error) {
return "ebook", "ebook-store", true, nil
}

conn, err = rc.getConnector(ei.Ref.Scope, ei.Ref.NamePrefix, "Read")
assert.NoError(t, err)
assert.NotNil(t, conn)

// plugin indicates to throw out error rather than fall into default connector
rc.PluginFunc = func(scope, namePrefix, opName string) (string, string, bool, error) {
return "", "", false, nil
}
conn, err = rc.getConnector(ei.Ref.Scope, ei.Ref.NamePrefix, "Read")
assert.Contains(t, err.Error(), "requests failed because they are explicitly set to fail")
assert.Nil(t, conn)

// plugin returns error
rc.PluginFunc = func(scope, namePrefix, opName string) (string, string, error) {
return "", "", errors.New("")
rc.PluginFunc = func(scope, namePrefix, opName string) (string, string, bool, error) {
return "", "", true, errors.New("")
}
conn, err = rc.getConnector(ei.Ref.Scope, ei.Ref.NamePrefix, "Read")
assert.Contains(t, err.Error(), "failed to execute getConnector due to Plugin function error")
Expand Down Expand Up @@ -241,8 +249,8 @@ func TestConnector_CreateIfNotExistsDefaultScope(t *testing.T) {
"p1": dosa.FieldValue("data")})
assert.NoError(t, err)

plugin := func(scope, namePrefix, opName string) (string, string, error) {
return "", "", errors.New("dummy errors")
plugin := func(scope, namePrefix, opName string) (string, string, bool, error) {
return "", "", true, errors.New("dummy errors")
}
rc.PluginFunc = plugin
// not exist scope, use default
Expand Down Expand Up @@ -380,8 +388,8 @@ func TestConnector_Read(t *testing.T) {
"c7": dosa.FieldValue(id)}, dosa.All())
assert.True(t, dosa.ErrorIsNotFound(err))

plugin := func(scope, namePrefix, opName string) (string, string, error) {
return "", "", errors.New("dummy errors")
plugin := func(scope, namePrefix, opName string) (string, string, bool, error) {
return "", "", true, errors.New("dummy errors")
}
rc.PluginFunc = plugin
// not exist scope, use default
Expand Down Expand Up @@ -413,8 +421,8 @@ func TestMultiRead(t *testing.T) {
_, err := rc.MultiRead(ctx, testInfoRandom, testMultiValues, dosa.All())
assert.NoError(t, err)

plugin := func(scope, namePrefix, opName string) (string, string, error) {
return "", "", errors.New("dummy errors")
plugin := func(scope, namePrefix, opName string) (string, string, bool, error) {
return "", "", true, errors.New("dummy errors")
}
rc.PluginFunc = plugin
// not exist scope, use default
Expand Down Expand Up @@ -480,8 +488,8 @@ func TestConnector_Upsert(t *testing.T) {
assert.Len(t, data, 20)
assert.NotNil(t, data[0]["c6"])

plugin := func(scope, namePrefix, opName string) (string, string, error) {
return "", "", errors.New("dummy errors")
plugin := func(scope, namePrefix, opName string) (string, string, bool, error) {
return "", "", true, errors.New("dummy errors")
}
rc.PluginFunc = plugin
// not exist scope, use default
Expand All @@ -506,8 +514,8 @@ func TestConnector_MultiUpsert(t *testing.T) {
_, err := rc.MultiUpsert(ctx, testInfoRandom, testMultiValues)
assert.NoError(t, err)

plugin := func(scope, namePrefix, opName string) (string, string, error) {
return "", "", errors.New("dummy errors")
plugin := func(scope, namePrefix, opName string) (string, string, bool, error) {
return "", "", true, errors.New("dummy errors")
}
rc.PluginFunc = plugin
// not exist scope, use default
Expand Down Expand Up @@ -567,8 +575,8 @@ func TestConnector_Remove(t *testing.T) {
"c7": dosa.FieldValue(id)})
assert.NoError(t, err)

plugin := func(scope, namePrefix, opName string) (string, string, error) {
return "", "", errors.New("dummy errors")
plugin := func(scope, namePrefix, opName string) (string, string, bool, error) {
return "", "", true, errors.New("dummy errors")
}
rc.PluginFunc = plugin
// not exist scope, use default
Expand Down Expand Up @@ -656,8 +664,8 @@ func TestConnector_RemoveRange(t *testing.T) {
assert.NoError(t, err)
assert.Empty(t, data)

plugin := func(scope, namePrefix, opName string) (string, string, error) {
return "", "", errors.New("dummy errors")
plugin := func(scope, namePrefix, opName string) (string, string, bool, error) {
return "", "", true, errors.New("dummy errors")
}
rc.PluginFunc = plugin
// not exist scope, use default
Expand Down Expand Up @@ -686,8 +694,8 @@ func TestConnector_MultiRemove(t *testing.T) {
assert.NoError(t, err)
assert.NotNil(t, errs)

plugin := func(scope, namePrefix, opName string) (string, string, error) {
return "", "", errors.New("dummy errors")
plugin := func(scope, namePrefix, opName string) (string, string, bool, error) {
return "", "", true, errors.New("dummy errors")
}
rc.PluginFunc = plugin
// not exist scope, use default
Expand Down Expand Up @@ -818,8 +826,8 @@ func TestConnector_Range(t *testing.T) {
assert.Empty(t, data)
assert.Empty(t, token)

plugin := func(scope, namePrefix, opName string) (string, string, error) {
return "", "", errors.New("dummy errors")
plugin := func(scope, namePrefix, opName string) (string, string, bool, error) {
return "", "", true, errors.New("dummy errors")
}
rc.PluginFunc = plugin
// not exist scope, use default
Expand Down Expand Up @@ -869,8 +877,8 @@ func TestConnector_Scan(t *testing.T) {
assert.Empty(t, data)
assert.Empty(t, token)

plugin := func(scope, namePrefix, opName string) (string, string, error) {
return "", "", errors.New("dummy errors")
plugin := func(scope, namePrefix, opName string) (string, string, bool, error) {
return "", "", true, errors.New("dummy errors")
}
rc.PluginFunc = plugin
// not exist scope, use default
Expand Down

0 comments on commit a5b4ca8

Please sign in to comment.