github.com/zhongdalu/gf@v1.0.0/g/container/gvar/gvar.go (about) 1 // Copyright 2018-2019 gf Author(https://github.com/zhongdalu/gf). 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/zhongdalu/gf. 6 7 // Package gvar provides an universal variable type, like generics. 8 package gvar 9 10 import ( 11 "encoding/json" 12 "time" 13 14 "github.com/zhongdalu/gf/g/container/gtype" 15 "github.com/zhongdalu/gf/g/os/gtime" 16 "github.com/zhongdalu/gf/g/util/gconv" 17 ) 18 19 // Var is an universal variable type. 20 type Var struct { 21 value interface{} // Underlying value. 22 safe bool // Concurrent safe or not. 23 } 24 25 // New returns a new Var with given <value>. 26 // The parameter <unsafe> used to specify whether using Var in un-concurrent-safety, 27 // which is false in default, means concurrent-safe. 28 func New(value interface{}, unsafe ...bool) *Var { 29 v := &Var{} 30 if len(unsafe) == 0 || !unsafe[0] { 31 v.safe = true 32 v.value = gtype.NewInterface(value) 33 } else { 34 v.value = value 35 } 36 return v 37 } 38 39 // MarshalJSON implements the interface MarshalJSON for json.Marshal. 40 func (v *Var) MarshalJSON() ([]byte, error) { 41 return json.Marshal(v.Val()) 42 } 43 44 // Set sets <value> to <v>, and returns the old value. 45 func (v *Var) Set(value interface{}) (old interface{}) { 46 if v.safe { 47 old = v.value.(*gtype.Interface).Set(value) 48 } else { 49 old = v.value 50 v.value = value 51 } 52 return 53 } 54 55 // Val returns the current value of <v>. 56 func (v *Var) Val() interface{} { 57 if v.safe { 58 return v.value.(*gtype.Interface).Val() 59 } 60 return v.value 61 } 62 63 // Interface is alias of Val. 64 func (v *Var) Interface() interface{} { 65 return v.Val() 66 } 67 68 // IsNil checks whether <v> is nil. 69 func (v *Var) IsNil() bool { 70 return v.Val() == nil 71 } 72 73 // Bytes converts and returns <v> as []byte. 74 func (v *Var) Bytes() []byte { 75 return gconv.Bytes(v.Val()) 76 } 77 78 // String converts and returns <v> as string. 79 func (v *Var) String() string { 80 return gconv.String(v.Val()) 81 } 82 83 // Bool converts and returns <v> as bool. 84 func (v *Var) Bool() bool { 85 return gconv.Bool(v.Val()) 86 } 87 88 // Int converts and returns <v> as int. 89 func (v *Var) Int() int { 90 return gconv.Int(v.Val()) 91 } 92 93 // Int8 converts and returns <v> as int8. 94 func (v *Var) Int8() int8 { 95 return gconv.Int8(v.Val()) 96 } 97 98 // Int16 converts and returns <v> as int16. 99 func (v *Var) Int16() int16 { 100 return gconv.Int16(v.Val()) 101 } 102 103 // Int32 converts and returns <v> as int32. 104 func (v *Var) Int32() int32 { 105 return gconv.Int32(v.Val()) 106 } 107 108 // Int64 converts and returns <v> as int64. 109 func (v *Var) Int64() int64 { 110 return gconv.Int64(v.Val()) 111 } 112 113 // Uint converts and returns <v> as uint. 114 func (v *Var) Uint() uint { 115 return gconv.Uint(v.Val()) 116 } 117 118 // Uint8 converts and returns <v> as uint8. 119 func (v *Var) Uint8() uint8 { 120 return gconv.Uint8(v.Val()) 121 } 122 123 // Uint16 converts and returns <v> as uint16. 124 func (v *Var) Uint16() uint16 { 125 return gconv.Uint16(v.Val()) 126 } 127 128 // Uint32 converts and returns <v> as uint32. 129 func (v *Var) Uint32() uint32 { 130 return gconv.Uint32(v.Val()) 131 } 132 133 // Uint64 converts and returns <v> as uint64. 134 func (v *Var) Uint64() uint64 { 135 return gconv.Uint64(v.Val()) 136 } 137 138 // Float32 converts and returns <v> as float32. 139 func (v *Var) Float32() float32 { 140 return gconv.Float32(v.Val()) 141 } 142 143 // Float64 converts and returns <v> as float64. 144 func (v *Var) Float64() float64 { 145 return gconv.Float64(v.Val()) 146 } 147 148 // Ints converts and returns <v> as []int. 149 func (v *Var) Ints() []int { 150 return gconv.Ints(v.Val()) 151 } 152 153 // Floats converts and returns <v> as []float64. 154 func (v *Var) Floats() []float64 { 155 return gconv.Floats(v.Val()) 156 } 157 158 // Strings converts and returns <v> as []string. 159 func (v *Var) Strings() []string { 160 return gconv.Strings(v.Val()) 161 } 162 163 // Interfaces converts and returns <v> as []interfaces{}. 164 func (v *Var) Interfaces() []interface{} { 165 return gconv.Interfaces(v.Val()) 166 } 167 168 // Time converts and returns <v> as time.Time. 169 // The parameter <format> specifies the format of the time string using gtime, 170 // eg: Y-m-d H:i:s. 171 func (v *Var) Time(format ...string) time.Time { 172 return gconv.Time(v.Val(), format...) 173 } 174 175 // Duration converts and returns <v> as time.Duration. 176 // If value of <v> is string, then it uses time.ParseDuration for conversion. 177 func (v *Var) Duration() time.Duration { 178 return gconv.Duration(v.Val()) 179 } 180 181 // GTime converts and returns <v> as *gtime.Time. 182 // The parameter <format> specifies the format of the time string using gtime, 183 // eg: Y-m-d H:i:s. 184 func (v *Var) GTime(format ...string) *gtime.Time { 185 return gconv.GTime(v.Val(), format...) 186 } 187 188 // Map converts <v> to map[string]interface{}. 189 func (v *Var) Map(tags ...string) map[string]interface{} { 190 return gconv.Map(v.Val(), tags...) 191 } 192 193 // MapDeep converts <v> to map[string]interface{} recursively. 194 func (v *Var) MapDeep(tags ...string) map[string]interface{} { 195 return gconv.MapDeep(v.Val(), tags...) 196 } 197 198 // Struct maps value of <v> to <pointer>. 199 // The parameter <pointer> should be a pointer to a struct instance. 200 // The parameter <mapping> is used to specify the key-to-attribute mapping rules. 201 func (v *Var) Struct(pointer interface{}, mapping ...map[string]string) error { 202 return gconv.Struct(v.Val(), pointer, mapping...) 203 } 204 205 // Struct maps value of <v> to <pointer> recursively. 206 // The parameter <pointer> should be a pointer to a struct instance. 207 // The parameter <mapping> is used to specify the key-to-attribute mapping rules. 208 func (v *Var) StructDeep(pointer interface{}, mapping ...map[string]string) error { 209 return gconv.StructDeep(v.Val(), pointer, mapping...) 210 } 211 212 // Structs converts <v> to given struct slice. 213 func (v *Var) Structs(pointer interface{}, mapping ...map[string]string) (err error) { 214 return gconv.Structs(v.Val(), pointer, mapping...) 215 } 216 217 // StructsDeep converts <v> to given struct slice recursively. 218 func (v *Var) StructsDeep(pointer interface{}, mapping ...map[string]string) (err error) { 219 return gconv.StructsDeep(v.Val(), pointer, mapping...) 220 }