diff --git a/README.md b/README.md index f543740..940c97b 100644 --- a/README.md +++ b/README.md @@ -13,5 +13,5 @@ #### 安装 ```go -go get -v -u go.dtapp.net/gophp +go get -v -u go.dtapp.net/gophp@v1.0.3 ``` diff --git a/go.mod b/go.mod index bf7d6ee..af0d1b2 100644 --- a/go.mod +++ b/go.mod @@ -1,10 +1,24 @@ module go.dtapp.net/gophp -go 1.18 +go 1.21 -require go.dtapp.net/gostring v1.0.10 +require ( + go.dtapp.net/gojson v1.0.2 + go.dtapp.net/gostring v1.0.12 +) require ( - go.dtapp.net/gorandom v1.0.1 // indirect - go.dtapp.net/gotime v1.0.5 // indirect + github.com/bytedance/sonic v1.10.2 // indirect + github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect + github.com/chenzhuoyu/iasm v0.9.1 // indirect + github.com/goccy/go-json v0.10.2 // indirect + github.com/json-iterator/go v1.1.12 // indirect + github.com/klauspost/cpuid/v2 v2.2.6 // indirect + github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect + github.com/modern-go/reflect2 v1.0.2 // indirect + github.com/twitchyliquid64/golang-asm v0.15.1 // indirect + go.dtapp.net/gorandom v1.0.2 // indirect + go.dtapp.net/gotime v1.0.6 // indirect + golang.org/x/arch v0.6.0 // indirect + golang.org/x/sys v0.15.0 // indirect ) diff --git a/go.sum b/go.sum index 740fc62..73d7375 100644 --- a/go.sum +++ b/go.sum @@ -1,6 +1,61 @@ -go.dtapp.net/gorandom v1.0.1 h1:IWfMClh1ECPvyUjlqD7MwLq4mZdUusD1qAwAdsvEJBs= -go.dtapp.net/gorandom v1.0.1/go.mod h1:ZPdgalKpvFV/ATQqR0k4ns/F/IpITAZpx6WkWirr5Y8= -go.dtapp.net/gostring v1.0.10 h1:eG+1kQehdJUitj9Hfwy79SndMHYOB7ABpWkTs7mDGeQ= -go.dtapp.net/gostring v1.0.10/go.mod h1:L4kREy89a9AraMHB5tUjjl+5rxP1gpXkDouRKKuzT50= -go.dtapp.net/gotime v1.0.5 h1:12aNgB2ULpP6QgQHEUkLilZ4ASvhpFxMFQkBwn0par8= -go.dtapp.net/gotime v1.0.5/go.mod h1:Gq7eNLr2iMLP18UNWONRq4V3Uhf/ADp4bIrS+Tc6ktY= +github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM= +github.com/bytedance/sonic v1.10.0-rc/go.mod h1:ElCzW+ufi8qKqNW0FY314xriJhyJhuoJ3gFZdAHF7NM= +github.com/bytedance/sonic v1.10.2 h1:GQebETVBxYB7JGWJtLBi07OVzWwt+8dWA00gEVW2ZFE= +github.com/bytedance/sonic v1.10.2/go.mod h1:iZcSUejdk5aukTND/Eu/ivjQuEL0Cu9/rf50Hi0u/g4= +github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY= +github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk= +github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d h1:77cEq6EriyTZ0g/qfRdp61a3Uu/AWrgIq2s0ClJV1g0= +github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d/go.mod h1:8EPpVsBuRksnlj1mLy4AWzRNQYxauNi62uWcE3to6eA= +github.com/chenzhuoyu/iasm v0.9.0/go.mod h1:Xjy2NpN3h7aUqeqM+woSuuvxmIe6+DDsiNLIrkAmYog= +github.com/chenzhuoyu/iasm v0.9.1 h1:tUHQJXo3NhBqw6s33wkGn9SP3bvrWLdlVIJ3hQBL7P0= +github.com/chenzhuoyu/iasm v0.9.1/go.mod h1:Xjy2NpN3h7aUqeqM+woSuuvxmIe6+DDsiNLIrkAmYog= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +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/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= +github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= +github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= +github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= +github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= +github.com/klauspost/cpuid/v2 v2.2.6 h1:ndNyv040zDGIDh8thGkXYjnFtiN02M1PVVF+JE/48xc= +github.com/klauspost/cpuid/v2 v2.2.6/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws= +github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= +github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= +github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= +go.dtapp.net/gojson v1.0.2 h1:NjslBOhAK3XvJepkML7LXcJRPtSfp3rDXGK/29VlDBw= +go.dtapp.net/gojson v1.0.2/go.mod h1:U0Vd2iSLKqdyg6oungBJVHxuYswTbeQVjruQC3/JqQ4= +go.dtapp.net/gorandom v1.0.2 h1:08BdcBP/fQiRw2Ii0OXATSTtObwNydlDlqc/j/u5O8Q= +go.dtapp.net/gorandom v1.0.2/go.mod h1:ZPdgalKpvFV/ATQqR0k4ns/F/IpITAZpx6WkWirr5Y8= +go.dtapp.net/gostring v1.0.12 h1:r1gwy4sa3SZAPJLMXBeYu7zgZLThA9a06SwRnJ1dlnA= +go.dtapp.net/gostring v1.0.12/go.mod h1:QDO8ApzmwFYzlcpA7lQxSO5ofjQZDmbshhRxQWihzyM= +go.dtapp.net/gotime v1.0.6 h1:f8YowUxpZtJbYawe5s5PmvGxRj61ydlzxAPYr5Fcetg= +go.dtapp.net/gotime v1.0.6/go.mod h1:Gq7eNLr2iMLP18UNWONRq4V3Uhf/ADp4bIrS+Tc6ktY= +golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= +golang.org/x/arch v0.6.0 h1:S0JTfE48HbRj80+4tbvZDYsJ3tGv6BUU3XxyZ7CirAc= +golang.org/x/arch v0.6.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys= +golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc= +golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50= +rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4= diff --git a/gophp.go b/gophp.go index 5c41b63..c0f2892 100644 --- a/gophp.go +++ b/gophp.go @@ -2,6 +2,7 @@ package gophp import ( "go.dtapp.net/gophp/serialize" + "reflect" "strconv" "strings" "unicode" @@ -16,6 +17,7 @@ func Serialize(value any) ([]byte, error) { func Unserialize(data []byte) (any, error) { return serialize.UnMarshal(data) } + func BaseConvert(number string, frombase, tobase int) (string, error) { i, err := strconv.ParseInt(number, frombase, 0) if err != nil { @@ -98,12 +100,14 @@ func Strtr(haystack string, params ...interface{}) string { } return haystack } + func Rtrim(str string, characterMask ...string) string { if len(characterMask) == 0 { return strings.TrimRightFunc(str, unicode.IsSpace) } return strings.TrimRight(str, characterMask[0]) } + func StrPad(input string, padLength int, padString string) string { output := padString for padLength > len(output) { @@ -114,3 +118,32 @@ func StrPad(input string, padLength int, padString string) string { } return output[:padLength-len(input)] + input } + +// Empty checks if a variable is empty (nil, zero value, or empty string). +func Empty(value interface{}) bool { + if value == nil { + return true + } + + // Use reflection to check if the value is the zero value for its type. + val := reflect.ValueOf(value) + switch val.Kind() { + case reflect.Array, reflect.Chan, reflect.Map, reflect.Slice, reflect.String: + return val.Len() == 0 + case reflect.Bool: + return !val.Bool() + case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: + return val.Int() == 0 + case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: + return val.Uint() == 0 + case reflect.Float32, reflect.Float64: + return val.Float() == 0 + } + + // If the type is not recognized, consider it non-empty. + return false +} + +func Isset(value interface{}) bool { + return value != nil +} diff --git a/gophp_test.go b/gophp_test.go index 9813389..65392ce 100644 --- a/gophp_test.go +++ b/gophp_test.go @@ -1,8 +1,8 @@ package gophp import ( - "encoding/json" "fmt" + "go.dtapp.net/gojson" "testing" ) @@ -47,9 +47,9 @@ func TestPhp2(t *testing.T) { m := make(map[string]interface{}) // struct 转json - j, _ := json.Marshal(stu) + j, _ := gojson.Marshal(stu) // json 转map - json.Unmarshal(j, &m) + gojson.Unmarshal(j, &m) fmt.Printf("Serialize %s\n", j) @@ -61,10 +61,10 @@ func TestPhp2(t *testing.T) { func TestUnserialize2(t *testing.T) { unserialize, _ := Unserialize([]byte(`a:2:{i:0;a:4:{s:7:"user_id";s:5:"10118";s:6:"amount";s:5:"69.00";s:11:"create_time";s:19:"2021-01-04 16:29:03";s:11:"update_time";s:19:"2021-06-15 16:02:46";}i:1;a:4:{s:7:"user_id";s:5:"10088";s:6:"amount";s:5:"10.00";s:11:"create_time";s:19:"2021-01-04 15:46:10";s:11:"update_time";s:19:"2021-06-15 15:50:06";}}`)) fmt.Printf("%s\n", unserialize) - arr, _ := json.Marshal(unserialize) + arr, _ := gojson.Marshal(unserialize) fmt.Printf("arr:%s\n", arr) var stu []student - _ = json.Unmarshal(arr, &stu) + _ = gojson.Unmarshal(arr, &stu) fmt.Printf("stu:%v\n", stu) } @@ -76,3 +76,23 @@ func TestUnserialize3(t *testing.T) { return } } + +func TestEmpty(t *testing.T) { + var emptyString string + var nonEmptyString = "Hello, World" + var emptySlice []int + var nonEmptySlice = []int{1, 2, 3} + var emptyMap map[string]int + var nonEmptyMap = map[string]int{"a": 1} + var emptyInt int + var nonEmptyInt = 42 + + t.Log(Empty(emptyString)) // true + t.Log(Empty(nonEmptyString)) // false + t.Log(Empty(emptySlice)) // true + t.Log(Empty(nonEmptySlice)) // false + t.Log(Empty(emptyMap)) // true + t.Log(Empty(nonEmptyMap)) // false + t.Log(Empty(emptyInt)) // true + t.Log(Empty(nonEmptyInt)) // false +} diff --git a/vendor/go.dtapp.net/gostring/.drone.yml b/vendor/go.dtapp.net/gostring/.drone.yml deleted file mode 100644 index c56c479..0000000 --- a/vendor/go.dtapp.net/gostring/.drone.yml +++ /dev/null @@ -1,17 +0,0 @@ -kind: pipeline -type: docker -name: clone - -steps: - - name: Test - image: golang:1.18 - commands: - - go env -w GO111MODULE=on - - go env -w GOPROXY=https://goproxy.cn,direct - - go test -v ./... - - name: Benchmark - image: golang:1.18 - commands: - - go env -w GO111MODULE=on - - go env -w GOPROXY=https://goproxy.cn,direct - - go test -bench=. -benchmem \ No newline at end of file diff --git a/vendor/go.dtapp.net/gostring/.gitignore b/vendor/go.dtapp.net/gostring/.gitignore deleted file mode 100644 index 5c75b7d..0000000 --- a/vendor/go.dtapp.net/gostring/.gitignore +++ /dev/null @@ -1,9 +0,0 @@ -.env -.git -.svn -.idea -.vscode -*.log -gomod.sh -*_test.go -/vendor/ diff --git a/vendor/go.dtapp.net/gostring/LICENSE b/vendor/go.dtapp.net/gostring/LICENSE deleted file mode 100644 index ff84bbf..0000000 --- a/vendor/go.dtapp.net/gostring/LICENSE +++ /dev/null @@ -1,21 +0,0 @@ -MIT License - -Copyright (c) 2018 李光春 - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. diff --git a/vendor/go.dtapp.net/gostring/README.md b/vendor/go.dtapp.net/gostring/README.md deleted file mode 100644 index 4a11776..0000000 --- a/vendor/go.dtapp.net/gostring/README.md +++ /dev/null @@ -1,17 +0,0 @@ -

