fix: refactor code to be testable #181
This commit was merged in pull request #212.
This commit is contained in:
@@ -10,20 +10,20 @@ import (
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type RandomGenerator interface {
|
||||
type RandomService interface {
|
||||
Bytes(size int) ([]byte, error)
|
||||
String(size int) (string, error)
|
||||
UUID() (uuid.UUID, error)
|
||||
}
|
||||
|
||||
type RandomGeneratorImpl struct {
|
||||
type RandomServiceImpl struct {
|
||||
}
|
||||
|
||||
func NewRandomGeneratorImpl() *RandomGeneratorImpl {
|
||||
return &RandomGeneratorImpl{}
|
||||
func NewRandomServiceImpl() *RandomServiceImpl {
|
||||
return &RandomServiceImpl{}
|
||||
}
|
||||
|
||||
func (r *RandomGeneratorImpl) Bytes(size int) ([]byte, error) {
|
||||
func (r *RandomServiceImpl) Bytes(size int) ([]byte, error) {
|
||||
b := make([]byte, 32)
|
||||
_, err := rand.Read(b)
|
||||
if err != nil {
|
||||
@@ -34,7 +34,7 @@ func (r *RandomGeneratorImpl) Bytes(size int) ([]byte, error) {
|
||||
return b, nil
|
||||
}
|
||||
|
||||
func (r *RandomGeneratorImpl) String(size int) (string, error) {
|
||||
func (r *RandomServiceImpl) String(size int) (string, error) {
|
||||
bytes, err := r.Bytes(size)
|
||||
if err != nil {
|
||||
return "", types.ErrInternal
|
||||
@@ -43,6 +43,6 @@ func (r *RandomGeneratorImpl) String(size int) (string, error) {
|
||||
return base64.StdEncoding.EncodeToString(bytes), nil
|
||||
}
|
||||
|
||||
func (r *RandomGeneratorImpl) UUID() (uuid.UUID, error) {
|
||||
func (r *RandomServiceImpl) UUID() (uuid.UUID, error) {
|
||||
return uuid.NewRandom()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user