diff --git a/CHANGELOG.md b/CHANGELOG.md index 4ef3cb4e0..aa0bfc0e1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ Versioning](http://semver.org/spec/v2.0.0.html) except to the first release. also added logs for error case of `ConnectionPool.tryConnect()` calls in `ConnectionPool.controller()` and `ConnectionPool.reconnect()` - Methods that are implemented but not included in the pooler interface (#395). +- Implemented stringer methods for pool.Role (#405). ### Changed diff --git a/pool/const.go b/pool/const.go index 7ec239f7d..b1748ae52 100644 --- a/pool/const.go +++ b/pool/const.go @@ -1,3 +1,4 @@ +//go:generate stringer -type Role -linecomment package pool /* @@ -31,7 +32,10 @@ const ( type Role uint32 const ( - UnknownRole Role = iota // A connection pool failed to discover mode of the instance. - MasterRole // The instance is read-write mode. - ReplicaRole // The instance is in read-only mode. + // UnknownRole - the connection pool was unable to detect the instance mode. + UnknownRole Role = iota // unknown + // MasterRole - the instance is in read-write mode. + MasterRole // master + // ReplicaRole - the instance is in read-only mode. + ReplicaRole // replica ) diff --git a/pool/const_test.go b/pool/const_test.go new file mode 100644 index 000000000..83e2678de --- /dev/null +++ b/pool/const_test.go @@ -0,0 +1,13 @@ +package pool + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestRole_String(t *testing.T) { + require.Equal(t, "unknown", UnknownRole.String()) + require.Equal(t, "master", MasterRole.String()) + require.Equal(t, "replica", ReplicaRole.String()) +} diff --git a/pool/role_string.go b/pool/role_string.go new file mode 100644 index 000000000..162ebd698 --- /dev/null +++ b/pool/role_string.go @@ -0,0 +1,25 @@ +// Code generated by "stringer -type Role -linecomment"; DO NOT EDIT. + +package pool + +import "strconv" + +func _() { + // An "invalid array index" compiler error signifies that the constant values have changed. + // Re-run the stringer command to generate them again. + var x [1]struct{} + _ = x[UnknownRole-0] + _ = x[MasterRole-1] + _ = x[ReplicaRole-2] +} + +const _Role_name = "unknownmasterreplica" + +var _Role_index = [...]uint8{0, 7, 13, 20} + +func (i Role) String() string { + if i >= Role(len(_Role_index)-1) { + return "Role(" + strconv.FormatInt(int64(i), 10) + ")" + } + return _Role_name[_Role_index[i]:_Role_index[i+1]] +}