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