mirror of
https://github.com/gogf/gf.git
synced 2025-04-05 11:18:50 +08:00
improva container for json marshal/unmarshal interface
This commit is contained in:
parent
195cae6577
commit
6384e75ed9
22
.example/container/garray/json_marshal.go
Normal file
22
.example/container/garray/json_marshal.go
Normal file
@ -0,0 +1,22 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/container/garray"
|
||||
)
|
||||
|
||||
func main() {
|
||||
type Student struct {
|
||||
Id int
|
||||
Name string
|
||||
Scores *garray.IntArray
|
||||
}
|
||||
s := Student{
|
||||
Id: 1,
|
||||
Name: "john",
|
||||
Scores: garray.NewIntArrayFrom([]int{100, 99, 98}),
|
||||
}
|
||||
b, _ := json.Marshal(s)
|
||||
fmt.Println(string(b))
|
||||
}
|
19
.example/container/garray/json_unmarshal.go
Normal file
19
.example/container/garray/json_unmarshal.go
Normal file
@ -0,0 +1,19 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/container/garray"
|
||||
)
|
||||
|
||||
func main() {
|
||||
b := []byte(`{"Id":1,"Name":"john","Scores":[100,99,98]}`)
|
||||
type Student struct {
|
||||
Id int
|
||||
Name string
|
||||
Scores *garray.IntArray
|
||||
}
|
||||
s := Student{}
|
||||
json.Unmarshal(b, &s)
|
||||
fmt.Println(s)
|
||||
}
|
@ -1,17 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/gogf/gf/container/gmap"
|
||||
)
|
||||
|
||||
func main() {
|
||||
m := gmap.NewIntIntMap()
|
||||
m.Set(1, 2)
|
||||
m.Set(3, 4)
|
||||
b, err := json.Marshal(m)
|
||||
fmt.Println(err)
|
||||
fmt.Println(string(b))
|
||||
}
|
19
.example/container/gmap/gmap_json_marshal.go
Normal file
19
.example/container/gmap/gmap_json_marshal.go
Normal file
@ -0,0 +1,19 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
|
||||
"github.com/gogf/gf/container/gmap"
|
||||
)
|
||||
|
||||
func main() {
|
||||
m := gmap.New()
|
||||
m.Sets(g.MapAnyAny{
|
||||
"name": "john",
|
||||
"score": 100,
|
||||
})
|
||||
b, _ := json.Marshal(m)
|
||||
fmt.Println(string(b))
|
||||
}
|
14
.example/container/gmap/gmap_json_unmarshal.go
Normal file
14
.example/container/gmap/gmap_json_unmarshal.go
Normal file
@ -0,0 +1,14 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/container/gmap"
|
||||
)
|
||||
|
||||
func main() {
|
||||
m := gmap.Map{}
|
||||
s := []byte(`{"name":"john","score":100}`)
|
||||
json.Unmarshal(s, &m)
|
||||
fmt.Println(m.Map())
|
||||
}
|
22
.example/container/gset/json_marshal.go
Normal file
22
.example/container/gset/json_marshal.go
Normal file
@ -0,0 +1,22 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/container/gset"
|
||||
)
|
||||
|
||||
func main() {
|
||||
type Student struct {
|
||||
Id int
|
||||
Name string
|
||||
Scores *gset.IntSet
|
||||
}
|
||||
s := Student{
|
||||
Id: 1,
|
||||
Name: "john",
|
||||
Scores: gset.NewIntSetFrom([]int{100, 99, 98}),
|
||||
}
|
||||
b, _ := json.Marshal(s)
|
||||
fmt.Println(string(b))
|
||||
}
|
19
.example/container/gset/json_unmarshal.go
Normal file
19
.example/container/gset/json_unmarshal.go
Normal file
@ -0,0 +1,19 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/container/gset"
|
||||
)
|
||||
|
||||
func main() {
|
||||
b := []byte(`{"Id":1,"Name":"john","Scores":[100,99,98]}`)
|
||||
type Student struct {
|
||||
Id int
|
||||
Name string
|
||||
Scores *gset.IntSet
|
||||
}
|
||||
s := Student{}
|
||||
json.Unmarshal(b, &s)
|
||||
fmt.Println(s)
|
||||
}
|
22
.example/container/gtype/json_marshal.go
Normal file
22
.example/container/gtype/json_marshal.go
Normal file
@ -0,0 +1,22 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/container/gtype"
|
||||
)
|
||||
|
||||
func main() {
|
||||
type Student struct {
|
||||
Id *gtype.Int
|
||||
Name *gtype.String
|
||||
Scores *gtype.Interface
|
||||
}
|
||||
s := Student{
|
||||
Id: gtype.NewInt(1),
|
||||
Name: gtype.NewString("john"),
|
||||
Scores: gtype.NewInterface([]int{100, 99, 98}),
|
||||
}
|
||||
b, _ := json.Marshal(s)
|
||||
fmt.Println(string(b))
|
||||
}
|
19
.example/container/gtype/json_unmarshal.go
Normal file
19
.example/container/gtype/json_unmarshal.go
Normal file
@ -0,0 +1,19 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/container/gtype"
|
||||
)
|
||||
|
||||
func main() {
|
||||
b := []byte(`{"Id":1,"Name":"john","Scores":[100,99,98]}`)
|
||||
type Student struct {
|
||||
Id *gtype.Int
|
||||
Name *gtype.String
|
||||
Scores *gtype.Interface
|
||||
}
|
||||
s := Student{}
|
||||
json.Unmarshal(b, &s)
|
||||
fmt.Println(s)
|
||||
}
|
22
.example/container/gvar/json_marshal.go
Normal file
22
.example/container/gvar/json_marshal.go
Normal file
@ -0,0 +1,22 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
)
|
||||
|
||||
func main() {
|
||||
type Student struct {
|
||||
Id *g.Var
|
||||
Name *g.Var
|
||||
Scores *g.Var
|
||||
}
|
||||
s := Student{
|
||||
Id: g.NewVar(1),
|
||||
Name: g.NewVar("john"),
|
||||
Scores: g.NewVar([]int{100, 99, 98}),
|
||||
}
|
||||
b, _ := json.Marshal(s)
|
||||
fmt.Println(string(b))
|
||||
}
|
19
.example/container/gvar/json_unmarshal.go
Normal file
19
.example/container/gvar/json_unmarshal.go
Normal file
@ -0,0 +1,19 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/frame/g"
|
||||
)
|
||||
|
||||
func main() {
|
||||
b := []byte(`{"Id":1,"Name":"john","Scores":[100,99,98]}`)
|
||||
type Student struct {
|
||||
Id *g.Var
|
||||
Name *g.Var
|
||||
Scores *g.Var
|
||||
}
|
||||
s := Student{}
|
||||
json.Unmarshal(b, &s)
|
||||
fmt.Println(s)
|
||||
}
|
@ -1,11 +1,12 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
)
|
||||
|
||||
func main() {
|
||||
s := "3.4028235e+38"
|
||||
fmt.Println(gconv.String(gconv.Float64(s)))
|
||||
b, _ := json.Marshal([]interface{}{1, 2, 3, 4, 5, 123.456, "a"})
|
||||
fmt.Println(gconv.String(b))
|
||||
}
|
||||
|
@ -9,6 +9,7 @@ package garray
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"github.com/gogf/gf/text/gstr"
|
||||
"math"
|
||||
"sort"
|
||||
|
||||
@ -583,8 +584,14 @@ func (a *Array) Join(glue string) string {
|
||||
a.mu.RLock()
|
||||
defer a.mu.RUnlock()
|
||||
buffer := bytes.NewBuffer(nil)
|
||||
s := ""
|
||||
for k, v := range a.array {
|
||||
buffer.WriteString(gconv.String(v))
|
||||
s = gconv.String(v)
|
||||
if gstr.IsNumeric(s) {
|
||||
buffer.WriteString(s)
|
||||
} else {
|
||||
buffer.WriteString(`"` + gstr.QuoteMeta(s, `"\`) + `"`)
|
||||
}
|
||||
if k != len(a.array)-1 {
|
||||
buffer.WriteString(glue)
|
||||
}
|
||||
@ -605,10 +612,7 @@ func (a *Array) CountValues() map[interface{}]int {
|
||||
|
||||
// String returns current array as a string.
|
||||
func (a *Array) String() string {
|
||||
a.mu.RLock()
|
||||
defer a.mu.RUnlock()
|
||||
jsonContent, _ := json.Marshal(a.array)
|
||||
return string(jsonContent)
|
||||
return "[" + a.Join(",") + "]"
|
||||
}
|
||||
|
||||
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
|
||||
|
@ -612,10 +612,7 @@ func (a *IntArray) CountValues() map[int]int {
|
||||
|
||||
// String returns current array as a string.
|
||||
func (a *IntArray) String() string {
|
||||
a.mu.RLock()
|
||||
defer a.mu.RUnlock()
|
||||
jsonContent, _ := json.Marshal(a.array)
|
||||
return string(jsonContent)
|
||||
return "[" + a.Join(",") + "]"
|
||||
}
|
||||
|
||||
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
|
||||
|
@ -9,6 +9,7 @@ package garray
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"github.com/gogf/gf/text/gstr"
|
||||
"math"
|
||||
"sort"
|
||||
"strings"
|
||||
@ -591,7 +592,7 @@ func (a *StrArray) Join(glue string) string {
|
||||
defer a.mu.RUnlock()
|
||||
buffer := bytes.NewBuffer(nil)
|
||||
for k, v := range a.array {
|
||||
buffer.WriteString(gconv.String(v))
|
||||
buffer.WriteString(`"` + gstr.QuoteMeta(v, `"\`) + `"`)
|
||||
if k != len(a.array)-1 {
|
||||
buffer.WriteString(glue)
|
||||
}
|
||||
@ -612,10 +613,7 @@ func (a *StrArray) CountValues() map[string]int {
|
||||
|
||||
// String returns current array as a string.
|
||||
func (a *StrArray) String() string {
|
||||
a.mu.RLock()
|
||||
defer a.mu.RUnlock()
|
||||
jsonContent, _ := json.Marshal(a.array)
|
||||
return string(jsonContent)
|
||||
return "[" + a.Join(",") + "]"
|
||||
}
|
||||
|
||||
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
|
||||
|
@ -9,6 +9,7 @@ package garray
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"github.com/gogf/gf/text/gstr"
|
||||
"github.com/gogf/gf/util/gutil"
|
||||
"math"
|
||||
"sort"
|
||||
@ -528,8 +529,14 @@ func (a *SortedArray) Join(glue string) string {
|
||||
a.mu.RLock()
|
||||
defer a.mu.RUnlock()
|
||||
buffer := bytes.NewBuffer(nil)
|
||||
s := ""
|
||||
for k, v := range a.array {
|
||||
buffer.WriteString(gconv.String(v))
|
||||
s = gconv.String(v)
|
||||
if gstr.IsNumeric(s) {
|
||||
buffer.WriteString(s)
|
||||
} else {
|
||||
buffer.WriteString(`"` + gstr.QuoteMeta(s, `"\`) + `"`)
|
||||
}
|
||||
if k != len(a.array)-1 {
|
||||
buffer.WriteString(glue)
|
||||
}
|
||||
@ -550,10 +557,7 @@ func (a *SortedArray) CountValues() map[interface{}]int {
|
||||
|
||||
// String returns current array as a string.
|
||||
func (a *SortedArray) String() string {
|
||||
a.mu.RLock()
|
||||
defer a.mu.RUnlock()
|
||||
jsonContent, _ := json.Marshal(a.array)
|
||||
return gconv.UnsafeBytesToStr(jsonContent)
|
||||
return "[" + a.Join(",") + "]"
|
||||
}
|
||||
|
||||
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
|
||||
|
@ -537,10 +537,7 @@ func (a *SortedIntArray) CountValues() map[int]int {
|
||||
|
||||
// String returns current array as a string.
|
||||
func (a *SortedIntArray) String() string {
|
||||
a.mu.RLock()
|
||||
defer a.mu.RUnlock()
|
||||
jsonContent, _ := json.Marshal(a.array)
|
||||
return string(jsonContent)
|
||||
return "[" + a.Join(",") + "]"
|
||||
}
|
||||
|
||||
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
|
||||
|
@ -9,6 +9,7 @@ package garray
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"github.com/gogf/gf/text/gstr"
|
||||
"math"
|
||||
"sort"
|
||||
|
||||
@ -516,7 +517,7 @@ func (a *SortedStrArray) Join(glue string) string {
|
||||
defer a.mu.RUnlock()
|
||||
buffer := bytes.NewBuffer(nil)
|
||||
for k, v := range a.array {
|
||||
buffer.WriteString(gconv.String(v))
|
||||
buffer.WriteString(`"` + gstr.QuoteMeta(v, `"\`) + `"`)
|
||||
if k != len(a.array)-1 {
|
||||
buffer.WriteString(glue)
|
||||
}
|
||||
@ -537,10 +538,7 @@ func (a *SortedStrArray) CountValues() map[string]int {
|
||||
|
||||
// String returns current array as a string.
|
||||
func (a *SortedStrArray) String() string {
|
||||
a.mu.RLock()
|
||||
defer a.mu.RUnlock()
|
||||
jsonContent, _ := json.Marshal(a.array)
|
||||
return string(jsonContent)
|
||||
return "[" + a.Join(",") + "]"
|
||||
}
|
||||
|
||||
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
|
||||
|
@ -255,7 +255,21 @@ func TestArray_Join(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
a1 := []interface{}{0, 1, 2, 3, 4, 5, 6}
|
||||
array1 := garray.NewArrayFrom(a1)
|
||||
gtest.Assert(array1.Join("."), "0.1.2.3.4.5.6")
|
||||
gtest.Assert(array1.Join("."), `0.1.2.3.4.5.6`)
|
||||
})
|
||||
|
||||
gtest.Case(t, func() {
|
||||
a1 := []interface{}{0, 1, `"a"`, `\a`}
|
||||
array1 := garray.NewArrayFrom(a1)
|
||||
gtest.Assert(array1.Join("."), `0.1."\"a\""."\\a"`)
|
||||
})
|
||||
}
|
||||
|
||||
func TestArray_String(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
a1 := []interface{}{0, 1, 2, 3, 4, 5, 6}
|
||||
array1 := garray.NewArrayFrom(a1)
|
||||
gtest.Assert(array1.String(), `[0,1,2,3,4,5,6]`)
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -262,6 +262,14 @@ func TestIntArray_Join(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestIntArray_String(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
a1 := []int{0, 1, 2, 3, 4, 5, 6}
|
||||
array1 := garray.NewIntArrayFrom(a1)
|
||||
gtest.Assert(array1.String(), "[0,1,2,3,4,5,6]")
|
||||
})
|
||||
}
|
||||
|
||||
func TestIntArray_SetArray(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
a1 := []int{1, 2, 3, 5}
|
||||
|
@ -248,7 +248,20 @@ func TestStrArray_Join(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
a1 := []string{"0", "1", "2", "3", "4", "5", "6"}
|
||||
array1 := garray.NewStrArrayFrom(a1)
|
||||
gtest.Assert(array1.Join("."), "0.1.2.3.4.5.6")
|
||||
gtest.Assert(array1.Join("."), `"0"."1"."2"."3"."4"."5"."6"`)
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
a1 := []string{"0", "1", `"a"`, `\a`}
|
||||
array1 := garray.NewStrArrayFrom(a1)
|
||||
gtest.Assert(array1.Join("."), `"0"."1"."\"a\""."\\a"`)
|
||||
})
|
||||
}
|
||||
|
||||
func TestStrArray_String(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
a1 := []string{"0", "1", "2", "3", "4", "5", "6"}
|
||||
array1 := garray.NewStrArrayFrom(a1)
|
||||
gtest.Assert(array1.String(), `["0","1","2","3","4","5","6"]`)
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -390,14 +390,26 @@ func TestSortedArray_Rands(t *testing.T) {
|
||||
func TestSortedArray_Join(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
a1 := []interface{}{"a", "d", "c"}
|
||||
|
||||
func1 := func(v1, v2 interface{}) int {
|
||||
return strings.Compare(gconv.String(v1), gconv.String(v2))
|
||||
}
|
||||
array1 := garray.NewSortedArrayFrom(a1, func1)
|
||||
gtest.Assert(array1.Join(","), "a,c,d")
|
||||
gtest.Assert(array1.Join("."), "a.c.d")
|
||||
gtest.Assert(array1.Join(","), `"a","c","d"`)
|
||||
gtest.Assert(array1.Join("."), `"a"."c"."d"`)
|
||||
})
|
||||
|
||||
gtest.Case(t, func() {
|
||||
a1 := []interface{}{0, 1, `"a"`, `\a`}
|
||||
array1 := garray.NewSortedArrayFrom(a1, gutil.ComparatorString)
|
||||
gtest.Assert(array1.Join("."), `"\"a\"".0.1."\\a"`)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedArray_String(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
a1 := []interface{}{0, 1, "a", "b"}
|
||||
array1 := garray.NewSortedArrayFrom(a1, gutil.ComparatorString)
|
||||
gtest.Assert(array1.String(), `[0,1,"a","b"]`)
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -209,6 +209,22 @@ func TestSortedIntArray_Sum(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedIntArray_Join(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
a1 := []int{1, 3, 5}
|
||||
array1 := garray.NewSortedIntArrayFrom(a1)
|
||||
gtest.Assert(array1.Join("."), `1.3.5`)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedIntArray_String(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
a1 := []int{1, 3, 5}
|
||||
array1 := garray.NewSortedIntArrayFrom(a1)
|
||||
gtest.Assert(array1.String(), `[1,3,5]`)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedIntArray_Contains(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
a1 := []int{1, 3, 5}
|
||||
|
@ -290,8 +290,22 @@ func TestSortedStrArray_Join(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
a1 := []string{"e", "a", "d"}
|
||||
array1 := garray.NewSortedStrArrayFrom(a1)
|
||||
gtest.Assert(array1.Join(","), "a,d,e")
|
||||
gtest.Assert(array1.Join("."), "a.d.e")
|
||||
gtest.Assert(array1.Join(","), `"a","d","e"`)
|
||||
gtest.Assert(array1.Join("."), `"a"."d"."e"`)
|
||||
})
|
||||
|
||||
gtest.Case(t, func() {
|
||||
a1 := []string{"a", `"b"`, `\c`}
|
||||
array1 := garray.NewSortedStrArrayFrom(a1)
|
||||
gtest.Assert(array1.Join("."), `"\"b\""."\\c"."a"`)
|
||||
})
|
||||
}
|
||||
|
||||
func TestSortedStrArray_String(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
a1 := []string{"e", "a", "d"}
|
||||
array1 := garray.NewSortedStrArrayFrom(a1)
|
||||
gtest.Assert(array1.String(), `["a","d","e"]`)
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -8,10 +8,10 @@
|
||||
package gset
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"strings"
|
||||
|
||||
"github.com/gogf/gf/internal/rwmutex"
|
||||
"github.com/gogf/gf/text/gstr"
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
)
|
||||
|
||||
@ -118,12 +118,30 @@ func (set *Set) Slice() []interface{} {
|
||||
|
||||
// Join joins items with a string <glue>.
|
||||
func (set *Set) Join(glue string) string {
|
||||
return strings.Join(gconv.Strings(set.Slice()), ",")
|
||||
set.mu.RLock()
|
||||
defer set.mu.RUnlock()
|
||||
buffer := bytes.NewBuffer(nil)
|
||||
s := ""
|
||||
l := len(set.data)
|
||||
i := 0
|
||||
for k, _ := range set.data {
|
||||
s = gconv.String(k)
|
||||
if gstr.IsNumeric(s) {
|
||||
buffer.WriteString(s)
|
||||
} else {
|
||||
buffer.WriteString(`"` + gstr.QuoteMeta(s, `"\`) + `"`)
|
||||
}
|
||||
if i != l-1 {
|
||||
buffer.WriteString(glue)
|
||||
}
|
||||
i++
|
||||
}
|
||||
return buffer.String()
|
||||
}
|
||||
|
||||
// String returns items as a string, which are joined by char ','.
|
||||
func (set *Set) String() string {
|
||||
return set.Join(",")
|
||||
return "[" + set.Join(",") + "]"
|
||||
}
|
||||
|
||||
// LockFunc locks writing with callback function <f>.
|
||||
|
@ -8,9 +8,8 @@
|
||||
package gset
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"strings"
|
||||
|
||||
"github.com/gogf/gf/internal/rwmutex"
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
)
|
||||
@ -112,12 +111,24 @@ func (set *IntSet) Slice() []int {
|
||||
|
||||
// Join joins items with a string <glue>.
|
||||
func (set *IntSet) Join(glue string) string {
|
||||
return strings.Join(gconv.Strings(set.Slice()), ",")
|
||||
set.mu.RLock()
|
||||
defer set.mu.RUnlock()
|
||||
buffer := bytes.NewBuffer(nil)
|
||||
l := len(set.data)
|
||||
i := 0
|
||||
for k, _ := range set.data {
|
||||
buffer.WriteString(gconv.String(k))
|
||||
if i != l-1 {
|
||||
buffer.WriteString(glue)
|
||||
}
|
||||
i++
|
||||
}
|
||||
return buffer.String()
|
||||
}
|
||||
|
||||
// String returns items as a string, which are joined by char ','.
|
||||
func (set *IntSet) String() string {
|
||||
return set.Join(",")
|
||||
return "[" + set.Join(",") + "]"
|
||||
}
|
||||
|
||||
// LockFunc locks writing with callback function <f>.
|
||||
|
@ -8,10 +8,10 @@
|
||||
package gset
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"strings"
|
||||
|
||||
"github.com/gogf/gf/internal/rwmutex"
|
||||
"github.com/gogf/gf/text/gstr"
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
)
|
||||
|
||||
@ -113,12 +113,28 @@ func (set *StrSet) Slice() []string {
|
||||
|
||||
// Join joins items with a string <glue>.
|
||||
func (set *StrSet) Join(glue string) string {
|
||||
return strings.Join(set.Slice(), ",")
|
||||
set.mu.RLock()
|
||||
defer set.mu.RUnlock()
|
||||
buffer := bytes.NewBuffer(nil)
|
||||
l := len(set.data)
|
||||
i := 0
|
||||
for k, _ := range set.data {
|
||||
if gstr.IsNumeric(k) {
|
||||
buffer.WriteString(k)
|
||||
} else {
|
||||
buffer.WriteString(`"` + gstr.QuoteMeta(k, `"\`) + `"`)
|
||||
}
|
||||
if i != l-1 {
|
||||
buffer.WriteString(glue)
|
||||
}
|
||||
i++
|
||||
}
|
||||
return buffer.String()
|
||||
}
|
||||
|
||||
// String returns items as a string, which are joined by char ','.
|
||||
func (set *StrSet) String() string {
|
||||
return set.Join(",")
|
||||
return "[" + set.Join(",") + "]"
|
||||
}
|
||||
|
||||
// LockFunc locks writing with callback function <f>.
|
||||
|
@ -190,8 +190,22 @@ func TestIntSet_Join(t *testing.T) {
|
||||
s1 := gset.NewIntSet()
|
||||
s1.Add(1).Add(2).Add(3)
|
||||
s3 := s1.Join(",")
|
||||
gtest.Assert(strings.Contains(s3, "1"), true)
|
||||
gtest.Assert(strings.Contains(s3, "2"), true)
|
||||
gtest.Assert(strings.Contains(s3, "3"), true)
|
||||
})
|
||||
}
|
||||
|
||||
func TestIntSet_String(t *testing.T) {
|
||||
gtest.Case(t, func() {
|
||||
s1 := gset.NewIntSet()
|
||||
s1.Add(1).Add(2).Add(3)
|
||||
s3 := s1.String()
|
||||
gtest.Assert(strings.Contains(s3, "["), true)
|
||||
gtest.Assert(strings.Contains(s3, "]"), true)
|
||||
gtest.Assert(strings.Contains(s3, "1"), true)
|
||||
gtest.Assert(strings.Contains(s3, "2"), true)
|
||||
gtest.Assert(strings.Contains(s3, "3"), true)
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -196,10 +196,21 @@ func TestNewStrSetFrom(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestStrSet_Join(t *testing.T) {
|
||||
s1 := gset.NewStrSetFrom([]string{"a", "b", "c"}, true)
|
||||
str1 := s1.Join(",")
|
||||
gtest.Assert(strings.Contains(str1, "b"), true)
|
||||
gtest.Assert(strings.Contains(str1, "d"), false)
|
||||
gtest.Case(t, func() {
|
||||
s1 := gset.NewStrSetFrom([]string{"a", "b", "c"}, true)
|
||||
str1 := s1.Join(",")
|
||||
gtest.Assert(strings.Contains(str1, "b"), true)
|
||||
gtest.Assert(strings.Contains(str1, "d"), false)
|
||||
})
|
||||
|
||||
gtest.Case(t, func() {
|
||||
s1 := gset.NewStrSet()
|
||||
s1.Add("a").Add(`"b"`).Add(`\c`)
|
||||
str1 := s1.Join(",")
|
||||
gtest.Assert(strings.Contains(str1, `\"b\"`), true)
|
||||
gtest.Assert(strings.Contains(str1, `\\c`), true)
|
||||
gtest.Assert(strings.Contains(str1, `a`), true)
|
||||
})
|
||||
}
|
||||
|
||||
func TestStrSet_String(t *testing.T) {
|
||||
@ -210,6 +221,14 @@ func TestStrSet_String(t *testing.T) {
|
||||
gtest.Assert(strings.Contains(str1, "d"), false)
|
||||
})
|
||||
|
||||
gtest.Case(t, func() {
|
||||
s1 := gset.New(true)
|
||||
s1.Add("a").Add("a2").Add("b").Add("c")
|
||||
str1 := s1.String()
|
||||
gtest.Assert(strings.Contains(str1, "["), true)
|
||||
gtest.Assert(strings.Contains(str1, "]"), true)
|
||||
gtest.Assert(strings.Contains(str1, "a2"), true)
|
||||
})
|
||||
}
|
||||
|
||||
func TestStrSet_Sum(t *testing.T) {
|
||||
|
@ -215,7 +215,14 @@ func TestSet_Join(t *testing.T) {
|
||||
s1.Add("a").Add("a1").Add("b").Add("c")
|
||||
str1 := s1.Join(",")
|
||||
gtest.Assert(strings.Contains(str1, "a1"), true)
|
||||
|
||||
})
|
||||
gtest.Case(t, func() {
|
||||
s1 := gset.New(true)
|
||||
s1.Add("a").Add(`"b"`).Add(`\c`)
|
||||
str1 := s1.Join(",")
|
||||
gtest.Assert(strings.Contains(str1, `\"b\"`), true)
|
||||
gtest.Assert(strings.Contains(str1, `\\c`), true)
|
||||
gtest.Assert(strings.Contains(str1, `a`), true)
|
||||
})
|
||||
}
|
||||
|
||||
@ -224,8 +231,9 @@ func TestSet_String(t *testing.T) {
|
||||
s1 := gset.New(true)
|
||||
s1.Add("a").Add("a2").Add("b").Add("c")
|
||||
str1 := s1.String()
|
||||
gtest.Assert(strings.Contains(str1, "["), true)
|
||||
gtest.Assert(strings.Contains(str1, "]"), true)
|
||||
gtest.Assert(strings.Contains(str1, "a2"), true)
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -67,6 +67,14 @@ func (v *Bool) Cas(old, new bool) bool {
|
||||
return atomic.CompareAndSwapInt32(&v.value, oldInt32, newInt32)
|
||||
}
|
||||
|
||||
// String implements String interface for string printing.
|
||||
func (v *Bool) String() string {
|
||||
if v.Val() {
|
||||
return "true"
|
||||
}
|
||||
return "false"
|
||||
}
|
||||
|
||||
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
|
||||
func (v *Bool) MarshalJSON() ([]byte, error) {
|
||||
if v.Val() {
|
||||
|
@ -52,6 +52,11 @@ func (v *Byte) Cas(old, new byte) bool {
|
||||
return atomic.CompareAndSwapInt32(&v.value, int32(old), int32(new))
|
||||
}
|
||||
|
||||
// String implements String interface for string printing.
|
||||
func (v *Byte) String() string {
|
||||
return strconv.FormatUint(uint64(v.Val()), 10)
|
||||
}
|
||||
|
||||
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
|
||||
func (v *Byte) MarshalJSON() ([]byte, error) {
|
||||
return gconv.UnsafeStrToBytes(strconv.FormatUint(uint64(v.Val()), 10)), nil
|
||||
|
@ -48,6 +48,11 @@ func (v *Bytes) Val() []byte {
|
||||
return nil
|
||||
}
|
||||
|
||||
// String implements String interface for string printing.
|
||||
func (v *Bytes) String() string {
|
||||
return string(v.Val())
|
||||
}
|
||||
|
||||
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
|
||||
func (v *Bytes) MarshalJSON() ([]byte, error) {
|
||||
val := v.Val()
|
||||
|
@ -65,6 +65,11 @@ func (v *Float32) Cas(old, new float32) bool {
|
||||
return atomic.CompareAndSwapUint32(&v.value, math.Float32bits(old), math.Float32bits(new))
|
||||
}
|
||||
|
||||
// String implements String interface for string printing.
|
||||
func (v *Float32) String() string {
|
||||
return strconv.FormatFloat(float64(v.Val()), 'g', -1, 32)
|
||||
}
|
||||
|
||||
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
|
||||
func (v *Float32) MarshalJSON() ([]byte, error) {
|
||||
return gconv.UnsafeStrToBytes(strconv.FormatFloat(float64(v.Val()), 'g', -1, 32)), nil
|
||||
|
@ -65,6 +65,11 @@ func (v *Float64) Cas(old, new float64) bool {
|
||||
return atomic.CompareAndSwapUint64(&v.value, math.Float64bits(old), math.Float64bits(new))
|
||||
}
|
||||
|
||||
// String implements String interface for string printing.
|
||||
func (v *Float64) String() string {
|
||||
return strconv.FormatFloat(v.Val(), 'g', -1, 64)
|
||||
}
|
||||
|
||||
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
|
||||
func (v *Float64) MarshalJSON() ([]byte, error) {
|
||||
return gconv.UnsafeStrToBytes(strconv.FormatFloat(v.Val(), 'g', -1, 64)), nil
|
||||
|
@ -52,6 +52,11 @@ func (v *Int) Cas(old, new int) bool {
|
||||
return atomic.CompareAndSwapInt64(&v.value, int64(old), int64(new))
|
||||
}
|
||||
|
||||
// String implements String interface for string printing.
|
||||
func (v *Int) String() string {
|
||||
return strconv.Itoa(v.Val())
|
||||
}
|
||||
|
||||
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
|
||||
func (v *Int) MarshalJSON() ([]byte, error) {
|
||||
return gconv.UnsafeStrToBytes(strconv.Itoa(v.Val())), nil
|
||||
|
@ -52,6 +52,11 @@ func (v *Int32) Cas(old, new int32) bool {
|
||||
return atomic.CompareAndSwapInt32(&v.value, old, new)
|
||||
}
|
||||
|
||||
// String implements String interface for string printing.
|
||||
func (v *Int32) String() string {
|
||||
return strconv.Itoa(int(v.Val()))
|
||||
}
|
||||
|
||||
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
|
||||
func (v *Int32) MarshalJSON() ([]byte, error) {
|
||||
return gconv.UnsafeStrToBytes(strconv.Itoa(int(v.Val()))), nil
|
||||
|
@ -52,6 +52,11 @@ func (v *Int64) Cas(old, new int64) bool {
|
||||
return atomic.CompareAndSwapInt64(&v.value, old, new)
|
||||
}
|
||||
|
||||
// String implements String interface for string printing.
|
||||
func (v *Int64) String() string {
|
||||
return strconv.FormatInt(v.Val(), 10)
|
||||
}
|
||||
|
||||
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
|
||||
func (v *Int64) MarshalJSON() ([]byte, error) {
|
||||
return gconv.UnsafeStrToBytes(strconv.FormatInt(v.Val(), 10)), nil
|
||||
|
@ -8,6 +8,7 @@ package gtype
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/gogf/gf/util/gconv"
|
||||
"sync/atomic"
|
||||
)
|
||||
|
||||
@ -43,6 +44,11 @@ func (v *Interface) Val() interface{} {
|
||||
return v.value.Load()
|
||||
}
|
||||
|
||||
// String implements String interface for string printing.
|
||||
func (v *Interface) String() string {
|
||||
return gconv.String(v.Val())
|
||||
}
|
||||
|
||||
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
|
||||
func (v *Interface) MarshalJSON() ([]byte, error) {
|
||||
return json.Marshal(v.Val())
|
||||
|
@ -47,6 +47,11 @@ func (v *String) Val() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
// String implements String interface for string printing.
|
||||
func (v *String) String() string {
|
||||
return v.Val()
|
||||
}
|
||||
|
||||
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
|
||||
func (v *String) MarshalJSON() ([]byte, error) {
|
||||
return gconv.UnsafeStrToBytes(`"` + v.Val() + `"`), nil
|
||||
|
@ -52,6 +52,11 @@ func (v *Uint) Cas(old, new uint) bool {
|
||||
return atomic.CompareAndSwapUint64(&v.value, uint64(old), uint64(new))
|
||||
}
|
||||
|
||||
// String implements String interface for string printing.
|
||||
func (v *Uint) String() string {
|
||||
return strconv.FormatUint(uint64(v.Val()), 10)
|
||||
}
|
||||
|
||||
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
|
||||
func (v *Uint) MarshalJSON() ([]byte, error) {
|
||||
return gconv.UnsafeStrToBytes(strconv.FormatUint(uint64(v.Val()), 10)), nil
|
||||
|
@ -52,6 +52,11 @@ func (v *Uint32) Cas(old, new uint32) bool {
|
||||
return atomic.CompareAndSwapUint32(&v.value, old, new)
|
||||
}
|
||||
|
||||
// String implements String interface for string printing.
|
||||
func (v *Uint32) String() string {
|
||||
return strconv.FormatUint(uint64(v.Val()), 10)
|
||||
}
|
||||
|
||||
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
|
||||
func (v *Uint32) MarshalJSON() ([]byte, error) {
|
||||
return gconv.UnsafeStrToBytes(strconv.FormatUint(uint64(v.Val()), 10)), nil
|
||||
|
@ -52,6 +52,11 @@ func (v *Uint64) Cas(old, new uint64) bool {
|
||||
return atomic.CompareAndSwapUint64(&v.value, old, new)
|
||||
}
|
||||
|
||||
// String implements String interface for string printing.
|
||||
func (v *Uint64) String() string {
|
||||
return strconv.FormatUint(v.Val(), 10)
|
||||
}
|
||||
|
||||
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
|
||||
func (v *Uint64) MarshalJSON() ([]byte, error) {
|
||||
return gconv.UnsafeStrToBytes(strconv.FormatUint(v.Val(), 10)), nil
|
||||
|
@ -217,7 +217,7 @@ func (bs *dbBase) GetStruct(pointer interface{}, query string, args ...interface
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return one.ToStruct(pointer)
|
||||
return one.Struct(pointer)
|
||||
}
|
||||
|
||||
// 数据库查询,查询多条记录,并自动转换为指定的slice对象, 如: []struct/[]*struct。
|
||||
@ -226,7 +226,7 @@ func (bs *dbBase) GetStructs(pointer interface{}, query string, args ...interfac
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return all.ToStructs(pointer)
|
||||
return all.Structs(pointer)
|
||||
}
|
||||
|
||||
// 将结果转换为指定的struct/*struct/[]struct/[]*struct,
|
||||
@ -415,9 +415,9 @@ func (bs *dbBase) doBatchInsert(link dbLink, table string, list interface{}, opt
|
||||
listMap := (List)(nil)
|
||||
switch v := list.(type) {
|
||||
case Result:
|
||||
listMap = v.ToList()
|
||||
listMap = v.List()
|
||||
case Record:
|
||||
listMap = List{v.ToMap()}
|
||||
listMap = List{v.Map()}
|
||||
case List:
|
||||
listMap = v
|
||||
case Map:
|
||||
|
@ -310,9 +310,9 @@ func (md *Model) Data(data ...interface{}) *Model {
|
||||
} else {
|
||||
switch params := data[0].(type) {
|
||||
case Result:
|
||||
model.data = params.ToList()
|
||||
model.data = params.List()
|
||||
case Record:
|
||||
model.data = params.ToMap()
|
||||
model.data = params.Map()
|
||||
case List:
|
||||
model.data = params
|
||||
case Map:
|
||||
|
@ -309,9 +309,9 @@ func (db *dbOracle) doBatchInsert(link dbLink, table string, list interface{}, o
|
||||
listMap := (List)(nil)
|
||||
switch v := list.(type) {
|
||||
case Result:
|
||||
listMap = v.ToList()
|
||||
listMap = v.List()
|
||||
case Record:
|
||||
listMap = List{v.ToMap()}
|
||||
listMap = List{v.Map()}
|
||||
case List:
|
||||
listMap = v
|
||||
case Map:
|
||||
|
@ -74,7 +74,7 @@ func (tx *TX) GetStruct(obj interface{}, query string, args ...interface{}) erro
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return one.ToStruct(obj)
|
||||
return one.Struct(obj)
|
||||
}
|
||||
|
||||
// 数据库查询,查询多条记录,并自动转换为指定的slice对象, 如: []struct/[]*struct。
|
||||
@ -83,7 +83,7 @@ func (tx *TX) GetStructs(objPointerSlice interface{}, query string, args ...inte
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return all.ToStructs(objPointerSlice)
|
||||
return all.Structs(objPointerSlice)
|
||||
}
|
||||
|
||||
// 将结果转换为指定的struct/*struct/[]struct/[]*struct,
|
||||
|
@ -15,13 +15,13 @@ import (
|
||||
|
||||
// 将记录结果转换为JSON字符串
|
||||
func (r Record) Json() string {
|
||||
content, _ := gparser.VarToJson(r.ToMap())
|
||||
content, _ := gparser.VarToJson(r.Map())
|
||||
return string(content)
|
||||
}
|
||||
|
||||
// 将记录结果转换为XML字符串
|
||||
func (r Record) Xml(rootTag ...string) string {
|
||||
content, _ := gparser.VarToXml(r.ToMap(), rootTag...)
|
||||
content, _ := gparser.VarToXml(r.Map(), rootTag...)
|
||||
return string(content)
|
||||
}
|
||||
|
||||
|
@ -13,13 +13,13 @@ import (
|
||||
|
||||
// Deprecated.
|
||||
func (r Record) ToJson() string {
|
||||
content, _ := gparser.VarToJson(r.ToMap())
|
||||
content, _ := gparser.VarToJson(r.Map())
|
||||
return string(content)
|
||||
}
|
||||
|
||||
// Deprecated.
|
||||
func (r Record) ToXml(rootTag ...string) string {
|
||||
content, _ := gparser.VarToXml(r.ToMap(), rootTag...)
|
||||
content, _ := gparser.VarToXml(r.Map(), rootTag...)
|
||||
return string(content)
|
||||
}
|
||||
|
||||
@ -37,5 +37,5 @@ func (r Record) ToStruct(pointer interface{}) error {
|
||||
if r == nil {
|
||||
return sql.ErrNoRows
|
||||
}
|
||||
return mapToStruct(r.ToMap(), pointer)
|
||||
return mapToStruct(r.Map(), pointer)
|
||||
}
|
||||
|
@ -16,13 +16,13 @@ import (
|
||||
|
||||
// Deprecated.
|
||||
func (r Result) ToJson() string {
|
||||
content, _ := gparser.VarToJson(r.ToList())
|
||||
content, _ := gparser.VarToJson(r.List())
|
||||
return string(content)
|
||||
}
|
||||
|
||||
// Deprecated.
|
||||
func (r Result) ToXml(rootTag ...string) string {
|
||||
content, _ := gparser.VarToXml(r.ToList(), rootTag...)
|
||||
content, _ := gparser.VarToXml(r.List(), rootTag...)
|
||||
return string(content)
|
||||
}
|
||||
|
||||
@ -30,7 +30,7 @@ func (r Result) ToXml(rootTag ...string) string {
|
||||
func (r Result) ToList() List {
|
||||
l := make(List, len(r))
|
||||
for k, v := range r {
|
||||
l[k] = v.ToMap()
|
||||
l[k] = v.Map()
|
||||
}
|
||||
return l
|
||||
}
|
||||
@ -40,7 +40,7 @@ func (r Result) ToStringMap(key string) map[string]Map {
|
||||
m := make(map[string]Map)
|
||||
for _, item := range r {
|
||||
if v, ok := item[key]; ok {
|
||||
m[v.String()] = item.ToMap()
|
||||
m[v.String()] = item.Map()
|
||||
}
|
||||
}
|
||||
return m
|
||||
@ -51,7 +51,7 @@ func (r Result) ToIntMap(key string) map[int]Map {
|
||||
m := make(map[int]Map)
|
||||
for _, item := range r {
|
||||
if v, ok := item[key]; ok {
|
||||
m[v.Int()] = item.ToMap()
|
||||
m[v.Int()] = item.Map()
|
||||
}
|
||||
}
|
||||
return m
|
||||
@ -62,7 +62,7 @@ func (r Result) ToUintMap(key string) map[uint]Map {
|
||||
m := make(map[uint]Map)
|
||||
for _, item := range r {
|
||||
if v, ok := item[key]; ok {
|
||||
m[v.Uint()] = item.ToMap()
|
||||
m[v.Uint()] = item.Map()
|
||||
}
|
||||
}
|
||||
return m
|
||||
@ -116,13 +116,13 @@ func (r Result) ToStructs(pointer interface{}) (err error) {
|
||||
for i := 0; i < l; i++ {
|
||||
if itemType.Kind() == reflect.Ptr {
|
||||
e := reflect.New(itemType.Elem()).Elem()
|
||||
if err = r[i].ToStruct(e); err != nil {
|
||||
if err = r[i].Struct(e); err != nil {
|
||||
return err
|
||||
}
|
||||
array.Index(i).Set(e.Addr())
|
||||
} else {
|
||||
e := reflect.New(itemType).Elem()
|
||||
if err = r[i].ToStruct(e); err != nil {
|
||||
if err = r[i].Struct(e); err != nil {
|
||||
return err
|
||||
}
|
||||
array.Index(i).Set(e)
|
||||
|
@ -96,8 +96,8 @@ func Test_Model_Inherit_MapToStruct_Mssql(t *testing.T) {
|
||||
gtest.Assert(err, nil)
|
||||
|
||||
user := new(User)
|
||||
fmt.Println(one.ToJson())
|
||||
gtest.Assert(one.ToStruct(user), nil)
|
||||
fmt.Println(one.Json())
|
||||
gtest.Assert(one.Struct(user), nil)
|
||||
gtest.Assert(user.Id, data["id"])
|
||||
gtest.Assert(user.Passport, data["passport"])
|
||||
gtest.Assert(strings.TrimSpace(user.Password), data["password"])
|
||||
|
@ -656,16 +656,16 @@ func Test_DB_ToJson(t *testing.T) {
|
||||
|
||||
users := make([]User, 0)
|
||||
|
||||
err = result.ToStructs(users)
|
||||
err = result.Structs(users)
|
||||
gtest.AssertNE(err, nil)
|
||||
|
||||
err = result.ToStructs(&users)
|
||||
err = result.Structs(&users)
|
||||
if err != nil {
|
||||
gtest.Fatal(err)
|
||||
}
|
||||
|
||||
//ToJson
|
||||
resultJson, err := gjson.LoadContent(result.ToJson())
|
||||
resultJson, err := gjson.LoadContent(result.Json())
|
||||
if err != nil {
|
||||
gtest.Fatal(err)
|
||||
}
|
||||
@ -677,7 +677,7 @@ func Test_DB_ToJson(t *testing.T) {
|
||||
gtest.Assert(users[0].CreateTime, resultJson.GetString("0.create_time"))
|
||||
|
||||
result = nil
|
||||
err = result.ToStructs(&users)
|
||||
err = result.Structs(&users)
|
||||
gtest.AssertNE(err, nil)
|
||||
})
|
||||
|
||||
@ -697,13 +697,13 @@ func Test_DB_ToJson(t *testing.T) {
|
||||
|
||||
users := User{}
|
||||
|
||||
err = result.ToStruct(&users)
|
||||
err = result.Struct(&users)
|
||||
if err != nil {
|
||||
gtest.Fatal(err)
|
||||
}
|
||||
|
||||
result = nil
|
||||
err = result.ToStruct(&users)
|
||||
err = result.Struct(&users)
|
||||
gtest.AssertNE(err, nil)
|
||||
})
|
||||
}
|
||||
@ -730,12 +730,12 @@ func Test_DB_ToXml(t *testing.T) {
|
||||
}
|
||||
|
||||
user := User{}
|
||||
err = record.ToStruct(&user)
|
||||
err = record.Struct(&user)
|
||||
if err != nil {
|
||||
gtest.Fatal(err)
|
||||
}
|
||||
|
||||
result, err := gxml.Decode([]byte(record.ToXml("doc")))
|
||||
result, err := gxml.Decode([]byte(record.Xml("doc")))
|
||||
if err != nil {
|
||||
gtest.Fatal(err)
|
||||
}
|
||||
@ -796,12 +796,12 @@ func Test_DB_ToStringMap(t *testing.T) {
|
||||
}
|
||||
|
||||
t_users := make([]t_user, 0)
|
||||
err = result.ToStructs(&t_users)
|
||||
err = result.Structs(&t_users)
|
||||
if err != nil {
|
||||
gtest.Fatal(err)
|
||||
}
|
||||
|
||||
resultStringMap := result.ToStringMap("id")
|
||||
resultStringMap := result.MapKeyStr("id")
|
||||
gtest.Assert(t_users[0].Id, resultStringMap[id]["id"])
|
||||
gtest.Assert(t_users[0].Passport, resultStringMap[id]["passport"])
|
||||
gtest.Assert(t_users[0].Password, resultStringMap[id]["password"])
|
||||
@ -833,12 +833,12 @@ func Test_DB_ToIntMap(t *testing.T) {
|
||||
}
|
||||
|
||||
t_users := make([]t_user, 0)
|
||||
err = result.ToStructs(&t_users)
|
||||
err = result.Structs(&t_users)
|
||||
if err != nil {
|
||||
gtest.Fatal(err)
|
||||
}
|
||||
|
||||
resultIntMap := result.ToIntMap("id")
|
||||
resultIntMap := result.MapKeyInt("id")
|
||||
gtest.Assert(t_users[0].Id, resultIntMap[id]["id"])
|
||||
gtest.Assert(t_users[0].Passport, resultIntMap[id]["passport"])
|
||||
gtest.Assert(t_users[0].Password, resultIntMap[id]["password"])
|
||||
@ -870,12 +870,12 @@ func Test_DB_ToUintMap(t *testing.T) {
|
||||
}
|
||||
|
||||
t_users := make([]t_user, 0)
|
||||
err = result.ToStructs(&t_users)
|
||||
err = result.Structs(&t_users)
|
||||
if err != nil {
|
||||
gtest.Fatal(err)
|
||||
}
|
||||
|
||||
resultUintMap := result.ToUintMap("id")
|
||||
resultUintMap := result.MapKeyUint("id")
|
||||
gtest.Assert(t_users[0].Id, resultUintMap[uint(id)]["id"])
|
||||
gtest.Assert(t_users[0].Passport, resultUintMap[uint(id)]["passport"])
|
||||
gtest.Assert(t_users[0].Password, resultUintMap[uint(id)]["password"])
|
||||
@ -909,12 +909,12 @@ func Test_DB_ToStringRecord(t *testing.T) {
|
||||
}
|
||||
|
||||
t_users := make([]t_user, 0)
|
||||
err = result.ToStructs(&t_users)
|
||||
err = result.Structs(&t_users)
|
||||
if err != nil {
|
||||
gtest.Fatal(err)
|
||||
}
|
||||
|
||||
resultStringRecord := result.ToStringRecord("id")
|
||||
resultStringRecord := result.RecordKeyStr("id")
|
||||
gtest.Assert(t_users[0].Id, resultStringRecord[ids]["id"].Int())
|
||||
gtest.Assert(t_users[0].Passport, resultStringRecord[ids]["passport"].String())
|
||||
gtest.Assert(t_users[0].Password, resultStringRecord[ids]["password"].String())
|
||||
@ -947,12 +947,12 @@ func Test_DB_ToIntRecord(t *testing.T) {
|
||||
}
|
||||
|
||||
t_users := make([]t_user, 0)
|
||||
err = result.ToStructs(&t_users)
|
||||
err = result.Structs(&t_users)
|
||||
if err != nil {
|
||||
gtest.Fatal(err)
|
||||
}
|
||||
|
||||
resultIntRecord := result.ToIntRecord("id")
|
||||
resultIntRecord := result.RecordKeyInt("id")
|
||||
gtest.Assert(t_users[0].Id, resultIntRecord[id]["id"].Int())
|
||||
gtest.Assert(t_users[0].Passport, resultIntRecord[id]["passport"].String())
|
||||
gtest.Assert(t_users[0].Password, resultIntRecord[id]["password"].String())
|
||||
@ -985,12 +985,12 @@ func Test_DB_ToUintRecord(t *testing.T) {
|
||||
}
|
||||
|
||||
t_users := make([]t_user, 0)
|
||||
err = result.ToStructs(&t_users)
|
||||
err = result.Structs(&t_users)
|
||||
if err != nil {
|
||||
gtest.Fatal(err)
|
||||
}
|
||||
|
||||
resultUintRecord := result.ToUintRecord("id")
|
||||
resultUintRecord := result.RecordKeyUint("id")
|
||||
gtest.Assert(t_users[0].Id, resultUintRecord[uint(id)]["id"].Int())
|
||||
gtest.Assert(t_users[0].Passport, resultUintRecord[uint(id)]["passport"].String())
|
||||
gtest.Assert(t_users[0].Password, resultUintRecord[uint(id)]["password"].String())
|
||||
|
@ -86,7 +86,7 @@ func Test_Model_Inherit_MapToStruct(t *testing.T) {
|
||||
|
||||
user := new(User)
|
||||
|
||||
gtest.Assert(one.ToStruct(user), nil)
|
||||
gtest.Assert(one.Struct(user), nil)
|
||||
gtest.Assert(user.Id, data["id"])
|
||||
gtest.Assert(user.Passport, data["passport"])
|
||||
gtest.Assert(user.Password, data["password"])
|
||||
|
@ -96,7 +96,7 @@ func Test_Model_Inherit_MapToStruct_Oracle(t *testing.T) {
|
||||
|
||||
user := new(User)
|
||||
|
||||
gtest.Assert(one.ToStruct(user), nil)
|
||||
gtest.Assert(one.Struct(user), nil)
|
||||
gtest.Assert(user.Id, data["id"])
|
||||
gtest.Assert(user.Passport, data["passport"])
|
||||
gtest.Assert(strings.TrimSpace(user.Password), data["password"])
|
||||
|
@ -642,16 +642,16 @@ func Test_DB_ToJson_Pgsql(t *testing.T) {
|
||||
|
||||
users := make([]User, 0)
|
||||
|
||||
err = result.ToStructs(users)
|
||||
err = result.Structs(users)
|
||||
gtest.AssertNE(err, nil)
|
||||
|
||||
err = result.ToStructs(&users)
|
||||
err = result.Structs(&users)
|
||||
if err != nil {
|
||||
gtest.Fatal(err)
|
||||
}
|
||||
|
||||
//ToJson
|
||||
resultJson, err := gjson.LoadContent(result.ToJson())
|
||||
resultJson, err := gjson.LoadContent(result.Json())
|
||||
if err != nil {
|
||||
gtest.Fatal(err)
|
||||
}
|
||||
@ -663,7 +663,7 @@ func Test_DB_ToJson_Pgsql(t *testing.T) {
|
||||
gtest.Assert(users[0].CreateTime, resultJson.GetString("0.create_time"))
|
||||
|
||||
result = nil
|
||||
err = result.ToStructs(&users)
|
||||
err = result.Structs(&users)
|
||||
gtest.AssertNE(err, nil)
|
||||
})
|
||||
|
||||
@ -683,13 +683,13 @@ func Test_DB_ToJson_Pgsql(t *testing.T) {
|
||||
|
||||
users := User{}
|
||||
|
||||
err = result.ToStruct(&users)
|
||||
err = result.Struct(&users)
|
||||
if err != nil {
|
||||
gtest.Fatal(err)
|
||||
}
|
||||
|
||||
result = nil
|
||||
err = result.ToStruct(&users)
|
||||
err = result.Struct(&users)
|
||||
gtest.AssertNE(err, nil)
|
||||
})
|
||||
}
|
||||
@ -719,12 +719,12 @@ func Test_DB_ToXml_Pgsql(t *testing.T) {
|
||||
}
|
||||
|
||||
user := User{}
|
||||
err = record.ToStruct(&user)
|
||||
err = record.Struct(&user)
|
||||
if err != nil {
|
||||
gtest.Fatal(err)
|
||||
}
|
||||
|
||||
result, err := gxml.Decode([]byte(record.ToXml("doc")))
|
||||
result, err := gxml.Decode([]byte(record.Xml("doc")))
|
||||
if err != nil {
|
||||
gtest.Fatal(err)
|
||||
}
|
||||
@ -788,12 +788,12 @@ func Test_DB_ToStringMap_Pgsql(t *testing.T) {
|
||||
}
|
||||
|
||||
t_users := make([]t_user, 0)
|
||||
err = result.ToStructs(&t_users)
|
||||
err = result.Structs(&t_users)
|
||||
if err != nil {
|
||||
gtest.Fatal(err)
|
||||
}
|
||||
|
||||
resultStringMap := result.ToStringMap("id")
|
||||
resultStringMap := result.MapKeyStr("id")
|
||||
gtest.Assert(t_users[0].Id, resultStringMap[id]["id"])
|
||||
gtest.Assert(t_users[0].Passport, resultStringMap[id]["passport"])
|
||||
gtest.Assert(t_users[0].Password, resultStringMap[id]["password"])
|
||||
@ -827,12 +827,12 @@ func Test_DB_ToIntMap_Pgsql(t *testing.T) {
|
||||
}
|
||||
|
||||
t_users := make([]t_user, 0)
|
||||
err = result.ToStructs(&t_users)
|
||||
err = result.Structs(&t_users)
|
||||
if err != nil {
|
||||
gtest.Fatal(err)
|
||||
}
|
||||
|
||||
resultIntMap := result.ToIntMap("id")
|
||||
resultIntMap := result.MapKeyInt("id")
|
||||
gtest.Assert(t_users[0].Id, resultIntMap[id]["id"])
|
||||
gtest.Assert(t_users[0].Passport, resultIntMap[id]["passport"])
|
||||
gtest.Assert(t_users[0].Password, resultIntMap[id]["password"])
|
||||
@ -866,12 +866,12 @@ func Test_DB_ToUintMap_Pgsql(t *testing.T) {
|
||||
}
|
||||
|
||||
t_users := make([]t_user, 0)
|
||||
err = result.ToStructs(&t_users)
|
||||
err = result.Structs(&t_users)
|
||||
if err != nil {
|
||||
gtest.Fatal(err)
|
||||
}
|
||||
|
||||
resultUintMap := result.ToUintMap("id")
|
||||
resultUintMap := result.MapKeyUint("id")
|
||||
gtest.Assert(t_users[0].Id, resultUintMap[uint(id)]["id"])
|
||||
gtest.Assert(t_users[0].Passport, resultUintMap[uint(id)]["passport"])
|
||||
gtest.Assert(t_users[0].Password, resultUintMap[uint(id)]["password"])
|
||||
@ -908,12 +908,12 @@ func Test_DB_ToStringRecord_Pgsql(t *testing.T) {
|
||||
}
|
||||
|
||||
t_users := make([]t_user, 0)
|
||||
err = result.ToStructs(&t_users)
|
||||
err = result.Structs(&t_users)
|
||||
if err != nil {
|
||||
gtest.Fatal(err)
|
||||
}
|
||||
|
||||
resultStringRecord := result.ToStringRecord("id")
|
||||
resultStringRecord := result.RecordKeyStr("id")
|
||||
gtest.Assert(t_users[0].Id, resultStringRecord[ids]["id"].Int())
|
||||
gtest.Assert(t_users[0].Passport, resultStringRecord[ids]["passport"].String())
|
||||
gtest.Assert(t_users[0].Password, resultStringRecord[ids]["password"].String())
|
||||
@ -948,12 +948,12 @@ func Test_DB_ToIntRecord_Pgsql(t *testing.T) {
|
||||
}
|
||||
|
||||
t_users := make([]t_user, 0)
|
||||
err = result.ToStructs(&t_users)
|
||||
err = result.Structs(&t_users)
|
||||
if err != nil {
|
||||
gtest.Fatal(err)
|
||||
}
|
||||
|
||||
resultIntRecord := result.ToIntRecord("id")
|
||||
resultIntRecord := result.RecordKeyInt("id")
|
||||
gtest.Assert(t_users[0].Id, resultIntRecord[id]["id"].Int())
|
||||
gtest.Assert(t_users[0].Passport, resultIntRecord[id]["passport"].String())
|
||||
gtest.Assert(t_users[0].Password, resultIntRecord[id]["password"].String())
|
||||
@ -988,12 +988,12 @@ func Test_DB_ToUintRecord_Pgsql(t *testing.T) {
|
||||
}
|
||||
|
||||
t_users := make([]t_user, 0)
|
||||
err = result.ToStructs(&t_users)
|
||||
err = result.Structs(&t_users)
|
||||
if err != nil {
|
||||
gtest.Fatal(err)
|
||||
}
|
||||
|
||||
resultUintRecord := result.ToUintRecord("id")
|
||||
resultUintRecord := result.RecordKeyUint("id")
|
||||
gtest.Assert(t_users[0].Id, resultUintRecord[uint(id)]["id"].Int())
|
||||
gtest.Assert(t_users[0].Passport, resultUintRecord[uint(id)]["passport"].String())
|
||||
gtest.Assert(t_users[0].Password, resultUintRecord[uint(id)]["password"].String())
|
||||
|
@ -26,13 +26,21 @@ func IsLetterLower(b byte) bool {
|
||||
}
|
||||
|
||||
// IsNumeric tests whether the given string s is numeric.
|
||||
// Note that float string like "123.456" is also numeric.
|
||||
func IsNumeric(s string) bool {
|
||||
length := len(s)
|
||||
if length == 0 {
|
||||
return false
|
||||
}
|
||||
for i := 0; i < len(s); i++ {
|
||||
if s[i] < byte('0') || s[i] > byte('9') {
|
||||
if s[i] == '.' {
|
||||
if i > 0 && i < len(s)-1 {
|
||||
continue
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
if s[i] < '0' || s[i] > '9' {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user