github.com/15mga/kiwi@v0.0.2-0.20240324021231-b95d5c3ac751/util/m.go (about)

     1  package util
     2  
     3  type (
     4  	M    map[string]any
     5  	MS   map[string]string
     6  	M32  map[string]int32
     7  	M64  map[string]int64
     8  	MF32 map[string]float32
     9  	MF64 map[string]float64
    10  )
    11  
    12  func MGet[T any](m M, key string) (T, bool) {
    13  	v, ok := m[key]
    14  	if !ok {
    15  		return Default[T](), false
    16  	}
    17  	v1, ok := v.(T)
    18  	return v1, ok
    19  }
    20  
    21  func MGet2[T any](m M, keys ...string) (T, bool) {
    22  	switch len(keys) {
    23  	case 0:
    24  		return Default[T](), false
    25  	case 1:
    26  		return MGet[T](m, keys[0])
    27  	default:
    28  		sm, ok := MGet[M](m, keys[0])
    29  		if !ok {
    30  			return Default[T](), false
    31  		}
    32  		return MGet2[T](sm, keys[1:]...)
    33  	}
    34  }
    35  
    36  func MGetOrSet[T any](m M, key string, new func() T) T {
    37  	v, ok := m[key]
    38  	if !ok {
    39  		v = new()
    40  		m[key] = v
    41  	}
    42  	return v.(T)
    43  }
    44  
    45  func MPop[T any](m M, key string) (T, bool) {
    46  	v, ok := m[key]
    47  	if !ok {
    48  		return Default[T](), true
    49  	}
    50  	delete(m, key)
    51  	v1, ok := v.(T)
    52  	return v1, ok
    53  }
    54  
    55  func MReplace[T any](m M, key string, val T) (T, bool) {
    56  	oldVal, ok := MGet[T](m, key)
    57  	if !ok {
    58  		return Default[T](), false
    59  	}
    60  	m[key] = val
    61  	return oldVal, true
    62  }
    63  
    64  func MUpdate[T any](m M, key string, fn func(T) T) bool {
    65  	oldVal, ok := MGet[T](m, key)
    66  	m[key] = fn(oldVal)
    67  	return ok
    68  }
    69  
    70  func NewM(bytes []byte, m M) *Err {
    71  	var buff ByteBuffer
    72  	buff.InitBytes(bytes)
    73  	return buff.RMAny(m)
    74  }
    75  
    76  func (m M) Replace(key string, val any) (oldVal any, ok bool) {
    77  	oldVal, ok = m[key]
    78  	m[key] = val
    79  	return
    80  }
    81  
    82  func (m M) Update(key string, action AnyToAny) (ok bool) {
    83  	oldVal, ok := m[key]
    84  	m[key] = action(oldVal)
    85  	return
    86  }
    87  
    88  func (m M) Set(key string, val any) {
    89  	m[key] = val
    90  }
    91  
    92  func (m M) Set2(val any, keys ...string) *Err {
    93  	switch len(keys) {
    94  	case 0:
    95  		return NewErr(EcParamsErr, M{
    96  			"error": "keys empty",
    97  		})
    98  	case 1:
    99  		m[keys[0]] = val
   100  		return nil
   101  	default:
   102  		k := keys[0]
   103  		o, ok := m[k]
   104  		if !ok {
   105  			o = M{}
   106  			m[k] = o
   107  		}
   108  		sm, ok := o.(M)
   109  		if !ok {
   110  			return NewErr(EcWrongType, M{
   111  				"error": "not M",
   112  				"key":   k,
   113  				"value": o,
   114  			})
   115  		}
   116  		return sm.Set2(val, keys[1:]...)
   117  	}
   118  }
   119  
   120  func (m M) Del(key string) {
   121  	delete(m, key)
   122  }
   123  
   124  func (m M) Get(key string) (any, bool) {
   125  	val, ok := m[key]
   126  	if !ok {
   127  		return nil, false
   128  	}
   129  	return val, true
   130  }
   131  
   132  func (m M) MustGet(key string, def any) any {
   133  	val, ok := m[key]
   134  	if !ok {
   135  		m[key] = def
   136  		return def
   137  	}
   138  	return val
   139  }
   140  
   141  func (m M) Has(key string) (ok bool) {
   142  	_, ok = m[key]
   143  	return
   144  }
   145  
   146  func (m M) ToJson() ([]byte, *Err) {
   147  	bytes, err := JsonMarshal(m)
   148  	return bytes, WrapErr(EcMarshallErr, err)
   149  }
   150  
   151  func (m M) FromJson(bytes []byte) *Err {
   152  	return JsonUnmarshal(bytes, &m)
   153  }
   154  
   155  func (m M) ToGob() ([]byte, *Err) {
   156  	bytes, err := GobMarshal(m)
   157  	return bytes, WrapErr(EcMarshallErr, err)
   158  }
   159  
   160  func (m M) FromGob(bytes []byte) *Err {
   161  	e := GobUnmarshal(bytes, &m)
   162  	if e != nil {
   163  		return WrapErr(EcUnmarshallErr, e)
   164  	}
   165  	return nil
   166  }
   167  
   168  func (m M) ToBytes() ([]byte, *Err) {
   169  	var buff ByteBuffer
   170  	buff.InitCap(64)
   171  	err := buff.WMAny(m)
   172  	if err != nil {
   173  		return nil, err
   174  	}
   175  	return buff.All(), nil
   176  }
   177  
   178  func (m M) FromBytes(bytes []byte) *Err {
   179  	var buff ByteBuffer
   180  	buff.InitBytes(bytes)
   181  	return buff.RMAny(m)
   182  }
   183  
   184  func (m M) ToSlice(slc *[]any) {
   185  	for k, v := range m {
   186  		*slc = append(*slc, k, v)
   187  	}
   188  }
   189  
   190  func (m M) Copy(toM M) M {
   191  	n := make(M, len(m))
   192  	for k, v := range m {
   193  		n[k] = v
   194  	}
   195  	return n
   196  }
   197  
   198  func (m M) CopyTo(n M) {
   199  	for k, v := range m {
   200  		n[k] = v
   201  	}
   202  }