-Golang String -

- -📦 Golang String - -[comment]: <> (go) -[![godoc](https://pkg.go.dev/badge/go.dtapp.net/gostring?status.svg)](https://pkg.go.dev/go.dtapp.net/gostring) -[![goproxy.cn](https://goproxy.cn/stats/go.dtapp.net/gostring/badges/download-count.svg)](https://goproxy.cn/stats/go.dtapp.net/gostring) -[![goreportcard.com](https://goreportcard.com/badge/go.dtapp.net/gostring)](https://goreportcard.com/report/go.dtapp.net/gostring) -[![deps.dev](https://img.shields.io/badge/deps-go-red.svg)](https://deps.dev/go/go.dtapp.net%2Fgostring) - -#### 安装 - -```go -go get -v -u go.dtapp.net/gostring -``` diff --git a/vendor/modules.txt b/vendor/modules.txt deleted file mode 100644 index 9205f18..0000000 --- a/vendor/modules.txt +++ /dev/null @@ -1,9 +0,0 @@ -# go.dtapp.net/gorandom v1.0.1 -## explicit; go 1.18 -go.dtapp.net/gorandom -# go.dtapp.net/gostring v1.0.10 -## explicit; go 1.18 -go.dtapp.net/gostring -# go.dtapp.net/gotime v1.0.5 -## explicit; go 1.18 -go.dtapp.net/gotime diff --git a/version.go b/version.go index 770580d..41fe115 100644 --- a/version.go +++ b/version.go @@ -1,3 +1,3 @@ package gophp -const Version = "1.0.2" +const Version = "1.0.3"