gvar.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. "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. // Int8 converts and returns `v` as int8.
  95. func (v *Var) Int8() int8 {
  96. return gconv.Int8(v.Val())
  97. }
  98. // Int16 converts and returns `v` as int16.
  99. func (v *Var) Int16() int16 {
  100. return gconv.Int16(v.Val())
  101. }
  102. // Int32 converts and returns `v` as int32.
  103. func (v *Var) Int32() int32 {
  104. return gconv.Int32(v.Val())
  105. }
  106. // Int64 converts and returns `v` as int64.
  107. func (v *Var) Int64() int64 {
  108. return gconv.Int64(v.Val())
  109. }
  110. // Uint converts and returns `v` as uint.
  111. func (v *Var) Uint() uint {
  112. return gconv.Uint(v.Val())
  113. }
  114. // Uint8 converts and returns `v` as uint8.
  115. func (v *Var) Uint8() uint8 {
  116. return gconv.Uint8(v.Val())
  117. }
  118. // Uint16 converts and returns `v` as uint16.
  119. func (v *Var) Uint16() uint16 {
  120. return gconv.Uint16(v.Val())
  121. }
  122. // Uint32 converts and returns `v` as uint32.
  123. func (v *Var) Uint32() uint32 {
  124. return gconv.Uint32(v.Val())
  125. }
  126. // Uint64 converts and returns `v` as uint64.
  127. func (v *Var) Uint64() uint64 {
  128. return gconv.Uint64(v.Val())
  129. }
  130. // Float32 converts and returns `v` as float32.
  131. func (v *Var) Float32() float32 {
  132. return gconv.Float32(v.Val())
  133. }
  134. // Float64 converts and returns `v` as float64.
  135. func (v *Var) Float64() float64 {
  136. return gconv.Float64(v.Val())
  137. }
  138. // Time converts and returns `v` as time.Time.
  139. // The parameter `format` specifies the format of the time string using gtime,
  140. // eg: Y-m-d H:i:s.
  141. func (v *Var) Time(format ...string) time.Time {
  142. return gconv.Time(v.Val(), format...)
  143. }
  144. // Duration converts and returns `v` as time.Duration.
  145. // If value of `v` is string, then it uses time.ParseDuration for conversion.
  146. func (v *Var) Duration() time.Duration {
  147. return gconv.Duration(v.Val())
  148. }
  149. // GTime converts and returns `v` as *gtime.Time.
  150. // The parameter `format` specifies the format of the time string using gtime,
  151. // eg: Y-m-d H:i:s.
  152. func (v *Var) GTime(format ...string) *gtime.Time {
  153. return gconv.GTime(v.Val(), format...)
  154. }
  155. // MarshalJSON implements the interface MarshalJSON for json.Marshal.
  156. func (v *Var) MarshalJSON() ([]byte, error) {
  157. return json.Marshal(v.Val())
  158. }
  159. // UnmarshalJSON implements the interface UnmarshalJSON for json.Unmarshal.
  160. func (v *Var) UnmarshalJSON(b []byte) error {
  161. var i interface{}
  162. err := json.UnmarshalUseNumber(b, &i)
  163. if err != nil {
  164. return err
  165. }
  166. v.Set(i)
  167. return nil
  168. }
  169. // UnmarshalValue is an interface implement which sets any type of value for Var.
  170. func (v *Var) UnmarshalValue(value interface{}) error {
  171. v.Set(value)
  172. return nil
  173. }