diff --git a/rate.go b/rate.go index 5b22409..f3f917c 100644 --- a/rate.go +++ b/rate.go @@ -1,10 +1,11 @@ package limiter import ( - "fmt" "strconv" "strings" "time" + + "github.com/pkg/errors" ) // Rate is the rate. @@ -20,7 +21,7 @@ func NewRateFromFormatted(formatted string) (Rate, error) { values := strings.Split(formatted, "-") if len(values) != 2 { - return rate, fmt.Errorf("Incorrect format '%s'", formatted) + return rate, errors.Errorf("incorrect format '%s'", formatted) } periods := map[string]time.Duration{ @@ -32,26 +33,21 @@ func NewRateFromFormatted(formatted string) (Rate, error) { limit, period := values[0], strings.ToUpper(values[1]) duration, ok := periods[period] - if !ok { - return rate, fmt.Errorf("Incorrect period '%s'", period) + return rate, errors.Errorf("incorrect period '%s'", period) } - var ( - p time.Duration - l int - ) - - p = 1 * duration - - l, err := strconv.Atoi(limit) + p := 1 * duration + l, err := strconv.ParseInt(limit, 10, 64) if err != nil { - return rate, fmt.Errorf("Incorrect limit '%s'", limit) + return rate, errors.Errorf("incorrect limit '%s'", limit) } - return Rate{ + rate = Rate{ Formatted: formatted, Period: p, - Limit: int64(l), - }, nil + Limit: l, + } + + return rate, nil } diff --git a/rate_test.go b/rate_test.go index dc64aac..3078570 100644 --- a/rate_test.go +++ b/rate_test.go @@ -5,11 +5,13 @@ import ( "testing" "time" - "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) // TestRate tests Rate methods. func TestRate(t *testing.T) { + is := require.New(t) + expected := map[string]Rate{ "10-S": { Formatted: "10-S", @@ -30,8 +32,8 @@ func TestRate(t *testing.T) { for k, v := range expected { r, err := NewRateFromFormatted(k) - assert.Nil(t, err) - assert.True(t, reflect.DeepEqual(v, r)) + is.NoError(err) + is.True(reflect.DeepEqual(v, r)) } wrongs := []string{ @@ -44,7 +46,7 @@ func TestRate(t *testing.T) { for _, w := range wrongs { _, err := NewRateFromFormatted(w) - assert.NotNil(t, err) + is.Error(err) } }