github.com/sohaha/zlsgo@v1.7.13-0.20240501141223-10dd1a906f76/ztype/slice.go (about)

     1  package ztype
     2  
     3  import (
     4  	"encoding/json"
     5  	"reflect"
     6  
     7  	"github.com/sohaha/zlsgo/zreflect"
     8  )
     9  
    10  type SliceType []Type
    11  
    12  func (s SliceType) Len() int {
    13  	return len(s)
    14  }
    15  
    16  func (s SliceType) MarshalJSON() ([]byte, error) {
    17  	return json.Marshal(s.Value())
    18  }
    19  
    20  func (s SliceType) Index(i int) Type {
    21  	if i < 0 || i >= len(s) {
    22  		return Type{}
    23  	}
    24  	return s[i]
    25  }
    26  
    27  func (s SliceType) Last() Type {
    28  	return s.Index(len(s) - 1)
    29  }
    30  
    31  func (s SliceType) First() Type {
    32  	return s.Index(0)
    33  }
    34  
    35  func (s SliceType) Value() []interface{} {
    36  	ss := make([]interface{}, 0, len(s))
    37  	for i := range s {
    38  		ss = append(ss, s[i].Value())
    39  	}
    40  	return ss
    41  }
    42  
    43  func (s SliceType) String() []string {
    44  	ss := make([]string, 0, len(s))
    45  	for i := range s {
    46  		ss = append(ss, s[i].String())
    47  	}
    48  	return ss
    49  }
    50  
    51  func (s SliceType) Int() []int {
    52  	ss := make([]int, 0, len(s))
    53  	for i := range s {
    54  		ss = append(ss, s[i].Int())
    55  	}
    56  	return ss
    57  }
    58  
    59  func (s SliceType) Maps() Maps {
    60  	ss := make(Maps, 0, len(s))
    61  	for i := range s {
    62  		ss = append(ss, s[i].Map())
    63  	}
    64  	return ss
    65  }
    66  
    67  // func (s SliceType) Slice() []SliceType {
    68  // 	ss := make([]SliceType, 0, len(s))
    69  // 	for i := range s {
    70  // 		ss = append(ss, s[i].Slice())
    71  // 	}
    72  // 	return ss
    73  // }
    74  
    75  // Deprecated: please use ToSlice
    76  func Slice(value interface{}, noConv ...bool) SliceType {
    77  	return ToSlice(value, noConv...)
    78  }
    79  
    80  // SliceStrToAny  []string to []interface{}
    81  func SliceStrToAny(slice []string) []interface{} {
    82  	ss := make([]interface{}, 0, len(slice))
    83  	for _, val := range slice {
    84  		ss = append(ss, val)
    85  	}
    86  	return ss
    87  }
    88  
    89  func ToSlice(value interface{}, noConv ...bool) (s SliceType) {
    90  	s = SliceType{}
    91  	if value == nil {
    92  		return
    93  	}
    94  	nc := len(noConv) > 0 && noConv[0]
    95  	switch val := value.(type) {
    96  	case []interface{}:
    97  		s = make(SliceType, len(val))
    98  		for i := range val {
    99  			s[i] = New(val[i])
   100  		}
   101  	case []string:
   102  		s = make(SliceType, len(val))
   103  		for i := range val {
   104  			s[i] = New(val[i])
   105  		}
   106  	case []int:
   107  		s = make(SliceType, len(val))
   108  		for i := range val {
   109  			s[i] = New(val[i])
   110  		}
   111  	case []int64:
   112  		s = make(SliceType, len(val))
   113  		for i := range val {
   114  			s[i] = New(val[i])
   115  		}
   116  	case string:
   117  		if nc {
   118  			return
   119  		}
   120  		var nval []interface{}
   121  		if err := json.Unmarshal([]byte(val), &nval); err == nil {
   122  			s = make(SliceType, len(nval))
   123  			for i := range nval {
   124  				s[i] = New(nval[i])
   125  			}
   126  			return
   127  		}
   128  		s = SliceType{New(val)}
   129  	default:
   130  		var nval []interface{}
   131  		vof := zreflect.ValueOf(&nval)
   132  		to := func() {
   133  			if conv.to("", value, vof) == nil {
   134  				s = make(SliceType, len(nval))
   135  				for i := range nval {
   136  					s[i] = New(nval[i])
   137  				}
   138  			}
   139  		}
   140  
   141  		switch vof.Type().Kind() {
   142  		case reflect.Slice:
   143  			to()
   144  		default:
   145  			if nc {
   146  				return
   147  			}
   148  			to()
   149  		}
   150  	}
   151  
   152  	return
   153  }