gvar.go 4.9 KB

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