Commit be162b7f authored by suyanlong's avatar suyanlong

Add about file function

parent bb681342
Pipeline #8073 failed with stages
......@@ -166,6 +166,8 @@ require (
github.com/tidwall/match v1.0.3 // indirect
github.com/tidwall/pretty v1.0.2 // indirect
github.com/ugorji/go/codec v1.1.7 // indirect
github.com/vmihailenco/msgpack/v5 v5.3.4 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
github.com/wangjia184/sortedset v0.0.0-20210325043434-64dd27e173e2 // indirect
github.com/whyrusleeping/go-keyspace v0.0.0-20160322163242-5b898ac5add1 // indirect
github.com/whyrusleeping/multiaddr-filter v0.0.0-20160516205228-e903e4adabd7 // indirect
......
......@@ -960,6 +960,10 @@ github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyC
github.com/valyala/fasthttp v1.2.0/go.mod h1:4vX61m6KN+xDduDNwXrhIAVZaZaZiQ1luJk8LWSxF3s=
github.com/valyala/quicktemplate v1.2.0/go.mod h1:EH+4AkTd43SvgIbQHYu59/cJyxDoOVRUAfrukLPuGJ4=
github.com/valyala/tcplisten v0.0.0-20161114210144-ceec8f93295a/go.mod h1:v3UYOV9WzVtRmSR+PDvWpU/qWl4Wa5LApYYX4ZtKbio=
github.com/vmihailenco/msgpack/v5 v5.3.4 h1:qMKAwOV+meBw2Y8k9cVwAy7qErtYCwBzZ2ellBfvnqc=
github.com/vmihailenco/msgpack/v5 v5.3.4/go.mod h1:7xyJ9e+0+9SaZT0Wt1RGleJXzli6Q/V5KbhBonMG9jc=
github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g=
github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds=
github.com/wangjia184/sortedset v0.0.0-20160527075905-f5d03557ba30/go.mod h1:YkocrP2K2tcw938x9gCOmT5G5eCD6jsTz0SZuyAqwIE=
github.com/wangjia184/sortedset v0.0.0-20210325043434-64dd27e173e2 h1:ViCftzy+iQHmxIJPZqJH3i0znSxFufq075LnMwt2/VQ=
github.com/wangjia184/sortedset v0.0.0-20210325043434-64dd27e173e2/go.mod h1:YkocrP2K2tcw938x9gCOmT5G5eCD6jsTz0SZuyAqwIE=
......
package tool
import (
"bytes"
"encoding/binary"
msgpack "github.com/vmihailenco/msgpack/v5"
)
// EncodeKey returns key in bytes.
func EncodeKey(key interface{}) (res []byte, err error) {
switch key.(type) {
case []byte:
return key.([]byte), nil
case bool, float32, float64, complex64, complex128, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
buf := new(bytes.Buffer)
err = binary.Write(buf, binary.BigEndian, key)
return buf.Bytes(), err
case int:
val := uint64(key.(int))
p := make([]byte, 8)
p[0] = byte(val >> 56)
p[1] = byte(val >> 48)
p[2] = byte(val >> 40)
p[3] = byte(val >> 32)
p[4] = byte(val >> 24)
p[5] = byte(val >> 16)
p[6] = byte(val >> 8)
p[7] = byte(val)
return p, err
case string:
return []byte(key.(string)), nil
default:
res, err = msgpack.Marshal(key)
return
}
}
// EncodeValue returns value in bytes.
func EncodeValue(value interface{}) (res []byte, err error) {
switch value.(type) {
case []byte:
return value.([]byte), nil
case string:
return []byte(value.(string)), err
default:
res, err = msgpack.Marshal(value)
return
}
}
// DecodeValue decode value to dest.
func DecodeValue(value []byte, dest interface{}) (err error) {
switch dest.(type) {
case *[]byte:
*dest.(*[]byte) = value
case *string:
*dest.(*string) = string(value)
default:
err = msgpack.Unmarshal(value, dest)
return
}
return
}
package tool
import (
"testing"
"github.com/stretchr/testify/assert"
)
func BenchmarkEncodeKey(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
tt := &TestKey{
Id: i,
Name: "roseduan",
}
_, err := EncodeKey(tt)
if err != nil {
panic(err)
}
}
}
func TestEncodeKey(t *testing.T) {
t.Run("int", func(t *testing.T) {
a := 100
v, err := EncodeKey(a)
assert.Equal(t, err, nil)
assert.NotEqual(t, len(v), 0)
})
}
func TestEncodeValue(t *testing.T) {
t.Run("byte", func(t *testing.T) {
b := []byte("roseduan")
res, err := EncodeValue(b)
assert.Equal(t, err, nil)
t.Log(res)
var r []byte
err = DecodeValue(res, &r)
assert.Equal(t, err, nil)
t.Log("val = ", string(r))
})
t.Run("struct", func(t *testing.T) {
v := &TestKey{
Id: 9943,
Name: "roseduan",
}
res, err := EncodeValue(v)
assert.Equal(t, err, nil)
t.Log(res)
r := &TestKey{}
err = DecodeValue(res, r)
assert.Equal(t, err, nil)
t.Log(r)
})
}
type TestKey struct {
Id int
Name string
}
package tool
import (
"io"
"io/ioutil"
"os"
"path"
)
// Exist check if the directory or file exists.
func Exist2(path string) bool {
if _, err := os.Stat(path); os.IsNotExist(err) {
return false
}
return true
}
// CopyDir copy directory from src to dst.
func CopyDir(src string, dst string) error {
var (
err error
dir []os.FileInfo
srcInfo os.FileInfo
)
if srcInfo, err = os.Stat(src); err != nil {
return err
}
if err = os.MkdirAll(dst, srcInfo.Mode()); err != nil {
return err
}
if dir, err = ioutil.ReadDir(src); err != nil {
return err
}
for _, fd := range dir {
srcPath := path.Join(src, fd.Name())
dstPath := path.Join(dst, fd.Name())
if fd.IsDir() {
if err = CopyDir(srcPath, dstPath); err != nil {
return err
}
} else {
if err = CopyFile(srcPath, dstPath); err != nil {
return err
}
}
}
return nil
}
// CopyFile copy a single file.
func CopyFile(src, dst string) error {
var (
err error
srcFile *os.File
dstFie *os.File
srcInfo os.FileInfo
)
if srcFile, err = os.Open(src); err != nil {
return err
}
defer srcFile.Close()
if dstFie, err = os.Create(dst); err != nil {
return err
}
defer dstFie.Close()
if _, err = io.Copy(dstFie, srcFile); err != nil {
return err
}
if srcInfo, err = os.Stat(src); err != nil {
return err
}
return os.Chmod(dst, srcInfo.Mode())
}
......@@ -2,6 +2,7 @@ package tool
import (
"os"
strconv "strconv"
"github.com/gookit/goutil/fsutil"
"github.com/huandu/xstrings"
......@@ -20,6 +21,14 @@ var (
WordCount = xstrings.WordCount
)
func Float64ToStr(val float64) string {
return strconv.FormatFloat(val, 'f', -1, 64)
}
func StrToFloat64(val string) (float64, error) {
return strconv.ParseFloat(val, 64)
}
func Asset(err error) {
if err != nil {
panic(err)
......
package tool
import "testing"
func TestStrToFloat64(t *testing.T) {
val := 3434.4455664545
res := Float64ToStr(val)
t.Log(res)
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment