initial import

This commit is contained in:
kevin
2020-07-26 17:09:05 +08:00
commit 7e3a369a8f
647 changed files with 54754 additions and 0 deletions

5
core/utils/report.go Normal file
View File

@@ -0,0 +1,5 @@
package utils
func Report(content string) {
// TODO: implement the report method
}

38
core/utils/times.go Normal file
View File

@@ -0,0 +1,38 @@
package utils
import (
"fmt"
"time"
"zero/core/timex"
)
type ElapsedTimer struct {
start time.Duration
}
func NewElapsedTimer() *ElapsedTimer {
return &ElapsedTimer{
start: timex.Now(),
}
}
func (et *ElapsedTimer) Duration() time.Duration {
return timex.Since(et.start)
}
func (et *ElapsedTimer) Elapsed() string {
return timex.Since(et.start).String()
}
func (et *ElapsedTimer) ElapsedMs() string {
return fmt.Sprintf("%.1fms", float32(timex.Since(et.start))/float32(time.Millisecond))
}
func CurrentMicros() int64 {
return time.Now().UnixNano() / int64(time.Microsecond)
}
func CurrentMillis() int64 {
return time.Now().UnixNano() / int64(time.Millisecond)
}

40
core/utils/times_test.go Normal file
View File

@@ -0,0 +1,40 @@
package utils
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
const sleepInterval = time.Millisecond * 10
func TestElapsedTimer_Duration(t *testing.T) {
timer := NewElapsedTimer()
time.Sleep(sleepInterval)
assert.True(t, timer.Duration() >= sleepInterval)
}
func TestElapsedTimer_Elapsed(t *testing.T) {
timer := NewElapsedTimer()
time.Sleep(sleepInterval)
duration, err := time.ParseDuration(timer.Elapsed())
assert.Nil(t, err)
assert.True(t, duration >= sleepInterval)
}
func TestElapsedTimer_ElapsedMs(t *testing.T) {
timer := NewElapsedTimer()
time.Sleep(sleepInterval)
duration, err := time.ParseDuration(timer.ElapsedMs())
assert.Nil(t, err)
assert.True(t, duration >= sleepInterval)
}
func TestCurrent(t *testing.T) {
currentMillis := CurrentMillis()
currentMicros := CurrentMicros()
assert.True(t, currentMillis > 0)
assert.True(t, currentMicros > 0)
assert.True(t, currentMillis*1000 <= currentMicros)
}

7
core/utils/uuid.go Normal file
View File

@@ -0,0 +1,7 @@
package utils
import "github.com/google/uuid"
func NewUuid() string {
return uuid.New().String()
}

11
core/utils/uuid_test.go Normal file
View File

@@ -0,0 +1,11 @@
package utils
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestUuid(t *testing.T) {
assert.Equal(t, 36, len(NewUuid()))
}

37
core/utils/version.go Normal file
View File

@@ -0,0 +1,37 @@
package utils
import (
"strconv"
"strings"
)
// returns -1 if the first version is lower than the second, 0 if they are equal, and 1 if the second is lower.
func CompareVersions(a, b string) int {
as := strings.Split(a, ".")
bs := strings.Split(b, ".")
var loop int
if len(as) > len(bs) {
loop = len(as)
} else {
loop = len(bs)
}
for i := 0; i < loop; i++ {
var x, y string
if len(as) > i {
x = as[i]
}
if len(bs) > i {
y = bs[i]
}
xi, _ := strconv.Atoi(x)
yi, _ := strconv.Atoi(y)
if xi > yi {
return 1
} else if xi < yi {
return -1
}
}
return 0
}

View File

@@ -0,0 +1,30 @@
package utils
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestCompareVersions(t *testing.T) {
cases := []struct {
ver1 string
ver2 string
out int
}{
{"1", "1.0.1", -1},
{"1.0.1", "1.0.2", -1},
{"1.0.3", "1.1", -1},
{"1.1", "1.1.1", -1},
{"1.3.2", "1.2", 1},
{"1.1.1", "1.1.1", 0},
{"1.1.0", "1.1", 0},
}
for _, each := range cases {
t.Run(each.ver1, func(t *testing.T) {
actual := CompareVersions(each.ver1, each.ver2)
assert.Equal(t, each.out, actual)
})
}
}