mirror of
https://gitee.com/johng/gf.git
synced 2024-12-02 12:17:53 +08:00
Merge branch 'master' of https://github.com/gogf/gf into gtree_Example
This commit is contained in:
commit
0ded168715
773
container/gvar/gvar_z_example_test.go
Normal file
773
container/gvar/gvar_z_example_test.go
Normal file
@ -0,0 +1,773 @@
|
||||
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
|
||||
package gvar_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gogf/gf/v2/container/gvar"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/internal/json"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
)
|
||||
|
||||
// New
|
||||
func ExampleVarNew() {
|
||||
v := gvar.New(400)
|
||||
g.Dump(v)
|
||||
|
||||
// Output:
|
||||
// "400"
|
||||
}
|
||||
|
||||
// Create
|
||||
func ExampleVarCreate() {
|
||||
var v = gvar.Create(100.0)
|
||||
g.Dump(&v)
|
||||
|
||||
// Output:
|
||||
// "100"
|
||||
}
|
||||
|
||||
// Clone
|
||||
func ExampleVar_Clone() {
|
||||
tmp := "fisrt hello"
|
||||
v := gvar.New(tmp)
|
||||
g.DumpWithType(v.Clone())
|
||||
fmt.Println(v == v.Clone())
|
||||
|
||||
// Output:
|
||||
// *gvar.Var(11) "fisrt hello"
|
||||
// false
|
||||
}
|
||||
|
||||
// Set
|
||||
func ExampleVar_Set() {
|
||||
var v = gvar.New(100.00)
|
||||
g.Dump(v.Set(200.00))
|
||||
g.Dump(v)
|
||||
|
||||
// Output:
|
||||
// 100
|
||||
// "200"
|
||||
}
|
||||
|
||||
// Val
|
||||
func ExampleVar_Val() {
|
||||
var v = gvar.New(100.00)
|
||||
g.DumpWithType(v.Val())
|
||||
|
||||
// Output:
|
||||
// float64(100)
|
||||
}
|
||||
|
||||
// Interface
|
||||
func ExampleVar_Interface() {
|
||||
var v = gvar.New(100.00)
|
||||
g.DumpWithType(v.Interface())
|
||||
|
||||
// Output:
|
||||
// float64(100)
|
||||
}
|
||||
|
||||
// Bytes
|
||||
func ExampleVar_Bytes() {
|
||||
var v = gvar.New("GoFrame")
|
||||
g.DumpWithType(v.Bytes())
|
||||
|
||||
// Output:
|
||||
// []byte(7) "GoFrame"
|
||||
}
|
||||
|
||||
// String
|
||||
func ExampleVar_String() {
|
||||
var v = gvar.New("GoFrame")
|
||||
g.DumpWithType(v.String())
|
||||
|
||||
// Output:
|
||||
// string(7) "GoFrame"
|
||||
}
|
||||
|
||||
// Bool
|
||||
func ExampleVar_Bool() {
|
||||
var v = gvar.New(true)
|
||||
g.DumpWithType(v.Bool())
|
||||
|
||||
// Output:
|
||||
// bool(true)
|
||||
}
|
||||
|
||||
// Int
|
||||
func ExampleVar_Int() {
|
||||
var v = gvar.New(-1000)
|
||||
g.DumpWithType(v.Int())
|
||||
|
||||
// Output:
|
||||
// int(-1000)
|
||||
}
|
||||
|
||||
// Uint
|
||||
func ExampleVar_Uint() {
|
||||
var v = gvar.New(1000)
|
||||
g.DumpWithType(v.Uint())
|
||||
|
||||
// Output:
|
||||
// uint(1000)
|
||||
}
|
||||
|
||||
// Float32
|
||||
func ExampleVar_Float32() {
|
||||
var price = gvar.New(100.00)
|
||||
g.DumpWithType(price.Float32())
|
||||
|
||||
// Output:
|
||||
// float32(100)
|
||||
}
|
||||
|
||||
// Time
|
||||
func ExampleVar_Time() {
|
||||
var v = gvar.New("2021-11-11 00:00:00")
|
||||
g.DumpWithType(v.Time())
|
||||
|
||||
// Output:
|
||||
// time.Time(29) "2021-11-11 00:00:00 +0800 CST"
|
||||
}
|
||||
|
||||
// GTime
|
||||
func ExampleVar_GTime() {
|
||||
var v = gvar.New("2021-11-11 00:00:00")
|
||||
g.DumpWithType(v.GTime())
|
||||
|
||||
// Output:
|
||||
// *gtime.Time(19) "2021-11-11 00:00:00"
|
||||
}
|
||||
|
||||
// Duration
|
||||
func ExampleVar_Duration() {
|
||||
var v = gvar.New("300s")
|
||||
g.DumpWithType(v.Duration())
|
||||
|
||||
// Output:
|
||||
// time.Duration(4) "5m0s"
|
||||
}
|
||||
|
||||
// MarshalJSON
|
||||
func ExampleVar_MarshalJSON() {
|
||||
testMap := g.Map{
|
||||
"code": "0001",
|
||||
"name": "Golang",
|
||||
"count": 10,
|
||||
}
|
||||
|
||||
var v = gvar.New(testMap)
|
||||
res, err := json.Marshal(&v)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
g.DumpWithType(res)
|
||||
|
||||
// Output:
|
||||
// []byte(42) "{"code":"0001","count":10,"name":"Golang"}"
|
||||
}
|
||||
|
||||
// UnmarshalJSON
|
||||
func ExampleVar_UnmarshalJSON() {
|
||||
tmp := []byte(`{
|
||||
"Code": "0003",
|
||||
"Name": "Golang Book3",
|
||||
"Quantity": 3000,
|
||||
"Price": 300,
|
||||
"OnSale": true
|
||||
}`)
|
||||
var v = gvar.New(map[string]interface{}{})
|
||||
if err := json.Unmarshal(tmp, &v); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
g.Dump(v)
|
||||
|
||||
// Output:
|
||||
// "{\"Code\":\"0003\",\"Name\":\"Golang Book3\",\"OnSale\":true,\"Price\":300,\"Quantity\":3000}"
|
||||
}
|
||||
|
||||
// UnmarshalValue
|
||||
func ExampleVar_UnmarshalValue() {
|
||||
tmp := g.Map{
|
||||
"code": "00002",
|
||||
"name": "GoFrame",
|
||||
"price": 100,
|
||||
"sale": true,
|
||||
}
|
||||
|
||||
var v = gvar.New(map[string]interface{}{})
|
||||
if err := v.UnmarshalValue(tmp); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
g.Dump(v)
|
||||
|
||||
// Output:
|
||||
// "{\"code\":\"00002\",\"name\":\"GoFrame\",\"price\":100,\"sale\":true}"
|
||||
}
|
||||
|
||||
// IsNil
|
||||
func ExampleVar_IsNil() {
|
||||
g.Dump(gvar.New(0).IsNil())
|
||||
g.Dump(gvar.New(0.1).IsNil())
|
||||
// true
|
||||
g.Dump(gvar.New(nil).IsNil())
|
||||
g.Dump(gvar.New("").IsNil())
|
||||
|
||||
// Output:
|
||||
// false
|
||||
// false
|
||||
// true
|
||||
// false
|
||||
}
|
||||
|
||||
// IsEmpty
|
||||
func ExampleVar_IsEmpty() {
|
||||
g.Dump(gvar.New(0).IsEmpty())
|
||||
g.Dump(gvar.New(nil).IsEmpty())
|
||||
g.Dump(gvar.New("").IsEmpty())
|
||||
g.Dump(gvar.New(g.Map{"k": "v"}).IsEmpty())
|
||||
|
||||
// Output:
|
||||
// true
|
||||
// true
|
||||
// true
|
||||
// false
|
||||
}
|
||||
|
||||
// IsInt
|
||||
func ExampleVar_IsInt() {
|
||||
g.Dump(gvar.New(0).IsInt())
|
||||
g.Dump(gvar.New(0.1).IsInt())
|
||||
g.Dump(gvar.New(nil).IsInt())
|
||||
g.Dump(gvar.New("").IsInt())
|
||||
|
||||
// Output:
|
||||
// true
|
||||
// false
|
||||
// false
|
||||
// false
|
||||
}
|
||||
|
||||
// IsUint
|
||||
func ExampleVar_IsUint() {
|
||||
g.Dump(gvar.New(0).IsUint())
|
||||
g.Dump(gvar.New(uint8(8)).IsUint())
|
||||
g.Dump(gvar.New(nil).IsUint())
|
||||
|
||||
// Output:
|
||||
// false
|
||||
// true
|
||||
// false
|
||||
}
|
||||
|
||||
// IsFloat
|
||||
func ExampleVar_IsFloat() {
|
||||
g.Dump(g.NewVar(uint8(8)).IsFloat())
|
||||
g.Dump(g.NewVar(float64(8)).IsFloat())
|
||||
g.Dump(g.NewVar(0.1).IsFloat())
|
||||
|
||||
// Output:
|
||||
// false
|
||||
// true
|
||||
// true
|
||||
}
|
||||
|
||||
// IsSlice
|
||||
func ExampleVar_IsSlice() {
|
||||
g.Dump(g.NewVar(0).IsSlice())
|
||||
g.Dump(g.NewVar(g.Slice{0}).IsSlice())
|
||||
|
||||
// Output:
|
||||
// false
|
||||
// true
|
||||
}
|
||||
|
||||
// IsMap
|
||||
func ExampleVar_IsMap() {
|
||||
g.Dump(g.NewVar(0).IsMap())
|
||||
g.Dump(g.NewVar(g.Map{"k": "v"}).IsMap())
|
||||
g.Dump(g.NewVar(g.Slice{}).IsMap())
|
||||
|
||||
// Output:
|
||||
// false
|
||||
// true
|
||||
// false
|
||||
}
|
||||
|
||||
// IsStruct
|
||||
func ExampleVar_IsStruct() {
|
||||
g.Dump(g.NewVar(0).IsStruct())
|
||||
g.Dump(g.NewVar(g.Map{"k": "v"}).IsStruct())
|
||||
|
||||
a := struct{}{}
|
||||
g.Dump(g.NewVar(a).IsStruct())
|
||||
g.Dump(g.NewVar(&a).IsStruct())
|
||||
|
||||
// Output:
|
||||
// false
|
||||
// false
|
||||
// true
|
||||
// true
|
||||
}
|
||||
|
||||
// ListItemValues
|
||||
func ExampleVar_ListItemValues() {
|
||||
var goods1 = g.List{
|
||||
g.Map{"id": 1, "price": 100.00},
|
||||
g.Map{"id": 2, "price": 0},
|
||||
g.Map{"id": 3, "price": nil},
|
||||
}
|
||||
var v = gvar.New(goods1)
|
||||
fmt.Println(v.ListItemValues("id"))
|
||||
fmt.Println(v.ListItemValues("price"))
|
||||
|
||||
// Output:
|
||||
// [1 2 3]
|
||||
// [100 0 <nil>]
|
||||
}
|
||||
|
||||
// ListItemValuesUnique
|
||||
func ExampleVar_ListItemValuesUnique() {
|
||||
var (
|
||||
goods1 = g.List{
|
||||
g.Map{"id": 1, "price": 100.00},
|
||||
g.Map{"id": 2, "price": 100.00},
|
||||
g.Map{"id": 3, "price": nil},
|
||||
}
|
||||
v = gvar.New(goods1)
|
||||
)
|
||||
|
||||
fmt.Println(v.ListItemValuesUnique("id"))
|
||||
fmt.Println(v.ListItemValuesUnique("price"))
|
||||
|
||||
// Output:
|
||||
// [1 2 3]
|
||||
// [100 <nil>]
|
||||
}
|
||||
|
||||
func ExampleVar_Struct() {
|
||||
params1 := g.Map{
|
||||
"uid": 1,
|
||||
"Name": "john",
|
||||
}
|
||||
v := gvar.New(params1)
|
||||
type tartget struct {
|
||||
Uid int
|
||||
Name string
|
||||
}
|
||||
t := new(tartget)
|
||||
if err := v.Struct(&t); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
g.Dump(t)
|
||||
|
||||
// Output:
|
||||
// {
|
||||
// Uid: 1,
|
||||
// Name: "john",
|
||||
// }
|
||||
}
|
||||
|
||||
func ExampleVar_Structs() {
|
||||
paramsArray := []g.Map{}
|
||||
params1 := g.Map{
|
||||
"uid": 1,
|
||||
"Name": "golang",
|
||||
}
|
||||
params2 := g.Map{
|
||||
"uid": 2,
|
||||
"Name": "java",
|
||||
}
|
||||
|
||||
paramsArray = append(paramsArray, params1, params2)
|
||||
v := gvar.New(paramsArray)
|
||||
type tartget struct {
|
||||
Uid int
|
||||
Name string
|
||||
}
|
||||
var t []tartget
|
||||
if err := v.Structs(&t); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
g.DumpWithType(t)
|
||||
|
||||
// Output:
|
||||
// []gvar_test.tartget(2) [
|
||||
// gvar_test.tartget(2) {
|
||||
// Uid: int(1),
|
||||
// Name: string(6) "golang",
|
||||
// },
|
||||
// gvar_test.tartget(2) {
|
||||
// Uid: int(2),
|
||||
// Name: string(4) "java",
|
||||
// },
|
||||
// ]
|
||||
}
|
||||
|
||||
// Ints
|
||||
func ExampleVar_Ints() {
|
||||
var (
|
||||
arr = []int{1, 2, 3, 4, 5}
|
||||
obj = gvar.New(arr)
|
||||
)
|
||||
|
||||
fmt.Println(obj.Ints())
|
||||
|
||||
// Output:
|
||||
// [1 2 3 4 5]
|
||||
}
|
||||
|
||||
// Int64s
|
||||
func ExampleVar_Int64s() {
|
||||
var (
|
||||
arr = []int64{1, 2, 3, 4, 5}
|
||||
obj = gvar.New(arr)
|
||||
)
|
||||
|
||||
fmt.Println(obj.Int64s())
|
||||
|
||||
// Output:
|
||||
// [1 2 3 4 5]
|
||||
}
|
||||
|
||||
// Uints
|
||||
func ExampleVar_Uints() {
|
||||
var (
|
||||
arr = []uint{1, 2, 3, 4, 5}
|
||||
obj = gvar.New(arr)
|
||||
)
|
||||
fmt.Println(obj.Uints())
|
||||
|
||||
// Output:
|
||||
// [1 2 3 4 5]
|
||||
}
|
||||
|
||||
// Uint64s
|
||||
func ExampleVar_Uint64s() {
|
||||
var (
|
||||
arr = []uint64{1, 2, 3, 4, 5}
|
||||
obj = gvar.New(arr)
|
||||
)
|
||||
|
||||
fmt.Println(obj.Uint64s())
|
||||
|
||||
// Output:
|
||||
// [1 2 3 4 5]
|
||||
}
|
||||
|
||||
// Floats
|
||||
func ExampleVar_Floats() {
|
||||
var (
|
||||
arr = []float64{1, 2, 3, 4, 5}
|
||||
obj = gvar.New(arr)
|
||||
)
|
||||
|
||||
fmt.Println(obj.Floats())
|
||||
|
||||
// Output:
|
||||
// [1 2 3 4 5]
|
||||
}
|
||||
|
||||
// Float32s
|
||||
func ExampleVar_Float32s() {
|
||||
var (
|
||||
arr = []float32{1, 2, 3, 4, 5}
|
||||
obj = gvar.New(arr)
|
||||
)
|
||||
|
||||
fmt.Println(obj.Float32s())
|
||||
|
||||
// Output:
|
||||
// [1 2 3 4 5]
|
||||
}
|
||||
|
||||
// Float64s
|
||||
func ExampleVar_Float64s() {
|
||||
var (
|
||||
arr = []float64{1, 2, 3, 4, 5}
|
||||
obj = gvar.New(arr)
|
||||
)
|
||||
|
||||
fmt.Println(obj.Float64s())
|
||||
|
||||
// Output:
|
||||
// [1 2 3 4 5]
|
||||
}
|
||||
|
||||
// Strings
|
||||
func ExampleVar_Strings() {
|
||||
var (
|
||||
arr = []string{"GoFrame", "Golang"}
|
||||
obj = gvar.New(arr)
|
||||
)
|
||||
fmt.Println(obj.Strings())
|
||||
|
||||
// Output:
|
||||
// [GoFrame Golang]
|
||||
}
|
||||
|
||||
// Interfaces
|
||||
func ExampleVar_Interfaces() {
|
||||
var (
|
||||
arr = []string{"GoFrame", "Golang"}
|
||||
obj = gvar.New(arr)
|
||||
)
|
||||
|
||||
fmt.Println(obj.Interfaces())
|
||||
|
||||
// Output:
|
||||
// [GoFrame Golang]
|
||||
}
|
||||
|
||||
// Slice
|
||||
func ExampleVar_Slice() {
|
||||
var (
|
||||
arr = []string{"GoFrame", "Golang"}
|
||||
obj = gvar.New(arr)
|
||||
)
|
||||
|
||||
fmt.Println(obj.Slice())
|
||||
|
||||
// Output:
|
||||
// [GoFrame Golang]
|
||||
}
|
||||
|
||||
// Array
|
||||
func ExampleVar_Array() {
|
||||
var (
|
||||
arr = []string{"GoFrame", "Golang"}
|
||||
obj = gvar.New(arr)
|
||||
)
|
||||
fmt.Println(obj.Array())
|
||||
|
||||
// Output:
|
||||
// [GoFrame Golang]
|
||||
}
|
||||
|
||||
// Vars
|
||||
func ExampleVar_Vars() {
|
||||
var (
|
||||
arr = []string{"GoFrame", "Golang"}
|
||||
obj = gvar.New(arr)
|
||||
)
|
||||
|
||||
fmt.Println(obj.Vars())
|
||||
|
||||
// Output:
|
||||
// [GoFrame Golang]
|
||||
}
|
||||
|
||||
// Map
|
||||
func ExampleVar_Map() {
|
||||
var (
|
||||
m = g.Map{"id": 1, "price": 100.00}
|
||||
v = gvar.New(m)
|
||||
res = v.Map()
|
||||
)
|
||||
|
||||
fmt.Println(res["id"], res["price"])
|
||||
|
||||
// Output:
|
||||
// 1 100
|
||||
}
|
||||
|
||||
// MapStrAny
|
||||
func ExampleVar_MapStrAny() {
|
||||
var (
|
||||
m1 = g.Map{"id": 1, "price": 100}
|
||||
v = gvar.New(m1)
|
||||
v2 = v.MapStrAny()
|
||||
)
|
||||
|
||||
fmt.Println(v2["price"], v2["id"])
|
||||
|
||||
// Output:
|
||||
// 100 1
|
||||
}
|
||||
|
||||
// MapStrStr
|
||||
func ExampleVar_MapStrStr() {
|
||||
var (
|
||||
m1 = g.Map{"id": 1, "price": 100}
|
||||
v = gvar.New(m1)
|
||||
v2 = v.MapStrStr()
|
||||
)
|
||||
|
||||
fmt.Println(v2["price"] + "$")
|
||||
|
||||
// Output:
|
||||
// 100$
|
||||
}
|
||||
|
||||
// MapStrVar
|
||||
func ExampleVar_MapStrVar() {
|
||||
var (
|
||||
m1 = g.Map{"id": 1, "price": 100}
|
||||
v = gvar.New(m1)
|
||||
v2 = v.MapStrVar()
|
||||
)
|
||||
|
||||
fmt.Println(v2["price"].Float64() * 100)
|
||||
|
||||
// Output:
|
||||
// 10000
|
||||
}
|
||||
|
||||
// MapDeep
|
||||
func ExampleVar_MapDeep() {
|
||||
var (
|
||||
m1 = g.Map{"id": 1, "price": 100}
|
||||
m2 = g.Map{"product": m1}
|
||||
v = gvar.New(m2)
|
||||
v2 = v.MapDeep()
|
||||
)
|
||||
|
||||
fmt.Println(v2["product"])
|
||||
|
||||
// Output:
|
||||
// map[id:1 price:100]
|
||||
}
|
||||
|
||||
// MapStrStrDeep
|
||||
func ExampleVar_MapStrStrDeep() {
|
||||
var (
|
||||
m1 = g.Map{"id": 1, "price": 100}
|
||||
m2 = g.Map{"product": m1}
|
||||
v = gvar.New(m2)
|
||||
v2 = v.MapStrStrDeep()
|
||||
)
|
||||
|
||||
fmt.Println(v2["product"])
|
||||
|
||||
// Output:
|
||||
// {"id":1,"price":100}
|
||||
}
|
||||
|
||||
// MapStrVarDeep
|
||||
func ExampleVar_MapStrVarDeep() {
|
||||
var (
|
||||
m1 = g.Map{"id": 1, "price": 100}
|
||||
m2 = g.Map{"product": m1}
|
||||
v = gvar.New(m2)
|
||||
v2 = v.MapStrVarDeep()
|
||||
)
|
||||
|
||||
fmt.Println(v2["product"])
|
||||
|
||||
// Output:
|
||||
// {"id":1,"price":100}
|
||||
}
|
||||
|
||||
// Maps
|
||||
func ExampleVar_Maps() {
|
||||
var m = gvar.New(g.ListIntInt{g.MapIntInt{0: 100, 1: 200}, g.MapIntInt{0: 300, 1: 400}})
|
||||
fmt.Printf("%#v", m.Maps())
|
||||
|
||||
// Output:
|
||||
// []map[string]interface {}{map[string]interface {}{"0":100, "1":200}, map[string]interface {}{"0":300, "1":400}}
|
||||
}
|
||||
|
||||
// MapsDeep
|
||||
func ExampleVar_MapsDeep() {
|
||||
var (
|
||||
p1 = g.MapStrAny{"product": g.Map{"id": 1, "price": 100}}
|
||||
p2 = g.MapStrAny{"product": g.Map{"id": 2, "price": 200}}
|
||||
v = gvar.New(g.ListStrAny{p1, p2})
|
||||
v2 = v.MapsDeep()
|
||||
)
|
||||
|
||||
fmt.Printf("%#v", v2)
|
||||
|
||||
// Output:
|
||||
// []map[string]interface {}{map[string]interface {}{"product":map[string]interface {}{"id":1, "price":100}}, map[string]interface {}{"product":map[string]interface {}{"id":2, "price":200}}}
|
||||
}
|
||||
|
||||
// MapToMap
|
||||
func ExampleVar_MapToMap() {
|
||||
var (
|
||||
m1 = gvar.New(g.MapIntInt{0: 100, 1: 200})
|
||||
m2 = g.MapStrStr{}
|
||||
)
|
||||
|
||||
err := m1.MapToMap(&m2)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
fmt.Printf("%#v", m2)
|
||||
|
||||
// Output:
|
||||
// map[string]string{"0":"100", "1":"200"}
|
||||
}
|
||||
|
||||
// MapToMaps
|
||||
func ExampleVar_MapToMaps() {
|
||||
var (
|
||||
p1 = g.MapStrAny{"product": g.Map{"id": 1, "price": 100}}
|
||||
p2 = g.MapStrAny{"product": g.Map{"id": 2, "price": 200}}
|
||||
v = gvar.New(g.ListStrAny{p1, p2})
|
||||
v2 []g.MapStrStr
|
||||
)
|
||||
|
||||
err := v.MapToMaps(&v2)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Printf("%#v", v2)
|
||||
|
||||
// Output:
|
||||
// []map[string]string{map[string]string{"product":"{\"id\":1,\"price\":100}"}, map[string]string{"product":"{\"id\":2,\"price\":200}"}}
|
||||
}
|
||||
|
||||
// MapToMapDeep
|
||||
func ExampleVar_MapToMapDeep() {
|
||||
var (
|
||||
p1 = gvar.New(g.MapStrAny{"product": g.Map{"id": 1, "price": 100}})
|
||||
p2 = g.MapStrAny{}
|
||||
)
|
||||
|
||||
err := p1.MapToMap(&p2)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Printf("%#v", p2)
|
||||
|
||||
// Output:
|
||||
// map[string]interface {}{"product":map[string]interface {}{"id":1, "price":100}}
|
||||
}
|
||||
|
||||
// Scan
|
||||
func ExampleVar_Scan() {
|
||||
type Student struct {
|
||||
Id *g.Var
|
||||
Name *g.Var
|
||||
Scores *g.Var
|
||||
}
|
||||
var (
|
||||
s Student
|
||||
m = g.Map{
|
||||
"Id": 1,
|
||||
"Name": "john",
|
||||
"Scores": []int{100, 99, 98},
|
||||
}
|
||||
)
|
||||
if err := gconv.Scan(m, &s); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
g.DumpWithType(s)
|
||||
|
||||
// Output:
|
||||
// gvar_test.Student(3) {
|
||||
// Id: *gvar.Var(1) "1",
|
||||
// Name: *gvar.Var(4) "john",
|
||||
// Scores: *gvar.Var(11) "[100,99,98]",
|
||||
// }
|
||||
}
|
@ -276,6 +276,7 @@ func doQuoteWord(s, charLeft, charRight string) string {
|
||||
|
||||
// doQuoteString quotes string with quote chars.
|
||||
// For example, if quote char is '`':
|
||||
// "null" => "NULL"
|
||||
// "user" => "`user`"
|
||||
// "user u" => "`user` u"
|
||||
// "user,user_detail" => "`user`,`user_detail`"
|
||||
@ -289,7 +290,11 @@ func doQuoteString(s, charLeft, charRight string) string {
|
||||
array2 := gstr.SplitAndTrim(v1, " ")
|
||||
array3 := gstr.Split(gstr.Trim(array2[0]), ".")
|
||||
if len(array3) == 1 {
|
||||
array3[0] = doQuoteWord(array3[0], charLeft, charRight)
|
||||
if strings.EqualFold(array3[0], "NULL") {
|
||||
array3[0] = doQuoteWord(array3[0], "", "")
|
||||
} else {
|
||||
array3[0] = doQuoteWord(array3[0], charLeft, charRight)
|
||||
}
|
||||
} else if len(array3) >= 2 {
|
||||
array3[0] = doQuoteWord(array3[0], charLeft, charRight)
|
||||
// Note:
|
||||
|
@ -1083,6 +1083,13 @@ func Test_Model_OrderBy(t *testing.T) {
|
||||
t.Assert(len(result), TableSize)
|
||||
t.Assert(result[0]["nickname"].String(), fmt.Sprintf("name_%d", TableSize))
|
||||
})
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
result, err := db.Model(table).Order("NULL").All()
|
||||
t.AssertNil(err)
|
||||
t.Assert(len(result), TableSize)
|
||||
t.Assert(result[0]["nickname"].String(), "name_1")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Model_GroupBy(t *testing.T) {
|
||||
|
27
frame/gins/gins_httpclient.go
Normal file
27
frame/gins/gins_httpclient.go
Normal file
@ -0,0 +1,27 @@
|
||||
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
|
||||
package gins
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
)
|
||||
|
||||
const (
|
||||
frameCoreComponentNameHttpClient = "gf.core.component.httpclient"
|
||||
)
|
||||
|
||||
// HttpClient returns an instance of http client with specified name.
|
||||
func HttpClient(name ...interface{}) *ghttp.Client {
|
||||
var (
|
||||
instanceKey = fmt.Sprintf("%s.%v", frameCoreComponentNameHttpClient, name)
|
||||
)
|
||||
return localInstances.GetOrSetFuncLock(instanceKey, func() interface{} {
|
||||
return ghttp.NewClient()
|
||||
}).(*ghttp.Client)
|
||||
}
|
30
frame/gins/gins_z_unit_httpclient_test.go
Normal file
30
frame/gins/gins_z_unit_httpclient_test.go
Normal file
@ -0,0 +1,30 @@
|
||||
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the MIT License.
|
||||
// If a copy of the MIT was not distributed with this file,
|
||||
// You can obtain one at https://github.com/gogf/gf.
|
||||
|
||||
package gins_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/gogf/gf/v2/frame/gins"
|
||||
"github.com/gogf/gf/v2/test/gtest"
|
||||
)
|
||||
|
||||
func Test_Client(t *testing.T) {
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var (
|
||||
c = gins.HttpClient()
|
||||
c1 = gins.HttpClient("c1")
|
||||
c2 = gins.HttpClient("c2")
|
||||
)
|
||||
c.SetAgent("test1")
|
||||
c.SetAgent("test2")
|
||||
t.AssertNE(fmt.Sprintf(`%p`, c), fmt.Sprintf(`%p`, c1))
|
||||
t.AssertNE(fmt.Sprintf(`%p`, c), fmt.Sprintf(`%p`, c2))
|
||||
t.AssertNE(fmt.Sprintf(`%p`, c1), fmt.Sprintf(`%p`, c2))
|
||||
})
|
||||
}
|
@ -32,7 +32,6 @@ import (
|
||||
type Client struct {
|
||||
http.Client // Underlying HTTP Client.
|
||||
dump bool // Mark this request will be dumped.
|
||||
parent *Client // Parent http client, this is used for chaining operations.
|
||||
header map[string]string // Custom header map.
|
||||
cookies map[string]string // Custom cookie map.
|
||||
prefix string // Prefix for request.
|
||||
|
@ -13,10 +13,7 @@ import (
|
||||
// Prefix is a chaining function,
|
||||
// which sets the URL prefix for next request of this client.
|
||||
func (c *Client) Prefix(prefix string) *Client {
|
||||
newClient := c
|
||||
if c.parent == nil {
|
||||
newClient = c.Clone()
|
||||
}
|
||||
newClient := c.Clone()
|
||||
newClient.SetPrefix(prefix)
|
||||
return newClient
|
||||
}
|
||||
@ -24,10 +21,7 @@ func (c *Client) Prefix(prefix string) *Client {
|
||||
// Header is a chaining function,
|
||||
// which sets custom HTTP headers with map for next request.
|
||||
func (c *Client) Header(m map[string]string) *Client {
|
||||
newClient := c
|
||||
if c.parent == nil {
|
||||
newClient = c.Clone()
|
||||
}
|
||||
newClient := c.Clone()
|
||||
newClient.SetHeaderMap(m)
|
||||
return newClient
|
||||
}
|
||||
@ -35,10 +29,7 @@ func (c *Client) Header(m map[string]string) *Client {
|
||||
// HeaderRaw is a chaining function,
|
||||
// which sets custom HTTP header using raw string for next request.
|
||||
func (c *Client) HeaderRaw(headers string) *Client {
|
||||
newClient := c
|
||||
if c.parent == nil {
|
||||
newClient = c.Clone()
|
||||
}
|
||||
newClient := c.Clone()
|
||||
newClient.SetHeaderRaw(headers)
|
||||
return newClient
|
||||
}
|
||||
@ -46,10 +37,7 @@ func (c *Client) HeaderRaw(headers string) *Client {
|
||||
// Cookie is a chaining function,
|
||||
// which sets cookie items with map for next request.
|
||||
func (c *Client) Cookie(m map[string]string) *Client {
|
||||
newClient := c
|
||||
if c.parent == nil {
|
||||
newClient = c.Clone()
|
||||
}
|
||||
newClient := c.Clone()
|
||||
newClient.SetCookieMap(m)
|
||||
return newClient
|
||||
}
|
||||
@ -57,10 +45,7 @@ func (c *Client) Cookie(m map[string]string) *Client {
|
||||
// ContentType is a chaining function,
|
||||
// which sets HTTP content type for the next request.
|
||||
func (c *Client) ContentType(contentType string) *Client {
|
||||
newClient := c
|
||||
if c.parent == nil {
|
||||
newClient = c.Clone()
|
||||
}
|
||||
newClient := c.Clone()
|
||||
newClient.SetContentType(contentType)
|
||||
return newClient
|
||||
}
|
||||
@ -70,10 +55,7 @@ func (c *Client) ContentType(contentType string) *Client {
|
||||
//
|
||||
// Note that it also checks and encodes the parameter to JSON format automatically.
|
||||
func (c *Client) ContentJson() *Client {
|
||||
newClient := c
|
||||
if c.parent == nil {
|
||||
newClient = c.Clone()
|
||||
}
|
||||
newClient := c.Clone()
|
||||
newClient.SetContentType("application/json")
|
||||
return newClient
|
||||
}
|
||||
@ -83,10 +65,7 @@ func (c *Client) ContentJson() *Client {
|
||||
//
|
||||
// Note that it also checks and encodes the parameter to XML format automatically.
|
||||
func (c *Client) ContentXml() *Client {
|
||||
newClient := c
|
||||
if c.parent == nil {
|
||||
newClient = c.Clone()
|
||||
}
|
||||
newClient := c.Clone()
|
||||
newClient.SetContentType("application/xml")
|
||||
return newClient
|
||||
}
|
||||
@ -94,10 +73,7 @@ func (c *Client) ContentXml() *Client {
|
||||
// Timeout is a chaining function,
|
||||
// which sets the timeout for next request.
|
||||
func (c *Client) Timeout(t time.Duration) *Client {
|
||||
newClient := c
|
||||
if c.parent == nil {
|
||||
newClient = c.Clone()
|
||||
}
|
||||
newClient := c.Clone()
|
||||
newClient.SetTimeout(t)
|
||||
return newClient
|
||||
}
|
||||
@ -105,10 +81,7 @@ func (c *Client) Timeout(t time.Duration) *Client {
|
||||
// BasicAuth is a chaining function,
|
||||
// which sets HTTP basic authentication information for next request.
|
||||
func (c *Client) BasicAuth(user, pass string) *Client {
|
||||
newClient := c
|
||||
if c.parent == nil {
|
||||
newClient = c.Clone()
|
||||
}
|
||||
newClient := c.Clone()
|
||||
newClient.SetBasicAuth(user, pass)
|
||||
return newClient
|
||||
}
|
||||
@ -116,10 +89,7 @@ func (c *Client) BasicAuth(user, pass string) *Client {
|
||||
// Retry is a chaining function,
|
||||
// which sets retry count and interval when failure for next request.
|
||||
func (c *Client) Retry(retryCount int, retryInterval time.Duration) *Client {
|
||||
newClient := c
|
||||
if c.parent == nil {
|
||||
newClient = c.Clone()
|
||||
}
|
||||
newClient := c.Clone()
|
||||
newClient.SetRetry(retryCount, retryInterval)
|
||||
return newClient
|
||||
}
|
||||
@ -127,10 +97,7 @@ func (c *Client) Retry(retryCount int, retryInterval time.Duration) *Client {
|
||||
// Dump is a chaining function,
|
||||
// which enables/disables dump feature for this request.
|
||||
func (c *Client) Dump(dump ...bool) *Client {
|
||||
newClient := c
|
||||
if c.parent == nil {
|
||||
newClient = c.Clone()
|
||||
}
|
||||
newClient := c.Clone()
|
||||
if len(dump) > 0 {
|
||||
newClient.SetDump(dump[0])
|
||||
} else {
|
||||
@ -145,10 +112,7 @@ func (c *Client) Dump(dump ...bool) *Client {
|
||||
// The correct pattern is like `http://USER:PASSWORD@IP:PORT` or `socks5://USER:PASSWORD@IP:PORT`.
|
||||
// Only `http` and `socks5` proxies are supported currently.
|
||||
func (c *Client) Proxy(proxyURL string) *Client {
|
||||
newClient := c
|
||||
if c.parent == nil {
|
||||
newClient = c.Clone()
|
||||
}
|
||||
newClient := c.Clone()
|
||||
newClient.SetProxy(proxyURL)
|
||||
return newClient
|
||||
}
|
||||
@ -156,10 +120,7 @@ func (c *Client) Proxy(proxyURL string) *Client {
|
||||
// RedirectLimit is a chaining function,
|
||||
// which sets the redirect limit the number of jumps for the request.
|
||||
func (c *Client) RedirectLimit(redirectLimit int) *Client {
|
||||
newClient := c
|
||||
if c.parent == nil {
|
||||
newClient = c.Clone()
|
||||
}
|
||||
newClient := c.Clone()
|
||||
newClient.SetRedirectLimit(redirectLimit)
|
||||
return newClient
|
||||
}
|
||||
|
@ -26,6 +26,7 @@ type Command struct {
|
||||
HelpFunc Function // Custom help function
|
||||
Examples string // Usage examples.
|
||||
Additional string // Additional info about this command, which will be appended to the end of help info.
|
||||
Strict bool // Strict parsing options, which means it returns error if invalid option given.
|
||||
parent *Command // Parent command for internal usage.
|
||||
commands []Command // Sub commands of this command.
|
||||
}
|
||||
|
@ -97,7 +97,7 @@ func (c *Command) reParse(ctx context.Context, parser *Parser) (*Parser, error)
|
||||
}
|
||||
supportedOptions[optionKey] = !option.Orphan
|
||||
}
|
||||
return Parse(supportedOptions)
|
||||
return Parse(supportedOptions, c.Strict)
|
||||
}
|
||||
|
||||
// searchCommand recursively searches the command according given arguments.
|
||||
|
@ -48,10 +48,6 @@ func StructType(object interface{}) (*Type, error) {
|
||||
}
|
||||
|
||||
exitLoop:
|
||||
for reflectKind == reflect.Ptr {
|
||||
reflectValue = reflectValue.Elem()
|
||||
reflectKind = reflectValue.Kind()
|
||||
}
|
||||
if reflectKind != reflect.Struct {
|
||||
return nil, errors.New(
|
||||
fmt.Sprintf(
|
||||
|
@ -78,12 +78,7 @@ func (oai *OpenApiV3) addPath(in addPathInput) error {
|
||||
} else {
|
||||
outputObject = reflect.New(reflectType.Out(0)).Elem()
|
||||
}
|
||||
for inputObject.Kind() == reflect.Ptr {
|
||||
inputObject = inputObject.Elem()
|
||||
}
|
||||
for outputObject.Kind() == reflect.Ptr {
|
||||
outputObject = outputObject.Elem()
|
||||
}
|
||||
|
||||
var (
|
||||
path = Path{}
|
||||
inputMetaMap = gmeta.Data(inputObject.Interface())
|
||||
|
Loading…
Reference in New Issue
Block a user