good morning!!!!

Skip to content
Snippets Groups Projects
Commit c609eb44 authored by José Carlos Nieto's avatar José Carlos Nieto
Browse files

fix: Moving Cluster() from db package into the mongo adapter.

parent 28ce1450
Branches
Tags
No related merge requests found
......@@ -31,6 +31,11 @@ import (
const connectionScheme = `mongodb`
// cluster is an array of hosts or sockets.
type cluster struct {
address []db.Address
}
// ConnectionURL implements a MongoDB connection struct.
type ConnectionURL struct {
User string
......@@ -84,6 +89,53 @@ func (c ConnectionURL) String() (s string) {
return u.String()
}
// String returns the string representation of the cluster struct.
func (c cluster) String() string {
l := len(c.address)
addresses := make([]string, 0, l)
for i := 0; i < l; i++ {
addresses = append(addresses, c.address[i].String())
}
return strings.Join(addresses, ",")
}
// Host is undefined in a cluster struct.
func (c cluster) Host() (string, error) {
return "", db.ErrUndefined
}
// Port is undefined in a cluster struct.
func (c cluster) Port() (uint, error) {
return 0, db.ErrUndefined
}
// Path is undefined in a cluster struct.
func (c cluster) Path() (string, error) {
return "", db.ErrUndefined
}
// Cluster returns the array of addresses in a cluster struct.
func (c cluster) Cluster() ([]db.Address, error) {
return c.address, nil
}
// Cluster converts the given Address structures into a cluster structure.
func Cluster(addresses ...db.Address) cluster {
return cluster{address: addresses}
}
// ParseCluster parses s into a cluster structure.
func ParseCluster(s string) (c cluster) {
var hosts []string
hosts = strings.Split(s, ",")
l := len(hosts)
for i := 0; i < l; i++ {
c.address = append(c.address, db.ParseAddress(hosts[i]))
}
return
}
// ParseURL parses s into a ConnectionURL struct.
func ParseURL(s string) (conn ConnectionURL, err error) {
var u *url.URL
......
......@@ -68,7 +68,7 @@ func TestConnectionURL(t *testing.T) {
}
// Setting cluster.
c.Address = db.Cluster(db.Host("localhost"), db.Host("1.2.3.4"), db.HostPort("example.org", 1234))
c.Address = Cluster(db.Host("localhost"), db.Host("1.2.3.4"), db.HostPort("example.org", 1234))
if c.String() != "mongodb://user:pass@localhost,1.2.3.4,example.org:1234/myfilename?cache=foobar&mode=ro" {
t.Fatal(`Test failed, got:`, c.String())
......
......@@ -33,7 +33,6 @@ type Address interface {
Host() (string, error)
Port() (uint, error)
Path() (string, error)
Cluster() ([]Address, error)
}
// socket is a UNIX address.
......@@ -47,22 +46,6 @@ type host struct {
port uint
}
// cluster is an array of hosts or sockets.
type cluster struct {
address []Address
}
// ParseCluster parses s into a cluster structure.
func ParseCluster(s string) (c cluster) {
var hosts []string
hosts = strings.Split(s, ",")
l := len(hosts)
for i := 0; i < l; i++ {
c.address = append(c.address, ParseAddress(hosts[i]))
}
return
}
// ParseAddress parses s into a host or socket structures.
func ParseAddress(s string) Address {
if strings.HasPrefix(s, "/") {
......@@ -95,11 +78,6 @@ func HostPort(name string, port uint) host {
return host{name: name}
}
// Cluster converts the given Address structures into a cluster structure.
func Cluster(addresses ...Address) cluster {
return cluster{address: addresses}
}
// String returns the string representation of the host struct.
func (h host) String() string {
if h.port > 0 {
......@@ -129,11 +107,6 @@ func (h host) Path() (string, error) {
return "", ErrUndefined
}
// Cluster returns an array with a single host struct.
func (h host) Cluster() ([]Address, error) {
return []Address{h}, nil
}
// String() returns the string representation of the socket struct.
func (s socket) String() string {
return s.path
......@@ -153,39 +126,3 @@ func (s socket) Port() (uint, error) {
func (s socket) Path() (string, error) {
return s.path, nil
}
// Cluster returns an array with a single socket struct.
func (s socket) Cluster() ([]Address, error) {
return []Address{s}, nil
}
// String returns the string representation of the cluster struct.
func (c cluster) String() string {
l := len(c.address)
addresses := make([]string, 0, l)
for i := 0; i < l; i++ {
addresses = append(addresses, c.address[i].String())
}
return strings.Join(addresses, ",")
}
// Host is undefined in a cluster struct.
func (c cluster) Host() (string, error) {
return "", ErrUndefined
}
// Port is undefined in a cluster struct.
func (c cluster) Port() (uint, error) {
return 0, ErrUndefined
}
// Path is undefined in a cluster struct.
func (c cluster) Path() (string, error) {
return "", ErrUndefined
}
// Cluster returns the array of addresses in a cluster struct.
func (c cluster) Cluster() ([]Address, error) {
return c.address, nil
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment