- The removed functions are not used in Iavl, Cosmos-sdk and tendermint repos - Code-hygenie `whoop whoop` Signed-off-by: Marko Baricevic <marbar3778@yahoo.com>pull/3788/head
@ -1,43 +0,0 @@ | |||
package common | |||
import ( | |||
"strings" | |||
"time" | |||
"github.com/pkg/errors" | |||
) | |||
// TimeLayout helps to parse a date string of the format YYYY-MM-DD | |||
// Intended to be used with the following function: | |||
// time.Parse(TimeLayout, date) | |||
var TimeLayout = "2006-01-02" //this represents YYYY-MM-DD | |||
// ParseDateRange parses a date range string of the format start:end | |||
// where the start and end date are of the format YYYY-MM-DD. | |||
// The parsed dates are time.Time and will return the zero time for | |||
// unbounded dates, ex: | |||
// unbounded start: :2000-12-31 | |||
// unbounded end: 2000-12-31: | |||
func ParseDateRange(dateRange string) (startDate, endDate time.Time, err error) { | |||
dates := strings.Split(dateRange, ":") | |||
if len(dates) != 2 { | |||
err = errors.New("bad date range, must be in format date:date") | |||
return | |||
} | |||
parseDate := func(date string) (out time.Time, err error) { | |||
if len(date) == 0 { | |||
return | |||
} | |||
out, err = time.Parse(TimeLayout, date) | |||
return | |||
} | |||
startDate, err = parseDate(dates[0]) | |||
if err != nil { | |||
return | |||
} | |||
endDate, err = parseDate(dates[1]) | |||
if err != nil { | |||
return | |||
} | |||
return | |||
} |
@ -1,46 +0,0 @@ | |||
package common | |||
import ( | |||
"testing" | |||
"time" | |||
"github.com/stretchr/testify/assert" | |||
) | |||
var ( | |||
date = time.Date(2015, time.Month(12), 31, 0, 0, 0, 0, time.UTC) | |||
date2 = time.Date(2016, time.Month(12), 31, 0, 0, 0, 0, time.UTC) | |||
zero time.Time | |||
) | |||
func TestParseDateRange(t *testing.T) { | |||
assert := assert.New(t) | |||
var testDates = []struct { | |||
dateStr string | |||
start time.Time | |||
end time.Time | |||
errNil bool | |||
}{ | |||
{"2015-12-31:2016-12-31", date, date2, true}, | |||
{"2015-12-31:", date, zero, true}, | |||
{":2016-12-31", zero, date2, true}, | |||
{"2016-12-31", zero, zero, false}, | |||
{"2016-31-12:", zero, zero, false}, | |||
{":2016-31-12", zero, zero, false}, | |||
} | |||
for _, test := range testDates { | |||
start, end, err := ParseDateRange(test.dateStr) | |||
if test.errNil { | |||
assert.Nil(err) | |||
testPtr := func(want, have time.Time) { | |||
assert.True(have.Equal(want)) | |||
} | |||
testPtr(test.start, start) | |||
testPtr(test.end, end) | |||
} else { | |||
assert.NotNil(err) | |||
} | |||
} | |||
} |
@ -1,74 +0,0 @@ | |||
package common | |||
import ( | |||
"bytes" | |||
"errors" | |||
"io" | |||
) | |||
type PrefixedReader struct { | |||
Prefix []byte | |||
reader io.Reader | |||
} | |||
func NewPrefixedReader(prefix []byte, reader io.Reader) *PrefixedReader { | |||
return &PrefixedReader{prefix, reader} | |||
} | |||
func (pr *PrefixedReader) Read(p []byte) (n int, err error) { | |||
if len(pr.Prefix) > 0 { | |||
read := copy(p, pr.Prefix) | |||
pr.Prefix = pr.Prefix[read:] | |||
return read, nil | |||
} | |||
return pr.reader.Read(p) | |||
} | |||
// NOTE: Not goroutine safe | |||
type BufferCloser struct { | |||
bytes.Buffer | |||
Closed bool | |||
} | |||
func NewBufferCloser(buf []byte) *BufferCloser { | |||
return &BufferCloser{ | |||
*bytes.NewBuffer(buf), | |||
false, | |||
} | |||
} | |||
func (bc *BufferCloser) Close() error { | |||
if bc.Closed { | |||
return errors.New("BufferCloser already closed") | |||
} | |||
bc.Closed = true | |||
return nil | |||
} | |||
func (bc *BufferCloser) Write(p []byte) (n int, err error) { | |||
if bc.Closed { | |||
return 0, errors.New("Cannot write to closed BufferCloser") | |||
} | |||
return bc.Buffer.Write(p) | |||
} | |||
func (bc *BufferCloser) WriteByte(c byte) error { | |||
if bc.Closed { | |||
return errors.New("Cannot write to closed BufferCloser") | |||
} | |||
return bc.Buffer.WriteByte(c) | |||
} | |||
func (bc *BufferCloser) WriteRune(r rune) (n int, err error) { | |||
if bc.Closed { | |||
return 0, errors.New("Cannot write to closed BufferCloser") | |||
} | |||
return bc.Buffer.WriteRune(r) | |||
} | |||
func (bc *BufferCloser) WriteString(s string) (n int, err error) { | |||
if bc.Closed { | |||
return 0, errors.New("Cannot write to closed BufferCloser") | |||
} | |||
return bc.Buffer.WriteString(s) | |||
} |
@ -1,46 +0,0 @@ | |||
package common | |||
import ( | |||
"os" | |||
"testing" | |||
) | |||
func TestOSGoPath(t *testing.T) { | |||
// restore original gopath upon exit | |||
path := os.Getenv("GOPATH") | |||
defer func() { | |||
_ = os.Setenv("GOPATH", path) | |||
}() | |||
err := os.Setenv("GOPATH", "~/testgopath") | |||
if err != nil { | |||
t.Fatal(err) | |||
} | |||
path = GoPath() | |||
if path != "~/testgopath" { | |||
t.Fatalf("should get GOPATH env var value, got %v", path) | |||
} | |||
os.Unsetenv("GOPATH") | |||
path = GoPath() | |||
if path != "~/testgopath" { | |||
t.Fatalf("subsequent calls should return the same value, got %v", path) | |||
} | |||
} | |||
func TestOSGoPathWithoutEnvVar(t *testing.T) { | |||
// restore original gopath upon exit | |||
path := os.Getenv("GOPATH") | |||
defer func() { | |||
_ = os.Setenv("GOPATH", path) | |||
}() | |||
os.Unsetenv("GOPATH") | |||
// reset cache | |||
gopath = "" | |||
path = GoPath() | |||
if path == "" || path == "~/testgopath" { | |||
t.Fatalf("should get nonempty result of calling go env GOPATH, got %v", path) | |||
} | |||
} |