From 07d681fc10f9b322cb0ae18c21a8e76b5f021679 Mon Sep 17 00:00:00 2001 From: Brandur Date: Tue, 13 Mar 2018 09:33:12 -0700 Subject: [PATCH] Use named field names in composite literals As #35 was coming in, I noticed that as of Go 1.10, `go vet` complains about the use of unkeyed fields in composite literals: ``` go vet ./... ./example_test.go:26: github.com/throttled/throttled.RateQuota composite literal uses unkeyed fields ./example_test.go:51: github.com/throttled/throttled.RateQuota composite literal uses unkeyed fields ./http_test.go:19: github.com/throttled/throttled.RateLimitResult composite literal uses unkeyed fields ./http_test.go:23: github.com/throttled/throttled.RateLimitResult composite literal uses unkeyed fields ./rate_test.go:41: github.com/throttled/throttled.RateQuota composite literal uses unkeyed fields ./rate_test.go:114: github.com/throttled/throttled.RateQuota composite literal uses unkeyed fields ``` Which is unfortunately failing the build for the `1.x` version. This patch names fields wherever we were using the unkeyed kind. --- example_test.go | 4 ++-- http_test.go | 19 ++++++++++++++++--- rate_test.go | 4 ++-- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/example_test.go b/example_test.go index 7c4c5df..160469e 100644 --- a/example_test.go +++ b/example_test.go @@ -23,7 +23,7 @@ func ExampleHTTPRateLimiter() { } // Maximum burst of 5 which refills at 20 tokens per minute. - quota := throttled.RateQuota{throttled.PerMin(20), 5} + quota := throttled.RateQuota{MaxRate: throttled.PerMin(20), MaxBurst: 5} rateLimiter, err := throttled.NewGCRARateLimiter(store, quota) if err != nil { @@ -48,7 +48,7 @@ func ExampleGCRARateLimiter() { } // Maximum burst of 5 which refills at 1 token per hour. - quota := throttled.RateQuota{throttled.PerHour(1), 5} + quota := throttled.RateQuota{MaxRate: throttled.PerHour(1), MaxBurst: 5} rateLimiter, err := throttled.NewGCRARateLimiter(store, quota) if err != nil { diff --git a/http_test.go b/http_test.go index 52a706e..7d9c9e1 100644 --- a/http_test.go +++ b/http_test.go @@ -16,11 +16,24 @@ type stubLimiter struct { func (sl *stubLimiter) RateLimit(key string, quantity int) (bool, throttled.RateLimitResult, error) { switch key { case "limit": - return true, throttled.RateLimitResult{-1, -1, -1, time.Minute}, nil + result := throttled.RateLimitResult{ + Limit: -1, + Remaining: -1, + ResetAfter: -1, + RetryAfter: time.Minute, + } + return true, result, nil case "error": - return false, throttled.RateLimitResult{}, errors.New("stubLimiter error") + result := throttled.RateLimitResult{} + return false, result, errors.New("stubLimiter error") default: - return false, throttled.RateLimitResult{1, 2, time.Minute, -1}, nil + result := throttled.RateLimitResult{ + Limit: 1, + Remaining: 2, + ResetAfter: time.Minute, + RetryAfter: -1, + } + return false, result, nil } } diff --git a/rate_test.go b/rate_test.go index a1a5f62..a218343 100644 --- a/rate_test.go +++ b/rate_test.go @@ -38,7 +38,7 @@ func (ts *testStore) CompareAndSwapWithTTL(key string, old, new int64, ttl time. func TestRateLimit(t *testing.T) { limit := 5 - rq := throttled.RateQuota{throttled.PerSec(1), limit - 1} + rq := throttled.RateQuota{MaxRate: throttled.PerSec(1), MaxBurst: limit - 1} start := time.Unix(0, 0) cases := []struct { now time.Time @@ -111,7 +111,7 @@ func TestRateLimit(t *testing.T) { } func TestRateLimitUpdateFailures(t *testing.T) { - rq := throttled.RateQuota{throttled.PerSec(1), 1} + rq := throttled.RateQuota{MaxRate: throttled.PerSec(1), MaxBurst: 1} mst, err := memstore.New(0) if err != nil { t.Fatal(err)