gvar.go 5.8 KB

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