github.com/gogf/gf/v2@v2.7.4/container/gvar/gvar.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/gogf/gf. 6 7 // Package gvar provides an universal variable type, like runtime generics. 8 package gvar 9 10 import ( 11 "time" 12 13 "github.com/gogf/gf/v2/container/gtype" 14 "github.com/gogf/gf/v2/internal/deepcopy" 15 "github.com/gogf/gf/v2/internal/json" 16 "github.com/gogf/gf/v2/os/gtime" 17 "github.com/gogf/gf/v2/util/gconv" 18 "github.com/gogf/gf/v2/util/gutil" 19 ) 20 21 // Var is an universal variable type implementer. 22 type Var struct { 23 value interface{} // Underlying value. 24 safe bool // Concurrent safe or not. 25 } 26 27 // New creates and returns a new Var with given `value`. 28 // The optional parameter `safe` specifies whether Var is used in concurrent-safety, 29 // which is false in default. 30 func New(value interface{}, safe ...bool) *Var { 31 if len(safe) > 0 && safe[0] { 32 return &Var{ 33 value: gtype.NewInterface(value), 34 safe: true, 35 } 36 } 37 return &Var{ 38 value: value, 39 } 40 } 41 42 // Copy does a deep copy of current Var and returns a pointer to this Var. 43 func (v *Var) Copy() *Var { 44 return New(gutil.Copy(v.Val()), v.safe) 45 } 46 47 // Clone does a shallow copy of current Var and returns a pointer to this Var. 48 func (v *Var) Clone() *Var { 49 return New(v.Val(), v.safe) 50 } 51 52 // Set sets `value` to `v`, and returns the old value. 53 func (v *Var) Set(value interface{}) (old interface{}) { 54 if v.safe { 55 if t, ok := v.value.(*gtype.Interface); ok { 56 old = t.Set(value) 57 return 58 } 59 } 60 old = v.value 61 v.value = value 62 return 63 } 64 65 // Val returns the current value of `v`. 66 func (v *Var) Val() interface{} { 67 if v == nil { 68 return nil 69 } 70 if v.safe { 71 if t, ok := v.value.(*gtype.Interface); ok { 72 return t.Val() 73 } 74 } 75 return v.value 76 } 77 78 // Interface is alias of Val. 79 func (v *Var) Interface() interface{} { 80 return v.Val() 81 } 82 83 // Bytes converts and returns `v` as []byte. 84 func (v *Var) Bytes() []byte { 85 return gconv.Bytes(v.Val()) 86 } 87 88 // String converts and returns `v` as string. 89 func (v *Var) String() string { 90 return gconv.String(v.Val()) 91 } 92 93 // Bool converts and returns `v` as bool. 94 func (v *Var) Bool() bool { 95 return gconv.Bool(v.Val()) 96 } 97 98 // Int converts and returns `v` as int. 99 func (v *Var) Int() int { 100 return gconv.Int(v.Val()) 101 } 102 103 // Int8 converts and returns `v` as int8. 104 func (v *Var) Int8() int8 { 105 return gconv.Int8(v.Val()) 106 } 107 108 // Int16 converts and returns `v` as int16. 109 func (v *Var) Int16() int16 { 110 return gconv.Int16(v.Val()) 111 } 112 113 // Int32 converts and returns `v` as int32. 114 func (v *Var) Int32() int32 { 115 return gconv.Int32(v.Val()) 116 } 117 118 // Int64 converts and returns `v` as int64. 119 func (v *Var) Int64() int64 { 120 return gconv.Int64(v.Val()) 121 } 122 123 // Uint converts and returns `v` as uint. 124 func (v *Var) Uint() uint { 125 return gconv.Uint(v.Val()) 126 } 127 128 // Uint8 converts and returns `v` as uint8. 129 func (v *Var) Uint8() uint8 { 130 return gconv.Uint8(v.Val()) 131 } 132 133 // Uint16 converts and returns `v` as uint16. 134 func (v *Var) Uint16() uint16 { 135 return gconv.Uint16(v.Val()) 136 } 137 138 // Uint32 converts and returns `v` as uint32. 139 func (v *Var) Uint32() uint32 { 140 return gconv.Uint32(v.Val()) 141 } 142 143 // Uint64 converts and returns `v` as uint64. 144 func (v *Var) Uint64() uint64 { 145 return gconv.Uint64(v.Val()) 146 } 147 148 // Float32 converts and returns `v` as float32. 149 func (v *Var) Float32() float32 { 150 return gconv.Float32(v.Val()) 151 } 152 153 // Float64 converts and returns `v` as float64. 154 func (v *Var) Float64() float64 { 155 return gconv.Float64(v.Val()) 156 } 157 158 // Time converts and returns `v` as time.Time. 159 // The parameter `format` specifies the format of the time string using gtime, 160 // eg: Y-m-d H:i:s. 161 func (v *Var) Time(format ...string) time.Time { 162 return gconv.Time(v.Val(), format...) 163 } 164 165 // Duration converts and returns `v` as time.Duration. 166 // If value of `v` is string, then it uses time.ParseDuration for conversion. 167 func (v *Var) Duration() time.Duration { 168 return gconv.Duration(v.Val()) 169 } 170 171 // GTime converts and returns `v` as *gtime.Time. 172 // The parameter `format` specifies the format of the time string using gtime, 173 // eg: Y-m-d H:i:s. 174 func (v *Var) GTime(format ...string) *gtime.Time { 175 return gconv.GTime(v.Val(), format...) 176 } 177 178 // MarshalJSON implements the interface MarshalJSON for json.Marshal. 179 func (v Var) MarshalJSON() ([]byte, error) { 180 return json.Marshal(v.Val()) 181 } 182 183 // UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal. 184 func (v *Var) UnmarshalJSON(b []byte) error { 185 var i interface{} 186 if err := json.UnmarshalUseNumber(b, &i); err != nil { 187 return err 188 } 189 v.Set(i) 190 return nil 191 } 192 193 // UnmarshalValue is an interface implement which sets any type of value for Var. 194 func (v *Var) UnmarshalValue(value interface{}) error { 195 v.Set(value) 196 return nil 197 } 198 199 // DeepCopy implements interface for deep copy of current type. 200 func (v *Var) DeepCopy() interface{} { 201 if v == nil { 202 return nil 203 } 204 return New(deepcopy.Copy(v.Val()), v.safe) 205 }