Merge branch 'develop'

This commit is contained in:
John
2020-06-16 20:42:26 +08:00
44 changed files with 501 additions and 390 deletions

View File

@ -265,25 +265,25 @@ func (m *AnyAnyMap) GetOrSetFuncLock(key interface{}, f func() interface{}) inte
// GetVar returns a Var with the value by given <key>.
// The returned Var is un-concurrent safe.
func (m *AnyAnyMap) GetVar(key interface{}) *gvar.Var {
func (m *AnyAnyMap) GetVar(key interface{}) gvar.Var {
return gvar.New(m.Get(key))
}
// GetVarOrSet returns a Var with result from GetVarOrSet.
// The returned Var is un-concurrent safe.
func (m *AnyAnyMap) GetVarOrSet(key interface{}, value interface{}) *gvar.Var {
func (m *AnyAnyMap) GetVarOrSet(key interface{}, value interface{}) gvar.Var {
return gvar.New(m.GetOrSet(key, value))
}
// GetVarOrSetFunc returns a Var with result from GetOrSetFunc.
// The returned Var is un-concurrent safe.
func (m *AnyAnyMap) GetVarOrSetFunc(key interface{}, f func() interface{}) *gvar.Var {
func (m *AnyAnyMap) GetVarOrSetFunc(key interface{}, f func() interface{}) gvar.Var {
return gvar.New(m.GetOrSetFunc(key, f))
}
// GetVarOrSetFuncLock returns a Var with result from GetOrSetFuncLock.
// The returned Var is un-concurrent safe.
func (m *AnyAnyMap) GetVarOrSetFuncLock(key interface{}, f func() interface{}) *gvar.Var {
func (m *AnyAnyMap) GetVarOrSetFuncLock(key interface{}, f func() interface{}) gvar.Var {
return gvar.New(m.GetOrSetFuncLock(key, f))
}

View File

@ -263,25 +263,25 @@ func (m *IntAnyMap) GetOrSetFuncLock(key int, f func() interface{}) interface{}
// GetVar returns a Var with the value by given <key>.
// The returned Var is un-concurrent safe.
func (m *IntAnyMap) GetVar(key int) *gvar.Var {
func (m *IntAnyMap) GetVar(key int) gvar.Var {
return gvar.New(m.Get(key))
}
// GetVarOrSet returns a Var with result from GetVarOrSet.
// The returned Var is un-concurrent safe.
func (m *IntAnyMap) GetVarOrSet(key int, value interface{}) *gvar.Var {
func (m *IntAnyMap) GetVarOrSet(key int, value interface{}) gvar.Var {
return gvar.New(m.GetOrSet(key, value))
}
// GetVarOrSetFunc returns a Var with result from GetOrSetFunc.
// The returned Var is un-concurrent safe.
func (m *IntAnyMap) GetVarOrSetFunc(key int, f func() interface{}) *gvar.Var {
func (m *IntAnyMap) GetVarOrSetFunc(key int, f func() interface{}) gvar.Var {
return gvar.New(m.GetOrSetFunc(key, f))
}
// GetVarOrSetFuncLock returns a Var with result from GetOrSetFuncLock.
// The returned Var is un-concurrent safe.
func (m *IntAnyMap) GetVarOrSetFuncLock(key int, f func() interface{}) *gvar.Var {
func (m *IntAnyMap) GetVarOrSetFuncLock(key int, f func() interface{}) gvar.Var {
return gvar.New(m.GetOrSetFuncLock(key, f))
}

View File

@ -259,25 +259,25 @@ func (m *StrAnyMap) GetOrSetFuncLock(key string, f func() interface{}) interface
// GetVar returns a Var with the value by given <key>.
// The returned Var is un-concurrent safe.
func (m *StrAnyMap) GetVar(key string) *gvar.Var {
func (m *StrAnyMap) GetVar(key string) gvar.Var {
return gvar.New(m.Get(key))
}
// GetVarOrSet returns a Var with result from GetVarOrSet.
// The returned Var is un-concurrent safe.
func (m *StrAnyMap) GetVarOrSet(key string, value interface{}) *gvar.Var {
func (m *StrAnyMap) GetVarOrSet(key string, value interface{}) gvar.Var {
return gvar.New(m.GetOrSet(key, value))
}
// GetVarOrSetFunc returns a Var with result from GetOrSetFunc.
// The returned Var is un-concurrent safe.
func (m *StrAnyMap) GetVarOrSetFunc(key string, f func() interface{}) *gvar.Var {
func (m *StrAnyMap) GetVarOrSetFunc(key string, f func() interface{}) gvar.Var {
return gvar.New(m.GetOrSetFunc(key, f))
}
// GetVarOrSetFuncLock returns a Var with result from GetOrSetFuncLock.
// The returned Var is un-concurrent safe.
func (m *StrAnyMap) GetVarOrSetFuncLock(key string, f func() interface{}) *gvar.Var {
func (m *StrAnyMap) GetVarOrSetFuncLock(key string, f func() interface{}) gvar.Var {
return gvar.New(m.GetOrSetFuncLock(key, f))
}

View File

@ -332,25 +332,25 @@ func (m *ListMap) GetOrSetFuncLock(key interface{}, f func() interface{}) interf
// GetVar returns a Var with the value by given <key>.
// The returned Var is un-concurrent safe.
func (m *ListMap) GetVar(key interface{}) *gvar.Var {
func (m *ListMap) GetVar(key interface{}) gvar.Var {
return gvar.New(m.Get(key))
}
// GetVarOrSet returns a Var with result from GetVarOrSet.
// The returned Var is un-concurrent safe.
func (m *ListMap) GetVarOrSet(key interface{}, value interface{}) *gvar.Var {
func (m *ListMap) GetVarOrSet(key interface{}, value interface{}) gvar.Var {
return gvar.New(m.GetOrSet(key, value))
}
// GetVarOrSetFunc returns a Var with result from GetOrSetFunc.
// The returned Var is un-concurrent safe.
func (m *ListMap) GetVarOrSetFunc(key interface{}, f func() interface{}) *gvar.Var {
func (m *ListMap) GetVarOrSetFunc(key interface{}, f func() interface{}) gvar.Var {
return gvar.New(m.GetOrSetFunc(key, f))
}
// GetVarOrSetFuncLock returns a Var with result from GetOrSetFuncLock.
// The returned Var is un-concurrent safe.
func (m *ListMap) GetVarOrSetFuncLock(key interface{}, f func() interface{}) *gvar.Var {
func (m *ListMap) GetVarOrSetFuncLock(key interface{}, f func() interface{}) gvar.Var {
return gvar.New(m.GetOrSetFuncLock(key, f))
}

View File

@ -173,25 +173,25 @@ func (tree *AVLTree) GetOrSetFuncLock(key interface{}, f func() interface{}) int
// GetVar returns a gvar.Var with the value by given <key>.
// The returned gvar.Var is un-concurrent safe.
func (tree *AVLTree) GetVar(key interface{}) *gvar.Var {
func (tree *AVLTree) GetVar(key interface{}) gvar.Var {
return gvar.New(tree.Get(key))
}
// GetVarOrSet returns a gvar.Var with result from GetVarOrSet.
// The returned gvar.Var is un-concurrent safe.
func (tree *AVLTree) GetVarOrSet(key interface{}, value interface{}) *gvar.Var {
func (tree *AVLTree) GetVarOrSet(key interface{}, value interface{}) gvar.Var {
return gvar.New(tree.GetOrSet(key, value))
}
// GetVarOrSetFunc returns a gvar.Var with result from GetOrSetFunc.
// The returned gvar.Var is un-concurrent safe.
func (tree *AVLTree) GetVarOrSetFunc(key interface{}, f func() interface{}) *gvar.Var {
func (tree *AVLTree) GetVarOrSetFunc(key interface{}, f func() interface{}) gvar.Var {
return gvar.New(tree.GetOrSetFunc(key, f))
}
// GetVarOrSetFuncLock returns a gvar.Var with result from GetOrSetFuncLock.
// The returned gvar.Var is un-concurrent safe.
func (tree *AVLTree) GetVarOrSetFuncLock(key interface{}, f func() interface{}) *gvar.Var {
func (tree *AVLTree) GetVarOrSetFuncLock(key interface{}, f func() interface{}) gvar.Var {
return gvar.New(tree.GetOrSetFuncLock(key, f))
}

View File

@ -171,25 +171,25 @@ func (tree *BTree) GetOrSetFuncLock(key interface{}, f func() interface{}) inter
// GetVar returns a gvar.Var with the value by given <key>.
// The returned gvar.Var is un-concurrent safe.
func (tree *BTree) GetVar(key interface{}) *gvar.Var {
func (tree *BTree) GetVar(key interface{}) gvar.Var {
return gvar.New(tree.Get(key))
}
// GetVarOrSet returns a gvar.Var with result from GetVarOrSet.
// The returned gvar.Var is un-concurrent safe.
func (tree *BTree) GetVarOrSet(key interface{}, value interface{}) *gvar.Var {
func (tree *BTree) GetVarOrSet(key interface{}, value interface{}) gvar.Var {
return gvar.New(tree.GetOrSet(key, value))
}
// GetVarOrSetFunc returns a gvar.Var with result from GetOrSetFunc.
// The returned gvar.Var is un-concurrent safe.
func (tree *BTree) GetVarOrSetFunc(key interface{}, f func() interface{}) *gvar.Var {
func (tree *BTree) GetVarOrSetFunc(key interface{}, f func() interface{}) gvar.Var {
return gvar.New(tree.GetOrSetFunc(key, f))
}
// GetVarOrSetFuncLock returns a gvar.Var with result from GetOrSetFuncLock.
// The returned gvar.Var is un-concurrent safe.
func (tree *BTree) GetVarOrSetFuncLock(key interface{}, f func() interface{}) *gvar.Var {
func (tree *BTree) GetVarOrSetFuncLock(key interface{}, f func() interface{}) gvar.Var {
return gvar.New(tree.GetOrSetFuncLock(key, f))
}

View File

@ -213,25 +213,25 @@ func (tree *RedBlackTree) GetOrSetFuncLock(key interface{}, f func() interface{}
// GetVar returns a gvar.Var with the value by given <key>.
// The returned gvar.Var is un-concurrent safe.
func (tree *RedBlackTree) GetVar(key interface{}) *gvar.Var {
func (tree *RedBlackTree) GetVar(key interface{}) gvar.Var {
return gvar.New(tree.Get(key))
}
// GetVarOrSet returns a gvar.Var with result from GetVarOrSet.
// The returned gvar.Var is un-concurrent safe.
func (tree *RedBlackTree) GetVarOrSet(key interface{}, value interface{}) *gvar.Var {
func (tree *RedBlackTree) GetVarOrSet(key interface{}, value interface{}) gvar.Var {
return gvar.New(tree.GetOrSet(key, value))
}
// GetVarOrSetFunc returns a gvar.Var with result from GetOrSetFunc.
// The returned gvar.Var is un-concurrent safe.
func (tree *RedBlackTree) GetVarOrSetFunc(key interface{}, f func() interface{}) *gvar.Var {
func (tree *RedBlackTree) GetVarOrSetFunc(key interface{}, f func() interface{}) gvar.Var {
return gvar.New(tree.GetOrSetFunc(key, f))
}
// GetVarOrSetFuncLock returns a gvar.Var with result from GetOrSetFuncLock.
// The returned gvar.Var is un-concurrent safe.
func (tree *RedBlackTree) GetVarOrSetFuncLock(key interface{}, f func() interface{}) *gvar.Var {
func (tree *RedBlackTree) GetVarOrSetFuncLock(key interface{}, f func() interface{}) gvar.Var {
return gvar.New(tree.GetOrSetFuncLock(key, f))
}

View File

@ -8,251 +8,75 @@
package gvar
import (
"encoding/json"
"time"
"github.com/gogf/gf/internal/empty"
"github.com/gogf/gf/container/gtype"
"github.com/gogf/gf/internal/intstore"
"github.com/gogf/gf/os/gtime"
"github.com/gogf/gf/util/gconv"
"reflect"
"time"
)
// Var is an universal variable type.
type Var struct {
value interface{} // Underlying value.
safe bool // Concurrent safe or not.
// Var is a universal variable interface, like generics.
type Var interface {
IsEmpty() bool
IsNil() bool
Val() interface{}
Set(value interface{}) (old interface{})
Interface() interface{}
String() string
Bool() bool
Int() int
Int16() int16
Int32() int32
Int64() int64
Int8() int8
Uint() uint
Uint16() uint16
Uint32() uint32
Uint64() uint64
Uint8() uint8
Float32() float32
Float64() float64
Ints() []int
Uints() []uint
Vars() []Var
Bytes() []byte
Floats() []float64
Array() []interface{}
Slice() []interface{}
Strings() []string
Interfaces() []interface{}
Map(tags ...string) map[string]interface{}
MapDeep(tags ...string) map[string]interface{}
MapStrStr(tags ...string) map[string]string
MapStrStrDeep(tags ...string) map[string]string
MapStrVar(tags ...string) map[string]Var
MapStrVarDeep(tags ...string) map[string]Var
MapToMap(pointer interface{}, mapping ...map[string]string) (err error)
MapToMapDeep(pointer interface{}, mapping ...map[string]string) (err error)
MapToMaps(pointer interface{}, mapping ...map[string]string) (err error)
MapToMapsDeep(pointer interface{}, mapping ...map[string]string) (err error)
Struct(pointer interface{}, mapping ...map[string]string) error
StructDeep(pointer interface{}, mapping ...map[string]string) error
Structs(pointer interface{}, mapping ...map[string]string) (err error)
StructsDeep(pointer interface{}, mapping ...map[string]string) (err error)
Time(format ...string) time.Time
Duration() time.Duration
GTime(format ...string) *gtime.Time
MarshalJSON() ([]byte, error)
UnmarshalJSON(b []byte) error
UnmarshalValue(value interface{}) error
}
// New creates and returns a new *Var with given <value>.
// The optional parameter <safe> specifies whether Var is used in concurrent-safety,
// which is false in default.
func New(value interface{}, safe ...bool) *Var {
v := Create(value, safe...)
return &v
}
// Create creates and returns a new Var with given <value>.
// The optional parameter <safe> specifies whether Var is used in concurrent-safety,
// which is false in default.
func Create(value interface{}, safe ...bool) Var {
v := Var{}
if len(safe) > 0 && !safe[0] {
v.safe = true
v.value = gtype.NewInterface(value)
} else {
v.value = value
}
return v
}
// Clone does a shallow copy of current Var and returns a pointer to this Var.
func (v *Var) Clone() *Var {
return New(v.Val(), v.safe)
}
// Set sets <value> to <v>, and returns the old value.
func (v *Var) Set(value interface{}) (old interface{}) {
if v.safe {
if t, ok := v.value.(*gtype.Interface); ok {
old = t.Set(value)
return
}
}
old = v.value
v.value = value
return
}
// Val returns the current value of <v>.
func (v *Var) Val() interface{} {
if v == nil {
return nil
}
if v.safe {
if t, ok := v.value.(*gtype.Interface); ok {
return t.Val()
}
}
return v.value
}
// Interface is alias of Val.
func (v *Var) Interface() interface{} {
return v.Val()
}
// IsNil checks whether <v> is nil.
func (v *Var) IsNil() bool {
return v.Val() == nil
}
// IsEmpty checks whether <v> is empty.
func (v *Var) IsEmpty() bool {
return empty.IsEmpty(v.Val())
}
// Bytes converts and returns <v> as []byte.
func (v *Var) Bytes() []byte {
return gconv.Bytes(v.Val())
}
// String converts and returns <v> as string.
func (v *Var) String() string {
return gconv.String(v.Val())
}
// Bool converts and returns <v> as bool.
func (v *Var) Bool() bool {
return gconv.Bool(v.Val())
}
// Int converts and returns <v> as int.
func (v *Var) Int() int {
return gconv.Int(v.Val())
}
// Ints converts and returns <v> as []int.
func (v *Var) Ints() []int {
return gconv.Ints(v.Val())
}
// Int8 converts and returns <v> as int8.
func (v *Var) Int8() int8 {
return gconv.Int8(v.Val())
}
// Int16 converts and returns <v> as int16.
func (v *Var) Int16() int16 {
return gconv.Int16(v.Val())
}
// Int32 converts and returns <v> as int32.
func (v *Var) Int32() int32 {
return gconv.Int32(v.Val())
}
// Int64 converts and returns <v> as int64.
func (v *Var) Int64() int64 {
return gconv.Int64(v.Val())
}
// Uint converts and returns <v> as uint.
func (v *Var) Uint() uint {
return gconv.Uint(v.Val())
}
// Uints converts and returns <v> as []uint.
func (v *Var) Uints() []uint {
return gconv.Uints(v.Val())
}
// Uint8 converts and returns <v> as uint8.
func (v *Var) Uint8() uint8 {
return gconv.Uint8(v.Val())
}
// Uint16 converts and returns <v> as uint16.
func (v *Var) Uint16() uint16 {
return gconv.Uint16(v.Val())
}
// Uint32 converts and returns <v> as uint32.
func (v *Var) Uint32() uint32 {
return gconv.Uint32(v.Val())
}
// Uint64 converts and returns <v> as uint64.
func (v *Var) Uint64() uint64 {
return gconv.Uint64(v.Val())
}
// Float32 converts and returns <v> as float32.
func (v *Var) Float32() float32 {
return gconv.Float32(v.Val())
}
// Float64 converts and returns <v> as float64.
func (v *Var) Float64() float64 {
return gconv.Float64(v.Val())
}
// Floats converts and returns <v> as []float64.
func (v *Var) Floats() []float64 {
return gconv.Floats(v.Val())
}
// Strings converts and returns <v> as []string.
func (v *Var) Strings() []string {
return gconv.Strings(v.Val())
}
// Interfaces converts and returns <v> as []interfaces{}.
func (v *Var) Interfaces() []interface{} {
return gconv.Interfaces(v.Val())
}
// Slice is alias of Interfaces.
func (v *Var) Slice() []interface{} {
return v.Interfaces()
}
// Array is alias of Interfaces.
func (v *Var) Array() []interface{} {
return v.Interfaces()
}
// Vars converts and returns <v> as []*Var.
func (v *Var) Vars() []*Var {
array := gconv.Interfaces(v.Val())
if len(array) == 0 {
return nil
}
vars := make([]*Var, len(array))
for k, v := range array {
vars[k] = New(v)
}
return vars
}
// Time converts and returns <v> as time.Time.
// The parameter <format> specifies the format of the time string using gtime,
// eg: Y-m-d H:i:s.
func (v *Var) Time(format ...string) time.Time {
return gconv.Time(v.Val(), format...)
}
// Duration converts and returns <v> as time.Duration.
// If value of <v> is string, then it uses time.ParseDuration for conversion.
func (v *Var) Duration() time.Duration {
return gconv.Duration(v.Val())
}
// GTime converts and returns <v> as *gtime.Time.
// The parameter <format> specifies the format of the time string using gtime,
// eg: Y-m-d H:i:s.
func (v *Var) GTime(format ...string) *gtime.Time {
return gconv.GTime(v.Val(), format...)
}
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
func (v *Var) MarshalJSON() ([]byte, error) {
return json.Marshal(v.Val())
}
// UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.
func (v *Var) UnmarshalJSON(b []byte) error {
var i interface{}
err := json.Unmarshal(b, &i)
if err != nil {
return err
}
v.Set(i)
return nil
}
// UnmarshalValue is an interface implement which sets any type of value for Var.
func (v *Var) UnmarshalValue(value interface{}) error {
v.Set(value)
return nil
func init() {
// Register the type of gvar.VarImp to local variable.
intstore.ReflectTypeVarImp = reflect.TypeOf(VarImp{})
}

264
container/gvar/gvar_imp.go Normal file
View File

@ -0,0 +1,264 @@
// Copyright 2018-2019 gf Author(https://github.com/gogf/gf). 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
import (
"encoding/json"
"time"
"github.com/gogf/gf/internal/empty"
"github.com/gogf/gf/container/gtype"
"github.com/gogf/gf/os/gtime"
"github.com/gogf/gf/util/gconv"
)
// VarImp is an universal variable type implementer.
type VarImp struct {
value interface{} // Underlying value.
safe bool // Concurrent safe or not.
}
// New creates and returns a new Var with given <value>.
// The optional parameter <safe> specifies whether Var is used in concurrent-safety,
// which is false in default.
func New(value interface{}, safe ...bool) Var {
v := VarImp{}
if len(safe) > 0 && !safe[0] {
v.safe = true
v.value = gtype.NewInterface(value)
} else {
v.value = value
}
return &v
}
// Create creates and returns a new VarImp with given <value>.
// The optional parameter <safe> specifies whether Var is used in concurrent-safety,
// which is false in default.
// Deprecated.
func Create(value interface{}, safe ...bool) VarImp {
v := VarImp{}
if len(safe) > 0 && !safe[0] {
v.safe = true
v.value = gtype.NewInterface(value)
} else {
v.value = value
}
return v
}
// Clone does a shallow copy of current Var and returns a pointer to this Var.
func (v *VarImp) Clone() Var {
return New(v.Val(), v.safe)
}
// Set sets <value> to <v>, and returns the old value.
func (v *VarImp) Set(value interface{}) (old interface{}) {
if v.safe {
if t, ok := v.value.(*gtype.Interface); ok {
old = t.Set(value)
return
}
}
old = v.value
v.value = value
return
}
// Val returns the current value of <v>.
func (v *VarImp) Val() interface{} {
if v == nil {
return nil
}
if v.safe {
if t, ok := v.value.(*gtype.Interface); ok {
return t.Val()
}
}
return v.value
}
// Interface is alias of Val.
func (v *VarImp) Interface() interface{} {
return v.Val()
}
// IsNil checks whether <v> is nil.
func (v *VarImp) IsNil() bool {
return v.Val() == nil
}
// IsEmpty checks whether <v> is empty.
func (v *VarImp) IsEmpty() bool {
return empty.IsEmpty(v.Val())
}
// Bytes converts and returns <v> as []byte.
func (v *VarImp) Bytes() []byte {
return gconv.Bytes(v.Val())
}
// String converts and returns <v> as string.
func (v *VarImp) String() string {
return gconv.String(v.Val())
}
// Bool converts and returns <v> as bool.
func (v *VarImp) Bool() bool {
return gconv.Bool(v.Val())
}
// Int converts and returns <v> as int.
func (v *VarImp) Int() int {
return gconv.Int(v.Val())
}
// Ints converts and returns <v> as []int.
func (v *VarImp) Ints() []int {
return gconv.Ints(v.Val())
}
// Int8 converts and returns <v> as int8.
func (v *VarImp) Int8() int8 {
return gconv.Int8(v.Val())
}
// Int16 converts and returns <v> as int16.
func (v *VarImp) Int16() int16 {
return gconv.Int16(v.Val())
}
// Int32 converts and returns <v> as int32.
func (v *VarImp) Int32() int32 {
return gconv.Int32(v.Val())
}
// Int64 converts and returns <v> as int64.
func (v *VarImp) Int64() int64 {
return gconv.Int64(v.Val())
}
// Uint converts and returns <v> as uint.
func (v *VarImp) Uint() uint {
return gconv.Uint(v.Val())
}
// Uints converts and returns <v> as []uint.
func (v *VarImp) Uints() []uint {
return gconv.Uints(v.Val())
}
// Uint8 converts and returns <v> as uint8.
func (v *VarImp) Uint8() uint8 {
return gconv.Uint8(v.Val())
}
// Uint16 converts and returns <v> as uint16.
func (v *VarImp) Uint16() uint16 {
return gconv.Uint16(v.Val())
}
// Uint32 converts and returns <v> as uint32.
func (v *VarImp) Uint32() uint32 {
return gconv.Uint32(v.Val())
}
// Uint64 converts and returns <v> as uint64.
func (v *VarImp) Uint64() uint64 {
return gconv.Uint64(v.Val())
}
// Float32 converts and returns <v> as float32.
func (v *VarImp) Float32() float32 {
return gconv.Float32(v.Val())
}
// Float64 converts and returns <v> as float64.
func (v *VarImp) Float64() float64 {
return gconv.Float64(v.Val())
}
// Floats converts and returns <v> as []float64.
func (v *VarImp) Floats() []float64 {
return gconv.Floats(v.Val())
}
// Strings converts and returns <v> as []string.
func (v *VarImp) Strings() []string {
return gconv.Strings(v.Val())
}
// Interfaces converts and returns <v> as []interfaces{}.
func (v *VarImp) Interfaces() []interface{} {
return gconv.Interfaces(v.Val())
}
// Slice is alias of Interfaces.
func (v *VarImp) Slice() []interface{} {
return v.Interfaces()
}
// Array is alias of Interfaces.
func (v *VarImp) Array() []interface{} {
return v.Interfaces()
}
// Vars converts and returns <v> as []Var.
func (v *VarImp) Vars() []Var {
array := gconv.Interfaces(v.Val())
if len(array) == 0 {
return nil
}
vars := make([]Var, len(array))
for k, v := range array {
vars[k] = New(v)
}
return vars
}
// Time converts and returns <v> as time.Time.
// The parameter <format> specifies the format of the time string using gtime,
// eg: Y-m-d H:i:s.
func (v *VarImp) Time(format ...string) time.Time {
return gconv.Time(v.Val(), format...)
}
// Duration converts and returns <v> as time.Duration.
// If value of <v> is string, then it uses time.ParseDuration for conversion.
func (v *VarImp) Duration() time.Duration {
return gconv.Duration(v.Val())
}
// GTime converts and returns <v> as *gtime.Time.
// The parameter <format> specifies the format of the time string using gtime,
// eg: Y-m-d H:i:s.
func (v *VarImp) GTime(format ...string) *gtime.Time {
return gconv.GTime(v.Val(), format...)
}
// MarshalJSON implements the interface MarshalJSON for json.Marshal.
func (v *VarImp) MarshalJSON() ([]byte, error) {
return json.Marshal(v.Val())
}
// UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.
func (v *VarImp) UnmarshalJSON(b []byte) error {
var i interface{}
err := json.Unmarshal(b, &i)
if err != nil {
return err
}
v.Set(i)
return nil
}
// UnmarshalValue is an interface implement which sets any type of value for Var.
func (v *VarImp) UnmarshalValue(value interface{}) error {
v.Set(value)
return nil
}

View File

@ -9,20 +9,20 @@ package gvar
import "github.com/gogf/gf/util/gconv"
// Map converts and returns <v> as map[string]interface{}.
func (v *Var) Map(tags ...string) map[string]interface{} {
func (v *VarImp) Map(tags ...string) map[string]interface{} {
return gconv.Map(v.Val(), tags...)
}
// MapStrStr converts and returns <v> as map[string]string.
func (v *Var) MapStrStr(tags ...string) map[string]string {
func (v *VarImp) MapStrStr(tags ...string) map[string]string {
return gconv.MapStrStr(v.Val(), tags...)
}
// MapStrVar converts and returns <v> as map[string]*Var.
func (v *Var) MapStrVar(tags ...string) map[string]*Var {
// MapStrVar converts and returns <v> as map[string]Var.
func (v *VarImp) MapStrVar(tags ...string) map[string]Var {
m := v.Map(tags...)
if len(m) > 0 {
vMap := make(map[string]*Var, len(m))
vMap := make(map[string]Var, len(m))
for k, v := range m {
vMap[k] = New(v)
}
@ -32,20 +32,20 @@ func (v *Var) MapStrVar(tags ...string) map[string]*Var {
}
// MapDeep converts and returns <v> as map[string]interface{} recursively.
func (v *Var) MapDeep(tags ...string) map[string]interface{} {
func (v *VarImp) MapDeep(tags ...string) map[string]interface{} {
return gconv.MapDeep(v.Val(), tags...)
}
// MapDeep converts and returns <v> as map[string]string recursively.
func (v *Var) MapStrStrDeep(tags ...string) map[string]string {
func (v *VarImp) MapStrStrDeep(tags ...string) map[string]string {
return gconv.MapStrStrDeep(v.Val(), tags...)
}
// MapStrVarDeep converts and returns <v> as map[string]*Var recursively.
func (v *Var) MapStrVarDeep(tags ...string) map[string]*Var {
// MapStrVarDeep converts and returns <v> as map[string]*VarImp recursively.
func (v *VarImp) MapStrVarDeep(tags ...string) map[string]Var {
m := v.MapDeep(tags...)
if len(m) > 0 {
vMap := make(map[string]*Var, len(m))
vMap := make(map[string]Var, len(m))
for k, v := range m {
vMap[k] = New(v)
}
@ -56,32 +56,32 @@ func (v *Var) MapStrVarDeep(tags ...string) map[string]*Var {
// Maps converts and returns <v> as map[string]string.
// See gconv.Maps.
func (v *Var) Maps(tags ...string) []map[string]interface{} {
func (v *VarImp) Maps(tags ...string) []map[string]interface{} {
return gconv.Maps(v.Val(), tags...)
}
// MapToMap converts any map type variable <params> to another map type variable <pointer>.
// See gconv.MapToMap.
func (v *Var) MapToMap(pointer interface{}) (err error) {
return gconv.MapToMap(v.Val(), pointer)
func (v *VarImp) MapToMap(pointer interface{}, mapping ...map[string]string) (err error) {
return gconv.MapToMap(v.Val(), pointer, mapping...)
}
// MapToMapDeep converts any map type variable <params> to another map type variable
// <pointer> recursively.
// See gconv.MapToMapDeep.
func (v *Var) MapToMapDeep(pointer interface{}) (err error) {
return gconv.MapToMapDeep(v.Val(), pointer)
func (v *VarImp) MapToMapDeep(pointer interface{}, mapping ...map[string]string) (err error) {
return gconv.MapToMapDeep(v.Val(), pointer, mapping...)
}
// MapToMaps converts any map type variable <params> to another map type variable <pointer>.
// See gconv.MapToMaps.
func (v *Var) MapToMaps(pointer interface{}, mapping ...map[string]string) (err error) {
func (v *VarImp) MapToMaps(pointer interface{}, mapping ...map[string]string) (err error) {
return gconv.MapToMaps(v.Val(), pointer, mapping...)
}
// MapToMapsDeep converts any map type variable <params> to another map type variable
// <pointer> recursively.
// See gconv.MapToMapsDeep.
func (v *Var) MapToMapsDeep(pointer interface{}, mapping ...map[string]string) (err error) {
func (v *VarImp) MapToMapsDeep(pointer interface{}, mapping ...map[string]string) (err error) {
return gconv.MapToMapsDeep(v.Val(), pointer, mapping...)
}

View File

@ -13,24 +13,24 @@ import (
// Struct maps value of <v> to <pointer>.
// The parameter <pointer> should be a pointer to a struct instance.
// The parameter <mapping> is used to specify the key-to-attribute mapping rules.
func (v *Var) Struct(pointer interface{}, mapping ...map[string]string) error {
func (v *VarImp) Struct(pointer interface{}, mapping ...map[string]string) error {
return gconv.Struct(v.Val(), pointer, mapping...)
}
// Struct maps value of <v> to <pointer> recursively.
// The parameter <pointer> should be a pointer to a struct instance.
// The parameter <mapping> is used to specify the key-to-attribute mapping rules.
func (v *Var) StructDeep(pointer interface{}, mapping ...map[string]string) error {
func (v *VarImp) StructDeep(pointer interface{}, mapping ...map[string]string) error {
return gconv.StructDeep(v.Val(), pointer, mapping...)
}
// Structs converts and returns <v> as given struct slice.
func (v *Var) Structs(pointer interface{}, mapping ...map[string]string) error {
func (v *VarImp) Structs(pointer interface{}, mapping ...map[string]string) error {
return gconv.Structs(v.Val(), pointer, mapping...)
}
// StructsDeep converts and returns <v> as given struct slice recursively.
func (v *Var) StructsDeep(pointer interface{}, mapping ...map[string]string) error {
func (v *VarImp) StructsDeep(pointer interface{}, mapping ...map[string]string) error {
return gconv.StructsDeep(v.Val(), pointer, mapping...)
}
@ -38,7 +38,7 @@ func (v *Var) StructsDeep(pointer interface{}, mapping ...map[string]string) err
// <pointer> to implement the converting.
// It calls function Struct if <pointer> is type of *struct/**struct to do the converting.
// It calls function Structs if <pointer> is type of *[]struct/*[]*struct to do the converting.
func (v *Var) Scan(pointer interface{}, mapping ...map[string]string) error {
func (v *VarImp) Scan(pointer interface{}, mapping ...map[string]string) error {
return gconv.Scan(v.Val(), pointer, mapping...)
}
@ -46,6 +46,6 @@ func (v *Var) Scan(pointer interface{}, mapping ...map[string]string) error {
// parameter <pointer> to implement the converting.
// It calls function StructDeep if <pointer> is type of *struct/**struct to do the converting.
// It calls function StructsDeep if <pointer> is type of *[]struct/*[]*struct to do the converting.
func (v *Var) ScanDeep(pointer interface{}, mapping ...map[string]string) error {
func (v *VarImp) ScanDeep(pointer interface{}, mapping ...map[string]string) error {
return gconv.ScanDeep(v.Val(), pointer, mapping...)
}

View File

@ -19,12 +19,12 @@ import (
func Test_Set(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var v gvar.Var
var v gvar.VarImp
v.Set(123.456)
t.Assert(v.Val(), 123.456)
})
gtest.C(t, func(t *gtest.T) {
var v gvar.Var
var v gvar.VarImp
v.Set(123.456)
t.Assert(v.Val(), 123.456)
})
@ -304,7 +304,7 @@ func Test_Duration(t *testing.T) {
func Test_UnmarshalValue(t *testing.T) {
type V struct {
Name string
Var *gvar.Var
Var gvar.Var
}
gtest.C(t, func(t *gtest.T) {
var v *V

View File

@ -47,7 +47,7 @@ func Test_Json(t *testing.T) {
})
gtest.C(t, func(t *gtest.T) {
var v gvar.Var
var v gvar.VarImp
s := "i love gf"
b, err := json.Marshal(s)
t.Assert(err, nil)

View File

@ -173,7 +173,7 @@ type Link interface {
type (
// Value is the field value type.
Value = *gvar.Var
Value = gvar.Var
// Record is the row record of the table.
Record map[string]Value

View File

@ -244,12 +244,12 @@ func (c *Core) GetScan(pointer interface{}, sql string, args ...interface{}) err
func (c *Core) GetValue(sql string, args ...interface{}) (Value, error) {
one, err := c.DB.GetOne(sql, args...)
if err != nil {
return nil, err
return gvar.New(nil), err
}
for _, v := range one {
return v, nil
}
return nil, nil
return gvar.New(nil), nil
}
// GetCount queries and returns the count from database.

View File

@ -8,6 +8,7 @@ package gdb
import (
"fmt"
"github.com/gogf/gf/container/gvar"
"github.com/gogf/gf/util/gconv"
"reflect"
)
@ -118,12 +119,12 @@ func (m *Model) Value(fieldsAndWhere ...interface{}) (Value, error) {
}
one, err := m.One()
if err != nil {
return nil, err
return gvar.New(nil), err
}
for _, v := range one {
return v, nil
}
return nil, nil
return gvar.New(nil), nil
}
// Array queries and returns data values as slice from database.

View File

@ -210,8 +210,8 @@ func (r *Redis) Do(command string, args ...interface{}) (interface{}, error) {
return conn.Do(command, args...)
}
// DoVar returns value from Do as *gvar.Var.
func (r *Redis) DoVar(command string, args ...interface{}) (*gvar.Var, error) {
// DoVar returns value from Do as gvar.Var.
func (r *Redis) DoVar(command string, args ...interface{}) (gvar.Var, error) {
v, err := r.Do(command, args...)
if result, ok := v.([]byte); ok {
return gvar.New(gconv.UnsafeBytesToStr(result)), err

View File

@ -44,13 +44,13 @@ func (c *Conn) Do(commandName string, args ...interface{}) (reply interface{}, e
}
// DoVar retrieves and returns the result from command as gvar.Var.
func (c *Conn) DoVar(command string, args ...interface{}) (*gvar.Var, error) {
func (c *Conn) DoVar(command string, args ...interface{}) (gvar.Var, error) {
v, err := c.Do(command, args...)
return gvar.New(v), err
}
// ReceiveVar receives a single reply as gvar.Var from the Redis server.
func (c *Conn) ReceiveVar() (*gvar.Var, error) {
func (c *Conn) ReceiveVar() (gvar.Var, error) {
v, err := c.Receive()
return gvar.New(v), err
}

View File

@ -15,7 +15,7 @@ import (
func Example_autoMarshalUnmarshalMap() {
var (
err error
result *gvar.Var
result gvar.Var
key = "user"
data = g.Map{
"id": 10000,
@ -40,7 +40,7 @@ func Example_autoMarshalUnmarshalStruct() {
}
var (
err error
result *gvar.Var
result gvar.Var
key = "user"
user = &User{
Id: 10000,
@ -71,7 +71,7 @@ func Example_autoMarshalUnmarshalStructSlice() {
}
var (
err error
result *gvar.Var
result gvar.Var
key = "user-slice"
users1 = []User{
{
@ -104,7 +104,7 @@ func Example_autoMarshalUnmarshalStructSlice() {
func Example_hashSet() {
var (
err error
result *gvar.Var
result gvar.Var
key = "user"
)
_, err = g.Redis().Do("HSET", key, "id", 10000)

View File

@ -356,7 +356,7 @@ func Test_Auto_MarshalSlice(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
var (
result *gvar.Var
result gvar.Var
key = "user-slice"
users1 = []User{
{

View File

@ -68,13 +68,13 @@ func (j *Json) Get(pattern string, def ...interface{}) interface{} {
return nil
}
// GetVar returns a *gvar.Var with value by given <pattern>.
func (j *Json) GetVar(pattern string, def ...interface{}) *gvar.Var {
// GetVar returns a gvar.Var with value by given <pattern>.
func (j *Json) GetVar(pattern string, def ...interface{}) gvar.Var {
return gvar.New(j.Get(pattern, def...))
}
// GetVars returns []*gvar.Var with value by given <pattern>.
func (j *Json) GetVars(pattern string, def ...interface{}) []*gvar.Var {
// GetVars returns []gvar.Var with value by given <pattern>.
func (j *Json) GetVars(pattern string, def ...interface{}) []gvar.Var {
return gvar.New(j.Get(pattern, def...)).Vars()
}

View File

@ -10,7 +10,7 @@ import (
"github.com/gogf/gf/container/gvar"
)
// Var is a universal variable type, like generics.
// Var is a universal variable interface, like generics.
type Var = gvar.Var
// Frequently-used map type alias.

View File

@ -13,16 +13,11 @@ import (
"github.com/gogf/gf/util/gutil"
)
// NewVar returns a *gvar.Var.
func NewVar(i interface{}, safe ...bool) *Var {
// NewVar returns a gvar.Var.
func NewVar(i interface{}, safe ...bool) Var {
return gvar.New(i, safe...)
}
// CreateVar returns a gvar.Var.
func CreateVar(i interface{}, safe ...bool) Var {
return gvar.Create(i, safe...)
}
// Wait blocks until all the web servers shutdown.
func Wait() {
ghttp.Wait()

View File

@ -42,7 +42,7 @@ func doInit() {
// Fetching Rules:
// 1. Command line arguments are in lowercase format, eg: gf.<package name>.<variable name>;
// 2. Environment arguments are in uppercase format, eg: GF_<package name>_<variable name>
func Get(key string, def ...interface{}) *gvar.Var {
func Get(key string, def ...interface{}) gvar.Var {
value := interface{}(nil)
if len(def) > 0 {
value = def[0]

View File

@ -0,0 +1,15 @@
// Copyright 2020 gf Author(https://github.com/gogf/gf). 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 intstore provides local variable storing for internal usage.
package intstore
import "reflect"
var (
// Reflect type for gvar.VarImp.
ReflectTypeVarImp = reflect.Type(nil)
)

View File

@ -99,7 +99,7 @@ func (r *Request) Get(key string, def ...interface{}) interface{} {
// GetVar is alis of GetRequestVar.
// See GetRequestVar.
func (r *Request) GetVar(key string, def ...interface{}) *gvar.Var {
func (r *Request) GetVar(key string, def ...interface{}) gvar.Var {
return r.GetRequestVar(key, def...)
}

View File

@ -29,7 +29,7 @@ func (r *Request) GetCtx() context.Context {
// GetCtxVar retrieves and returns a Var with given key name.
// The optional parameter <def> specifies the default value of the Var if given <key>
// does not exist in the context.
func (r *Request) GetCtxVar(key interface{}, def ...interface{}) *gvar.Var {
func (r *Request) GetCtxVar(key interface{}, def ...interface{}) gvar.Var {
value := r.Context().Value(key)
if value == nil && len(def) > 0 {
value = def[0]

View File

@ -37,7 +37,7 @@ func (r *Request) GetForm(key string, def ...interface{}) interface{} {
// GetFormVar retrieves and returns parameter <key> from form as Var.
// It returns <def> if <key> does not exist in the form and <def> is given, or else it returns nil.
func (r *Request) GetFormVar(key string, def ...interface{}) *gvar.Var {
func (r *Request) GetFormVar(key string, def ...interface{}) gvar.Var {
return gvar.New(r.GetForm(key, def...))
}
@ -169,13 +169,13 @@ func (r *Request) GetFormMapStrStr(kvMap ...map[string]interface{}) map[string]s
return nil
}
// GetFormMapStrVar retrieves and returns all form parameters passed from client as map[string]*gvar.Var.
// GetFormMapStrVar retrieves and returns all form parameters passed from client as map[string]gvar.Var.
// The parameter <kvMap> specifies the keys retrieving from client parameters, the associated values
// are the default values if the client does not pass.
func (r *Request) GetFormMapStrVar(kvMap ...map[string]interface{}) map[string]*gvar.Var {
func (r *Request) GetFormMapStrVar(kvMap ...map[string]interface{}) map[string]gvar.Var {
postMap := r.GetFormMap(kvMap...)
if len(postMap) > 0 {
m := make(map[string]*gvar.Var, len(postMap))
m := make(map[string]gvar.Var, len(postMap))
for k, v := range postMap {
m[k] = gvar.New(v)
}

View File

@ -29,9 +29,9 @@ func (r *Request) GetParam(key string, def ...interface{}) interface{} {
return nil
}
// GetParamVar returns custom parameter with given name <key> as *gvar.Var.
// GetParamVar returns custom parameter with given name <key> as gvar.Var.
// It returns <def> if <key> does not exist.
// It returns nil if <def> is not passed.
func (r *Request) GetParamVar(key string, def ...interface{}) *gvar.Var {
func (r *Request) GetParamVar(key string, def ...interface{}) gvar.Var {
return gvar.New(r.GetParam(key, def...))
}

View File

@ -39,7 +39,7 @@ func (r *Request) GetPost(key string, def ...interface{}) interface{} {
}
// Deprecated.
func (r *Request) GetPostVar(key string, def ...interface{}) *gvar.Var {
func (r *Request) GetPostVar(key string, def ...interface{}) gvar.Var {
return gvar.New(r.GetPost(key, def...))
}
@ -180,15 +180,15 @@ func (r *Request) GetPostMapStrStr(kvMap ...map[string]interface{}) map[string]s
}
// GetPostMapStrVar retrieves and returns all parameters in the form and body passed from client
// as map[string]*gvar.Var. The parameter <kvMap> specifies the keys
// as map[string]gvar.Var. The parameter <kvMap> specifies the keys
// retrieving from client parameters, the associated values are the default values if the client
// does not pass.
//
// Deprecated.
func (r *Request) GetPostMapStrVar(kvMap ...map[string]interface{}) map[string]*gvar.Var {
func (r *Request) GetPostMapStrVar(kvMap ...map[string]interface{}) map[string]gvar.Var {
postMap := r.GetPostMap(kvMap...)
if len(postMap) > 0 {
m := make(map[string]*gvar.Var, len(postMap))
m := make(map[string]gvar.Var, len(postMap))
for k, v := range postMap {
m[k] = gvar.New(v)
}

View File

@ -46,7 +46,7 @@ func (r *Request) GetQuery(key string, def ...interface{}) interface{} {
return nil
}
func (r *Request) GetQueryVar(key string, def ...interface{}) *gvar.Var {
func (r *Request) GetQueryVar(key string, def ...interface{}) gvar.Var {
return gvar.New(r.GetQuery(key, def...))
}
@ -172,13 +172,13 @@ func (r *Request) GetQueryMapStrStr(kvMap ...map[string]interface{}) map[string]
}
// GetQueryMapStrVar retrieves and returns all parameters passed from client using HTTP GET method
// as map[string]*gvar.Var. The parameter <kvMap> specifies the keys
// as map[string]gvar.Var. The parameter <kvMap> specifies the keys
// retrieving from client parameters, the associated values are the default values if the client
// does not pass.
func (r *Request) GetQueryMapStrVar(kvMap ...map[string]interface{}) map[string]*gvar.Var {
func (r *Request) GetQueryMapStrVar(kvMap ...map[string]interface{}) map[string]gvar.Var {
queryMap := r.GetQueryMap(kvMap...)
if len(queryMap) > 0 {
m := make(map[string]*gvar.Var, len(queryMap))
m := make(map[string]gvar.Var, len(queryMap))
for k, v := range queryMap {
m[k] = gvar.New(v)
}

View File

@ -46,9 +46,9 @@ func (r *Request) GetRequest(key string, def ...interface{}) interface{} {
}
// GetRequestVar retrieves and returns the parameter named <key> passed from client and
// custom params as *gvar.Var, no matter what HTTP method the client is using. The parameter
// custom params as gvar.Var, no matter what HTTP method the client is using. The parameter
// <def> specifies the default value if the <key> does not exist.
func (r *Request) GetRequestVar(key string, def ...interface{}) *gvar.Var {
func (r *Request) GetRequestVar(key string, def ...interface{}) gvar.Var {
return gvar.New(r.GetRequest(key, def...))
}
@ -247,13 +247,13 @@ func (r *Request) GetRequestMapStrStr(kvMap ...map[string]interface{}) map[strin
}
// GetRequestMapStrVar retrieves and returns all parameters passed from client and custom
// params as map[string]*gvar.Var, no matter what HTTP method the client is using. The parameter
// params as map[string]gvar.Var, no matter what HTTP method the client is using. The parameter
// <kvMap> specifies the keys retrieving from client parameters, the associated values are the
// default values if the client does not pass.
func (r *Request) GetRequestMapStrVar(kvMap ...map[string]interface{}) map[string]*gvar.Var {
func (r *Request) GetRequestMapStrVar(kvMap ...map[string]interface{}) map[string]gvar.Var {
requestMap := r.GetRequestMap(kvMap...)
if len(requestMap) > 0 {
m := make(map[string]*gvar.Var, len(requestMap))
m := make(map[string]gvar.Var, len(requestMap))
for k, v := range requestMap {
m[k] = gvar.New(v)
}

View File

@ -22,9 +22,9 @@ func (r *Request) GetRouterValue(key string, def ...interface{}) interface{} {
return nil
}
// GetRouterVar retrieves and returns the router value as *gvar.var with given key name <key>.
// GetRouterVar retrieves and returns the router value as gvar.Var with given key name <key>.
// It returns <def> if <key> does not exist.
func (r *Request) GetRouterVar(key string, def ...interface{}) *gvar.Var {
func (r *Request) GetRouterVar(key string, def ...interface{}) gvar.Var {
return gvar.New(r.GetRouterValue(key, def...))
}

View File

@ -59,8 +59,8 @@ func Get(name string, def ...interface{}) interface{} {
return nil
}
// Get retrieves and returns the build-in binary variable of given name as *gvar.Var.
func GetVar(name string, def ...interface{}) *gvar.Var {
// Get retrieves and returns the build-in binary variable of given name as gvar.Var.
func GetVar(name string, def ...interface{}) gvar.Var {
return gvar.New(Get(name, def...))
}

View File

@ -40,8 +40,8 @@ func Get(key interface{}) interface{} {
return cache.Get(key)
}
// GetVar retrieves and returns the value of <key> as *gvar.Var.
func GetVar(key interface{}) *gvar.Var {
// GetVar retrieves and returns the value of <key> as gvar.Var.
func GetVar(key interface{}) gvar.Var {
return cache.GetVar(key)
}

View File

@ -223,8 +223,8 @@ func (c *memCache) Get(key interface{}) interface{} {
return nil
}
// GetVar retrieves and returns the value of <key> as *gvar.Var.
func (c *memCache) GetVar(key interface{}) *gvar.Var {
// GetVar retrieves and returns the value of <key> as gvar.Var.
func (c *memCache) GetVar(key interface{}) gvar.Var {
return gvar.New(c.Get(key))
}

View File

@ -23,7 +23,7 @@ func (c *Config) Get(pattern string, def ...interface{}) interface{} {
return nil
}
func (c *Config) GetVar(pattern string, def ...interface{}) *gvar.Var {
func (c *Config) GetVar(pattern string, def ...interface{}) gvar.Var {
if j := c.getJson(); j != nil {
return gvar.New(j.Get(pattern, def...))
}

View File

@ -51,8 +51,8 @@ func GetOpt(name string, def ...string) string {
return ""
}
// GetOptVar returns the option value named <name> as *gvar.Var.
func GetOptVar(name string, def ...string) *gvar.Var {
// GetOptVar returns the option value named <name> as gvar.Var.
func GetOptVar(name string, def ...string) gvar.Var {
doInit()
return gvar.New(GetOpt(name, def...))
}
@ -82,8 +82,8 @@ func GetArg(index int, def ...string) string {
return ""
}
// GetArgVar returns the argument at <index> as *gvar.Var.
func GetArgVar(index int, def ...string) *gvar.Var {
// GetArgVar returns the argument at <index> as gvar.Var.
func GetArgVar(index int, def ...string) gvar.Var {
doInit()
return gvar.New(GetArg(index, def...))
}

View File

@ -170,8 +170,8 @@ func (p *Parser) GetOpt(name string, def ...string) string {
return ""
}
// GetOptVar returns the option value named <name> as *gvar.Var.
func (p *Parser) GetOptVar(name string, def ...interface{}) *gvar.Var {
// GetOptVar returns the option value named <name> as gvar.Var.
func (p *Parser) GetOptVar(name string, def ...interface{}) gvar.Var {
if p.ContainsOpt(name) {
return gvar.New(p.GetOpt(name))
}
@ -203,8 +203,8 @@ func (p *Parser) GetArg(index int, def ...string) string {
return ""
}
// GetArgVar returns the argument at <index> as *gvar.Var.
func (p *Parser) GetArgVar(index int, def ...string) *gvar.Var {
// GetArgVar returns the argument at <index> as gvar.Var.
func (p *Parser) GetArgVar(index int, def ...string) gvar.Var {
return gvar.New(p.GetArg(index, def...))
}

View File

@ -43,7 +43,7 @@ func Get(key string, def ...string) string {
// GetVar creates and returns a Var with the value of the environment variable
// named by the <key>. It uses the given <def> if the variable does not exist
// in the environment.
func GetVar(key string, def ...interface{}) *gvar.Var {
func GetVar(key string, def ...interface{}) gvar.Var {
v, ok := os.LookupEnv(key)
if !ok && len(def) > 0 {
return gvar.New(def[0])

View File

@ -248,7 +248,7 @@ func (s *Session) Get(key string, def ...interface{}) interface{} {
return nil
}
func (s *Session) GetVar(key string, def ...interface{}) *gvar.Var {
func (s *Session) GetVar(key string, def ...interface{}) gvar.Var {
return gvar.New(s.Get(key, def...), true)
}

View File

@ -12,6 +12,7 @@ package gconv
import (
"encoding/json"
"fmt"
"github.com/gogf/gf/internal/intstore"
"github.com/gogf/gf/os/gtime"
"reflect"
"strconv"
@ -251,6 +252,18 @@ func Convert(i interface{}, t string, params ...interface{}) interface{} {
case "[]map[string]interface{}":
return Maps(i)
case "gvar.Var":
rv := reflect.New(intstore.ReflectTypeVarImp)
ri := rv.Interface()
if v, ok := ri.(apiSet); ok {
v.Set(i)
} else if v, ok := ri.(apiUnmarshalValue); ok {
v.UnmarshalValue(i)
} else {
rv.Set(reflect.ValueOf(i))
}
return ri
default:
return i
}

View File

@ -51,3 +51,8 @@ type apiMapStrAny interface {
type apiUnmarshalValue interface {
UnmarshalValue(interface{}) error
}
// apiSet is the interface for custom value assignment.
type apiSet interface {
Set(value interface{}) (old interface{})
}

View File

@ -241,34 +241,28 @@ func bindVarToStructAttr(elem reflect.Value, name string, value interface{}) (er
return nil
}
// bindVarToStructByIndex sets value to struct object attribute by index.
func bindVarToStructByIndex(elem reflect.Value, index int, value interface{}) (err error) {
structFieldValue := elem.FieldByIndex([]int{index})
if !structFieldValue.IsValid() {
return nil
}
// CanSet checks whether attribute is public accessible.
if !structFieldValue.CanSet() {
return nil
}
// If any panic, it secondly uses reflect conversion and assignment.
defer func() {
if recover() != nil {
err = bindVarToReflectValue(structFieldValue, value)
}
}()
if empty.IsNil(value) {
structFieldValue.Set(reflect.Zero(structFieldValue.Type()))
} else {
// It firstly simply assigns the value to the attribute.
structFieldValue.Set(reflect.ValueOf(Convert(value, structFieldValue.Type().String())))
}
return nil
}
// bindVarToReflectValue sets <value> to reflect value object <structFieldValue>.
func bindVarToReflectValue(structFieldValue reflect.Value, value interface{}) (err error) {
switch structFieldValue.Kind() {
kind := structFieldValue.Kind()
// Converting using interface, for some kinds.
switch kind {
case reflect.Slice, reflect.Array, reflect.Ptr, reflect.Interface:
if !structFieldValue.IsNil() {
if v, ok := structFieldValue.Interface().(apiSet); ok {
v.Set(value)
return nil
} else if v, ok := structFieldValue.Interface().(apiUnmarshalValue); ok {
err = v.UnmarshalValue(value)
if err == nil {
return err
}
}
}
}
// Converting by kind.
switch kind {
case reflect.Struct:
if err := Struct(value, structFieldValue); err != nil {
// Note there's reflect conversion mechanism here.