From b4ef3ea401069367fcf3310476093fb1be0d2466 Mon Sep 17 00:00:00 2001 From: Takumi2008 Date: Thu, 3 Jun 2021 15:44:15 -0500 Subject: [PATCH] support parsing rfc822 dataetime without day and run go fmt --- clock/rfc822.go | 24 +++++++++++++++++++++--- clock/rfc822_test.go | 28 ++++++++++++++++++++++++---- collections/lru_cache_test.go | 2 +- discovery/memberlist.go | 1 - errors/errors.go | 7 +++---- testutil/until.go | 1 - testutil/until_test.go | 4 ++-- 7 files changed, 51 insertions(+), 16 deletions(-) diff --git a/clock/rfc822.go b/clock/rfc822.go index c6a4dc25..a8fcb7d1 100644 --- a/clock/rfc822.go +++ b/clock/rfc822.go @@ -22,17 +22,35 @@ func ParseRFC822Time(s string) (Time, error) { if err == nil { return t, nil } - if parseErr, ok := err.(*ParseError); !ok || parseErr.LayoutElem != "MST" { + if parseErr, ok := err.(*ParseError); !ok || (parseErr.LayoutElem != "MST" && parseErr.LayoutElem != "Mon") { return Time{}, parseErr } if t, err = Parse("Mon, 2 Jan 2006 15:04:05 -0700", s); err == nil { return t, nil } - if parseErr, ok := err.(*ParseError); !ok || parseErr.LayoutElem != "" { + if parseErr, ok := err.(*ParseError); !ok || (parseErr.LayoutElem != "" && parseErr.LayoutElem != "Mon") { return Time{}, parseErr } if t, err = Parse("Mon, 2 Jan 2006 15:04:05 -0700 (MST)", s); err == nil { - return t, nil + return t, err + } + if parseErr, ok := err.(*ParseError); !ok || parseErr.LayoutElem != "Mon" { + return Time{}, err + } + if t, err = Parse("2 Jan 2006 15:04:05 MST", s); err == nil { + return t, err + } + if parseErr, ok := err.(*ParseError); !ok || parseErr.LayoutElem != "MST" { + return Time{}, parseErr + } + if t, err = Parse("2 Jan 2006 15:04:05 -0700", s); err == nil { + return t, err + } + if parseErr, ok := err.(*ParseError); !ok || parseErr.LayoutElem != "" { + return Time{}, parseErr + } + if t, err = Parse("2 Jan 2006 15:04:05 -0700 (MST)", s); err == nil { + return t, err } return Time{}, err } diff --git a/clock/rfc822_test.go b/clock/rfc822_test.go index 6f383f1b..ddf7959d 100644 --- a/clock/rfc822_test.go +++ b/clock/rfc822_test.go @@ -134,13 +134,33 @@ func TestRFC822UnmarshalingError(t *testing.T) { outError: `parsing time "Thu, 29 Aug 2019 11:20:07" as "Mon, 2 Jan 2006 15:04:05 -0700": cannot parse "" as "-0700"`, }, { inEncoded: `{"ts": "foo"}`, - outError: `parsing time "foo" as "Mon, 2 Jan 2006 15:04:05 MST": cannot parse "foo" as "Mon"`, + outError: `parsing time "foo" as "2 Jan 2006 15:04:05 MST": cannot parse "foo" as "2"`, }, { inEncoded: `{"ts": 42}`, outError: "invalid syntax", }} { - var ts testStruct - err := json.Unmarshal([]byte(tc.inEncoded), &ts) - assert.EqualError(t, err, tc.outError) + t.Run(tc.inEncoded, func(t *testing.T) { + var ts testStruct + err := json.Unmarshal([]byte(tc.inEncoded), &ts) + assert.EqualError(t, err, tc.outError) + }) + } +} + +func TestParseRFC822Time(t *testing.T) { + for _, tt := range []struct { + rfc822Time string + }{ + {"Thu, 3 Jun 2021 12:01:05 MST"}, + {"Thu, 3 Jun 2021 12:01:05 -0700"}, + {"Thu, 3 Jun 2021 12:01:05 -0700 (MST)"}, + {"2 Jun 2021 17:06:41 GMT"}, + {"2 Jun 2021 17:06:41 -0700"}, + {"2 Jun 2021 17:06:41 -0700 (MST)"}, + } { + t.Run(tt.rfc822Time, func(t *testing.T) { + _, err := ParseRFC822Time(tt.rfc822Time) + assert.NoError(t, err) + }) } } diff --git a/collections/lru_cache_test.go b/collections/lru_cache_test.go index e25a2872..bef00a8b 100644 --- a/collections/lru_cache_test.go +++ b/collections/lru_cache_test.go @@ -125,7 +125,7 @@ func TestLRUCacheMap(t *testing.T) { assert.Equal(t, 5, count) assert.Equal(t, 4, cache.Size()) - for _, item := range []int{1,2,4,5} { + for _, item := range []int{1, 2, 4, 5} { v, ok := cache.Get(fmt.Sprintf("%d", item)) require.True(t, ok) assert.Equal(t, item, v.(int)) diff --git a/discovery/memberlist.go b/discovery/memberlist.go index 1202f01c..57ab9b5c 100644 --- a/discovery/memberlist.go +++ b/discovery/memberlist.go @@ -149,7 +149,6 @@ func (m *MemberList) Close(ctx context.Context) error { case err := <-errCh: return err } - return nil } func (m *MemberList) GetPeers(_ context.Context) ([]Peer, error) { diff --git a/errors/errors.go b/errors/errors.go index 90beee9a..c01ae2dc 100644 --- a/errors/errors.go +++ b/errors/errors.go @@ -104,17 +104,16 @@ import ( // New also records the stack trace at the depth specified. func NewWithDepth(message string, depth int) error { return &fundamental{ - msg: message, + msg: message, CallStack: stack.New(depth), } } - // New returns an error with the supplied message. // New also records the stack trace at the point it was called. func New(message string) error { return &fundamental{ - msg: message, + msg: message, CallStack: stack.New(1), } } @@ -124,7 +123,7 @@ func New(message string) error { // Errorf also records the stack trace at the point it was called. func Errorf(format string, args ...interface{}) error { return &fundamental{ - msg: fmt.Sprintf(format, args...), + msg: fmt.Sprintf(format, args...), CallStack: stack.New(1), } } diff --git a/testutil/until.go b/testutil/until.go index 8a5386d5..0948a725 100644 --- a/testutil/until.go +++ b/testutil/until.go @@ -70,4 +70,3 @@ func UntilConnect(t TestingT, a int, d time.Duration, addr string) { t.Errorf("never connected to TCP server at '%s' after %d attempts", addr, a) t.FailNow() } - diff --git a/testutil/until_test.go b/testutil/until_test.go index a2739245..8fbb6146 100644 --- a/testutil/until_test.go +++ b/testutil/until_test.go @@ -37,7 +37,7 @@ func TestUntilPass(t *testing.T) { if r.Method == http.MethodPost { // Sleep some rand amount to time to simulate some // async process happening on the server - time.Sleep(time.Duration(rand.Intn(10))*time.Millisecond) + time.Sleep(time.Duration(rand.Intn(10)) * time.Millisecond) // Set the value value = r.FormValue("value") } else { @@ -67,4 +67,4 @@ func TestUntilPass(t *testing.T) { assert.Equal(t, "batch job completed\n", string(b)) }) -} \ No newline at end of file +}