Skip to content

Commit 255ca5c

Browse files
author
Maksim Konovalov
committed
pool: implemented Role Stringer interface
implemented Role Stringer interface for human-readable printing
1 parent f1d88dc commit 255ca5c

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Versioning](http://semver.org/spec/v2.0.0.html) except to the first release.
99
## [Unreleased]
1010

1111
### Added
12+
- Implemented Stringer interface for pool.Role type (#405).
1213
- Add err log to `ConnectionPool.Add()` in case, when unable to establish
1314
connection and ctx is not canceled;
1415
also added logs for error case of `ConnectionPool.tryConnect()` calls in

pool/const.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,14 @@ const (
3535
MasterRole // The instance is read-write mode.
3636
ReplicaRole // The instance is in read-only mode.
3737
)
38+
39+
var role2String = map[Role]string{
40+
UnknownRole: "unknown",
41+
MasterRole: "master",
42+
ReplicaRole: "replica",
43+
}
44+
45+
// String is implementation of the Stringer interface for human readability when printing a value
46+
func (r Role) String() string {
47+
return role2String[r]
48+
}

pool/const_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package pool
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
func TestRole_String(t *testing.T) {
11+
// Checking that print is non-number(len = 1 ) Role
12+
require.True(t, len(fmt.Sprintf("%v", UnknownRole)) > 1)
13+
require.True(t, len(fmt.Sprintf("%v", MasterRole)) > 1)
14+
require.True(t, len(fmt.Sprintf("%v", ReplicaRole)) > 1)
15+
}

0 commit comments

Comments
 (0)