diff --git a/byteutil/util.go b/byteutil/util.go new file mode 100644 index 0000000..01f2de6 --- /dev/null +++ b/byteutil/util.go @@ -0,0 +1,44 @@ +package byteutil + +import ( + "bytes" + "encoding/binary" +) + +// IntToBytes 将int转换为[]byte +func IntToBytes(n int) []byte { + data := int64(n) + byteBuf := bytes.NewBuffer([]byte{}) + _ = binary.Write(byteBuf, binary.BigEndian, data) + return byteBuf.Bytes() +} + +// BytesToInt 将[]byte转换为int +func BytesToInt(bys []byte) int { + byteBuf := bytes.NewBuffer(bys) + var data int64 + _ = binary.Read(byteBuf, binary.BigEndian, &data) + return int(data) +} + +// ByteToLower lowers a byte +func ByteToLower(b byte) byte { + if b <= '\u007F' { + if 'A' <= b && b <= 'Z' { + b += 'a' - 'A' + } + return b + } + return b +} + +// ByteToUpper upper a byte +func ByteToUpper(b byte) byte { + if b <= '\u007F' { + if 'a' <= b && b <= 'z' { + b -= 'a' - 'A' + } + return b + } + return b +} diff --git a/byteutil/util_test.go b/byteutil/util_test.go new file mode 100644 index 0000000..add4259 --- /dev/null +++ b/byteutil/util_test.go @@ -0,0 +1,11 @@ +package byteutil + +import ( + "fmt" + "testing" +) + +func TestIntToBytes(t *testing.T) { + fmt.Println(IntToBytes(1)) + fmt.Println(BytesToInt(IntToBytes(1))) +} diff --git a/entgo/go.mod b/entgo/go.mod index ceb4b09..957a87f 100644 --- a/entgo/go.mod +++ b/entgo/go.mod @@ -6,7 +6,7 @@ require ( entgo.io/contrib v0.4.5 entgo.io/ent v0.12.4 github.com/go-kratos/kratos/v2 v2.7.1 - github.com/google/uuid v1.3.1 + github.com/google/uuid v1.4.0 github.com/stretchr/testify v1.8.4 github.com/tx7do/go-utils v1.1.0 ) diff --git a/entgo/go.sum b/entgo/go.sum index 9f57237..5ae9ccd 100644 --- a/entgo/go.sum +++ b/entgo/go.sum @@ -29,6 +29,7 @@ github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4= github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/google/uuid v1.4.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/hashicorp/hcl/v2 v2.19.1 h1://i05Jqznmb2EXqa39Nsvyan2o5XyMowW5fnCKW5RPI= github.com/hashicorp/hcl/v2 v2.19.1/go.mod h1:ThLC89FV4p9MPW804KVbe/cEXoQ8NZEh+JtMeeGErHE= github.com/jhump/protoreflect v1.15.3 h1:6SFRuqU45u9hIZPJAoZ8c28T3nK64BNdp9w6jFonzls= diff --git a/go.mod b/go.mod index 2e1858b..d5769a1 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/tx7do/go-utils go 1.20 require ( + github.com/google/uuid v1.4.0 github.com/sony/sonyflake v1.2.0 github.com/stretchr/testify v1.8.4 golang.org/x/crypto v0.14.0 diff --git a/go.sum b/go.sum index 6979d10..7df99e1 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,8 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/google/uuid v1.4.0 h1:MtMxsa51/r9yyhkyLsVeVt0B+BGQZzpQiTQ4eHZ8bc4= +github.com/google/uuid v1.4.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= diff --git a/uuid/test_trans.go b/uuid/test_trans.go new file mode 100644 index 0000000..3e2ae66 --- /dev/null +++ b/uuid/test_trans.go @@ -0,0 +1,7 @@ +package uuid + +import "testing" + +func TestUUID(t *testing.T) { + +} diff --git a/uuid/trans.go b/uuid/trans.go new file mode 100644 index 0000000..3cd0cf6 --- /dev/null +++ b/uuid/trans.go @@ -0,0 +1,31 @@ +package uuid + +import ( + "github.com/google/uuid" + "github.com/tx7do/go-utils/trans" +) + +func ToUuidPtr(str *string) *uuid.UUID { + var id *uuid.UUID + if str != nil { + _id, err := uuid.Parse(*str) + if err != nil { + return nil + } + id = &_id + } + return id +} + +func ToUuid(str string) uuid.UUID { + id, _ := uuid.Parse(str) + return id +} + +func ToStringPtr(id *uuid.UUID) *string { + var strUUID *string + if id != nil { + strUUID = trans.String(id.String()) + } + return strUUID +}