From c376ffc35174fac1f367ca04c8beae2e4daa4db0 Mon Sep 17 00:00:00 2001 From: Kevin Wan Date: Fri, 19 Feb 2021 14:30:38 +0800 Subject: [PATCH] fix golint issues in core/filex (#485) --- core/filex/file.go | 6 ++++-- core/filex/lookup.go | 3 +++ core/filex/progressscanner.go | 4 ++++ core/filex/rangereader.go | 3 +++ core/stores/redis/redis_test.go | 17 ++++++++--------- 5 files changed, 22 insertions(+), 11 deletions(-) diff --git a/core/filex/file.go b/core/filex/file.go index af0794bb..e82fd8c8 100644 --- a/core/filex/file.go +++ b/core/filex/file.go @@ -7,6 +7,7 @@ import ( const bufSize = 1024 +// FirstLine returns the first line of the file. func FirstLine(filename string) (string, error) { file, err := os.Open(filename) if err != nil { @@ -17,6 +18,7 @@ func FirstLine(filename string) (string, error) { return firstLine(file) } +// LastLine returns the last line of the file. func LastLine(filename string) (string, error) { file, err := os.Open(filename) if err != nil { @@ -69,11 +71,11 @@ func lastLine(filename string, file *os.File) (string, error) { if buf[n-1] == '\n' { buf = buf[:n-1] - n -= 1 + n-- } else { buf = buf[:n] } - for n -= 1; n >= 0; n-- { + for n--; n >= 0; n-- { if buf[n] == '\n' { return string(append(buf[n+1:], last...)), nil } diff --git a/core/filex/lookup.go b/core/filex/lookup.go index 9dfc5c68..d69e42b4 100644 --- a/core/filex/lookup.go +++ b/core/filex/lookup.go @@ -5,12 +5,15 @@ import ( "os" ) +// OffsetRange represents a content block of a file. type OffsetRange struct { File string Start int64 Stop int64 } +// SplitLineChunks splits file into chunks. +// The whole line are guaranteed to be split in the same chunk. func SplitLineChunks(filename string, chunks int) ([]OffsetRange, error) { info, err := os.Stat(filename) if err != nil { diff --git a/core/filex/progressscanner.go b/core/filex/progressscanner.go index be0e8611..11f3c1a5 100644 --- a/core/filex/progressscanner.go +++ b/core/filex/progressscanner.go @@ -3,8 +3,11 @@ package filex import "gopkg.in/cheggaaa/pb.v1" type ( + // A Scanner is used to read lines. Scanner interface { + // Scan checks if has remaining to read. Scan() bool + // Text returns next line. Text() string } @@ -14,6 +17,7 @@ type ( } ) +// NewProgressScanner returns a Scanner with progress indicator. func NewProgressScanner(scanner Scanner, bar *pb.ProgressBar) Scanner { return &progressScanner{ Scanner: scanner, diff --git a/core/filex/rangereader.go b/core/filex/rangereader.go index 74f6fcbe..cd69f678 100644 --- a/core/filex/rangereader.go +++ b/core/filex/rangereader.go @@ -5,12 +5,14 @@ import ( "os" ) +// A RangeReader is used to read a range of content from a file. type RangeReader struct { file *os.File start int64 stop int64 } +// NewRangeReader returns a RangeReader, which will read the range of content from file. func NewRangeReader(file *os.File, start, stop int64) *RangeReader { return &RangeReader{ file: file, @@ -19,6 +21,7 @@ func NewRangeReader(file *os.File, start, stop int64) *RangeReader { } } +// Read reads the range of content into p. func (rr *RangeReader) Read(p []byte) (n int, err error) { stat, err := rr.file.Stat() if err != nil { diff --git a/core/stores/redis/redis_test.go b/core/stores/redis/redis_test.go index 4b009a0c..c76e0fc0 100644 --- a/core/stores/redis/redis_test.go +++ b/core/stores/redis/redis_test.go @@ -341,39 +341,38 @@ func TestRedis_GetBit(t *testing.T) { }) } -func TestRedis_BitCount(t *testing.T) { +func TestRedis_BitCount(t *testing.T) { runOnRedis(t, func(client *Redis) { - for i := 0; i < 11; i++{ + for i := 0; i < 11; i++ { err := client.SetBit("key", int64(i), 1) assert.Nil(t, err) } - _, err := NewRedis(client.Addr,"").BitCount("key",0,-1) + _, err := NewRedis(client.Addr, "").BitCount("key", 0, -1) assert.NotNil(t, err) - val, err := client.BitCount("key", 0,-1) + val, err := client.BitCount("key", 0, -1) assert.Nil(t, err) assert.Equal(t, int64(11), val) - val, err = client.BitCount("key", 0,0) + val, err = client.BitCount("key", 0, 0) assert.Nil(t, err) assert.Equal(t, int64(8), val) - val, err = client.BitCount("key", 1,1) + val, err = client.BitCount("key", 1, 1) assert.Nil(t, err) assert.Equal(t, int64(3), val) - val, err = client.BitCount("key", 0,1) + val, err = client.BitCount("key", 0, 1) assert.Nil(t, err) assert.Equal(t, int64(11), val) - val, err = client.BitCount("key", 2,2) + val, err = client.BitCount("key", 2, 2) assert.Nil(t, err) assert.Equal(t, int64(0), val) }) } - func TestRedis_Persist(t *testing.T) { runOnRedis(t, func(client *Redis) { _, err := NewRedis(client.Addr, "").Persist("key")