Skip to content

Commit

Permalink
Add String() methods to all components of the routing connector (#391)
Browse files Browse the repository at this point in the history
* Add String() methods to all components of the routing connector

* better test
  • Loading branch information
phliar authored Jul 15, 2019
1 parent fcdd409 commit fdb6352
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Changelog

## v3.4.7 (unreleased)
- Nothing changed yet
- Add String() method to all components of the routing connector

## v3.4.6 (2019-06-11)
- use append but not copy to clone
Expand Down
13 changes: 13 additions & 0 deletions connectors/routing/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ package routing
import (
"fmt"
"sort"
"strings"

"github.com/pkg/errors"
)
Expand Down Expand Up @@ -119,3 +120,15 @@ func (c *Config) findDefaultRouter() *Rule {
}
return nil
}

func (r *Routers) String() string {
s := []string{}
for _, rule := range *r {
s = append(s, rule.String())
}
return "[" + strings.Join(s, ",") + "]"
}

func (c *Config) String() string {
return c.Routers.String()
}
15 changes: 15 additions & 0 deletions connectors/routing/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package routing

import (
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -67,6 +68,20 @@ routers:
assert.Equal(t, testCfg.Routers, rs)
err = yaml.Unmarshal([]byte(`bad yaml file`), testCfg)
assert.Error(t, err)

s := []string{
"{production.serviceA -> cassandra}",
"{production.default -> cassandra}",
"{ebook.ebook-store -> ebook}",
"{ebook.default -> ebook}",
"{ebook.apple.* -> ebook}",
"{ebook.* -> ebook}",
"{development.serviceB -> cassandra}",
"{development.default -> cassandra}",
"{default.default -> dosa}",
}

assert.Equal(t, "["+strings.Join(s, ",")+"]", rs.String())
}

func buildRouter(scope, namePrefix, connector string) *Rule {
Expand Down
4 changes: 4 additions & 0 deletions connectors/routing/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ func NewConnector(cfg Config, connectorMap map[string]dosa.Connector) *Connector
}
}

func (rc *Connector) String() string {
return fmt.Sprintf("[Routing %s]", rc.config.String())
}

// get connector by scope an namePrefix
func (rc *Connector) getConnector(scope, namePrefix string) (dosa.Connector, error) {
router := rc.config.FindRouter(scope, namePrefix)
Expand Down
13 changes: 13 additions & 0 deletions connectors/routing/connector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"context"
"reflect"
"sort"
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -49,6 +50,17 @@ var (
buildRouter("default", "default", "memory"),
},
}
routersStr = strings.Join([]string{
"{production.map -> memory}",
"{production.default -> random}",
"{ebook.ebook-store -> memory}",
"{ebook.default -> random}",
"{ebook.apple.* -> memory}",
"{ebook.* -> devnull}",
"{development.map -> memory}",
"{development.default -> random}",
"{default.default -> memory}",
}, ",")
testInfo = &dosa.EntityInfo{
Ref: &dosa.SchemaRef{
Scope: "production",
Expand Down Expand Up @@ -170,6 +182,7 @@ func TestGetConnector(t *testing.T) {
// no plugin
// glob match
rc := NewConnector(cfg, connectorMap)
assert.Equal(t, "[Routing ["+routersStr+"]]", rc.String())
ei := &dosa.EntityInfo{
Ref: &dosa.SchemaRef{Scope: "ebook", NamePrefix: "apple.v1"},
}
Expand Down
5 changes: 5 additions & 0 deletions connectors/routing/routing_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package routing

import (
"fmt"
"strings"

"github.com/gobwas/glob"
Expand Down Expand Up @@ -75,3 +76,7 @@ func (r *Rule) RouteTo(scope string, namePrefix string) bool {

return false
}

func (r *Rule) String() string {
return fmt.Sprintf("{%s.%s -> %s}", r.Scope, r.NamePrefix, r.Connector)
}
6 changes: 6 additions & 0 deletions connectors/routing/routing_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,9 @@ func TestTestNewRoutingConfigError(t *testing.T) {
assert.Nil(t, rConfig)
assert.Contains(t, err.Error(), "scope could not be empty")
}

func TestString(t *testing.T) {
r, err := NewRule("production", "test", "memory")
assert.Nil(t, err)
assert.Equal(t, "{production.test -> memory}", r.String())
}

0 comments on commit fdb6352

Please sign in to comment.