gitee.com/h79/goutils@v1.22.10/common/common.go (about)

     1  package common
     2  
     3  import (
     4  	"errors"
     5  	"reflect"
     6  )
     7  
     8  func ElementsTo[T EleType](c []T) []interface{} {
     9  	var ret = make([]interface{}, len(c))
    10  	for i := range c {
    11  		ret[i] = c[i]
    12  	}
    13  	return ret
    14  }
    15  
    16  // IncludeElement 包含 找src在all
    17  func IncludeElement[T EleType](all []T, src ...T) []T {
    18  	if len(src) == 0 {
    19  		return all
    20  	}
    21  	if len(all) == 0 {
    22  		return []T{}
    23  	}
    24  	var rest []T
    25  	for i := range src {
    26  		for j := range all {
    27  			if all[j] == src[i] {
    28  				rest = append(rest, src[i])
    29  				break
    30  			}
    31  		}
    32  	}
    33  	return rest
    34  }
    35  
    36  // ExcludeElement 排除 找src不存在all
    37  func ExcludeElement[T EleType](all []T, src ...T) []T {
    38  	if len(src) == 0 {
    39  		return []T{}
    40  	}
    41  	if len(all) == 0 {
    42  		return src
    43  	}
    44  	var rest []T
    45  	for i := range src {
    46  		found := false
    47  		for j := range all {
    48  			if all[j] == src[i] {
    49  				found = true
    50  				break
    51  			}
    52  		}
    53  		if !found {
    54  			rest = append(rest, src[i])
    55  		}
    56  	}
    57  	return rest
    58  }
    59  
    60  // RemoveDuplicateElement 除重
    61  func RemoveDuplicateElement[T EleType](values []T) []T {
    62  	var result []T
    63  	temp := map[T]struct{}{}
    64  	for i := range values {
    65  		if _, ok := temp[values[i]]; !ok {
    66  			temp[values[i]] = struct{}{}
    67  			result = append(result, values[i])
    68  		}
    69  	}
    70  	return result
    71  }
    72  
    73  // RemoveElement 移除某元素
    74  func RemoveElement[T EleType](all []T, src T) []T {
    75  	return FilterElement(all, func(v T) bool {
    76  		return src != v
    77  	})
    78  }
    79  
    80  type FilterFunc[T EleType] func(v T) bool
    81  
    82  // FilterElement 过滤某元素
    83  func FilterElement[T EleType](all []T, cb FilterFunc[T]) []T {
    84  	var rest []T
    85  	for i := range all {
    86  		if cb(all[i]) {
    87  			rest = append(rest, all[i])
    88  		}
    89  	}
    90  	return rest
    91  }
    92  
    93  func FindElement[T EleType](all []T, src T) bool {
    94  	for i := range all {
    95  		if src == all[i] {
    96  			return true
    97  		}
    98  	}
    99  	return false
   100  }
   101  
   102  type EleType interface {
   103  	string | bool | int8 | int16 | int | int32 | int64 | uint8 | uint16 | uint | uint32 | uint64 | float32 | float64
   104  }
   105  
   106  type ReverseType interface {
   107  	rune | bool | int8 | int16 | int64 | uint8 | uint16 | uint32 | uint64 | float32 | float64
   108  }
   109  
   110  func ReverseElement[T ReverseType](rs []T) []T {
   111  	for i, j := 0, len(rs)-1; i < j; i, j = i+1, j-1 {
   112  		rs[i], rs[j] = rs[j], rs[i]
   113  	}
   114  	return rs
   115  }
   116  
   117  // Defined checks if variable is defined in a struct
   118  func Defined(data interface{}, field string) bool {
   119  	t := reflect.Indirect(reflect.ValueOf(data)).Type()
   120  
   121  	if t.Kind() != reflect.Struct {
   122  		return false
   123  	}
   124  	_, b := t.FieldByName(field)
   125  	return b
   126  }
   127  
   128  // Dict allows to send more than one variable into a template
   129  func Dict(values ...interface{}) (map[string]interface{}, error) {
   130  	if len(values)%2 != 0 {
   131  		return nil, errors.New("invalid dict call")
   132  	}
   133  	dict := make(map[string]interface{}, len(values)/2)
   134  	for i := 0; i < len(values); i += 2 {
   135  		key, ok := values[i].(string)
   136  		if !ok {
   137  			return nil, errors.New("dict keys must be strings")
   138  		}
   139  		dict[key] = values[i+1]
   140  	}
   141  	return dict, nil
   142  }
   143  
   144  func IsNil(o interface{}) bool {
   145  	if o == nil {
   146  		return true
   147  	}
   148  	return IsNilReflect(reflect.ValueOf(o))
   149  }
   150  
   151  func IsNilReflect(v reflect.Value) bool {
   152  	switch v.Kind() {
   153  	case reflect.Pointer, reflect.UnsafePointer:
   154  		fallthrough
   155  	case reflect.Chan, reflect.Func, reflect.Map:
   156  		fallthrough
   157  	case reflect.Interface, reflect.Slice:
   158  		return v.IsNil()
   159  	default:
   160  	}
   161  	return false
   162  }
   163  
   164  // IsMap checks if some variable is a map
   165  func IsMap(o interface{}) bool {
   166  	if o == nil {
   167  		return false
   168  	}
   169  	return reflect.ValueOf(o).Kind() == reflect.Map
   170  }
   171  
   172  // IsSlice checks if some variable is a slice
   173  func IsSlice(o interface{}) bool {
   174  	if o == nil {
   175  		return false
   176  	}
   177  	return reflect.ValueOf(o).Kind() == reflect.Slice
   178  }