github.com/gogf/gf/v2@v2.7.4/util/gconv/gconv_slice_int.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 gconv
     8  
     9  import (
    10  	"reflect"
    11  
    12  	"github.com/gogf/gf/v2/internal/json"
    13  	"github.com/gogf/gf/v2/internal/reflection"
    14  	"github.com/gogf/gf/v2/util/gconv/internal/localinterface"
    15  )
    16  
    17  // SliceInt is alias of Ints.
    18  func SliceInt(any interface{}) []int {
    19  	return Ints(any)
    20  }
    21  
    22  // SliceInt32 is alias of Int32s.
    23  func SliceInt32(any interface{}) []int32 {
    24  	return Int32s(any)
    25  }
    26  
    27  // SliceInt64 is alias of Int64s.
    28  func SliceInt64(any interface{}) []int64 {
    29  	return Int64s(any)
    30  }
    31  
    32  // Ints converts `any` to []int.
    33  func Ints(any interface{}) []int {
    34  	if any == nil {
    35  		return nil
    36  	}
    37  	var (
    38  		array []int = nil
    39  	)
    40  	switch value := any.(type) {
    41  	case []string:
    42  		array = make([]int, len(value))
    43  		for k, v := range value {
    44  			array[k] = Int(v)
    45  		}
    46  	case []int:
    47  		array = value
    48  	case []int8:
    49  		array = make([]int, len(value))
    50  		for k, v := range value {
    51  			array[k] = int(v)
    52  		}
    53  	case []int16:
    54  		array = make([]int, len(value))
    55  		for k, v := range value {
    56  			array[k] = int(v)
    57  		}
    58  	case []int32:
    59  		array = make([]int, len(value))
    60  		for k, v := range value {
    61  			array[k] = int(v)
    62  		}
    63  	case []int64:
    64  		array = make([]int, len(value))
    65  		for k, v := range value {
    66  			array[k] = int(v)
    67  		}
    68  	case []uint:
    69  		array = make([]int, len(value))
    70  		for k, v := range value {
    71  			array[k] = int(v)
    72  		}
    73  	case []uint8:
    74  		if json.Valid(value) {
    75  			_ = json.UnmarshalUseNumber(value, &array)
    76  		} else {
    77  			array = make([]int, len(value))
    78  			for k, v := range value {
    79  				array[k] = int(v)
    80  			}
    81  		}
    82  	case []uint16:
    83  		array = make([]int, len(value))
    84  		for k, v := range value {
    85  			array[k] = int(v)
    86  		}
    87  	case []uint32:
    88  		array = make([]int, len(value))
    89  		for k, v := range value {
    90  			array[k] = int(v)
    91  		}
    92  	case []uint64:
    93  		array = make([]int, len(value))
    94  		for k, v := range value {
    95  			array[k] = int(v)
    96  		}
    97  	case []bool:
    98  		array = make([]int, len(value))
    99  		for k, v := range value {
   100  			if v {
   101  				array[k] = 1
   102  			} else {
   103  				array[k] = 0
   104  			}
   105  		}
   106  	case []float32:
   107  		array = make([]int, len(value))
   108  		for k, v := range value {
   109  			array[k] = Int(v)
   110  		}
   111  	case []float64:
   112  		array = make([]int, len(value))
   113  		for k, v := range value {
   114  			array[k] = Int(v)
   115  		}
   116  	case []interface{}:
   117  		array = make([]int, len(value))
   118  		for k, v := range value {
   119  			array[k] = Int(v)
   120  		}
   121  	case [][]byte:
   122  		array = make([]int, len(value))
   123  		for k, v := range value {
   124  			array[k] = Int(v)
   125  		}
   126  	}
   127  	if array != nil {
   128  		return array
   129  	}
   130  	if v, ok := any.(localinterface.IInts); ok {
   131  		return v.Ints()
   132  	}
   133  	if v, ok := any.(localinterface.IInterfaces); ok {
   134  		return Ints(v.Interfaces())
   135  	}
   136  	// JSON format string value converting.
   137  	if checkJsonAndUnmarshalUseNumber(any, &array) {
   138  		return array
   139  	}
   140  	// Not a common type, it then uses reflection for conversion.
   141  	originValueAndKind := reflection.OriginValueAndKind(any)
   142  	switch originValueAndKind.OriginKind {
   143  	case reflect.Slice, reflect.Array:
   144  		var (
   145  			length = originValueAndKind.OriginValue.Len()
   146  			slice  = make([]int, length)
   147  		)
   148  		for i := 0; i < length; i++ {
   149  			slice[i] = Int(originValueAndKind.OriginValue.Index(i).Interface())
   150  		}
   151  		return slice
   152  
   153  	default:
   154  		if originValueAndKind.OriginValue.IsZero() {
   155  			return []int{}
   156  		}
   157  		return []int{Int(any)}
   158  	}
   159  }
   160  
   161  // Int32s converts `any` to []int32.
   162  func Int32s(any interface{}) []int32 {
   163  	if any == nil {
   164  		return nil
   165  	}
   166  	var (
   167  		array []int32 = nil
   168  	)
   169  	switch value := any.(type) {
   170  	case []string:
   171  		array = make([]int32, len(value))
   172  		for k, v := range value {
   173  			array[k] = Int32(v)
   174  		}
   175  	case []int:
   176  		array = make([]int32, len(value))
   177  		for k, v := range value {
   178  			array[k] = int32(v)
   179  		}
   180  	case []int8:
   181  		array = make([]int32, len(value))
   182  		for k, v := range value {
   183  			array[k] = int32(v)
   184  		}
   185  	case []int16:
   186  		array = make([]int32, len(value))
   187  		for k, v := range value {
   188  			array[k] = int32(v)
   189  		}
   190  	case []int32:
   191  		array = value
   192  	case []int64:
   193  		array = make([]int32, len(value))
   194  		for k, v := range value {
   195  			array[k] = int32(v)
   196  		}
   197  	case []uint:
   198  		array = make([]int32, len(value))
   199  		for k, v := range value {
   200  			array[k] = int32(v)
   201  		}
   202  	case []uint8:
   203  		if json.Valid(value) {
   204  			_ = json.UnmarshalUseNumber(value, &array)
   205  		} else {
   206  			array = make([]int32, len(value))
   207  			for k, v := range value {
   208  				array[k] = int32(v)
   209  			}
   210  		}
   211  	case []uint16:
   212  		array = make([]int32, len(value))
   213  		for k, v := range value {
   214  			array[k] = int32(v)
   215  		}
   216  	case []uint32:
   217  		array = make([]int32, len(value))
   218  		for k, v := range value {
   219  			array[k] = int32(v)
   220  		}
   221  	case []uint64:
   222  		array = make([]int32, len(value))
   223  		for k, v := range value {
   224  			array[k] = int32(v)
   225  		}
   226  	case []bool:
   227  		array = make([]int32, len(value))
   228  		for k, v := range value {
   229  			if v {
   230  				array[k] = 1
   231  			} else {
   232  				array[k] = 0
   233  			}
   234  		}
   235  	case []float32:
   236  		array = make([]int32, len(value))
   237  		for k, v := range value {
   238  			array[k] = Int32(v)
   239  		}
   240  	case []float64:
   241  		array = make([]int32, len(value))
   242  		for k, v := range value {
   243  			array[k] = Int32(v)
   244  		}
   245  	case []interface{}:
   246  		array = make([]int32, len(value))
   247  		for k, v := range value {
   248  			array[k] = Int32(v)
   249  		}
   250  	case [][]byte:
   251  		array = make([]int32, len(value))
   252  		for k, v := range value {
   253  			array[k] = Int32(v)
   254  		}
   255  	}
   256  	if array != nil {
   257  		return array
   258  	}
   259  	if v, ok := any.(localinterface.IInts); ok {
   260  		return Int32s(v.Ints())
   261  	}
   262  	if v, ok := any.(localinterface.IInterfaces); ok {
   263  		return Int32s(v.Interfaces())
   264  	}
   265  	// JSON format string value converting.
   266  	if checkJsonAndUnmarshalUseNumber(any, &array) {
   267  		return array
   268  	}
   269  	// Not a common type, it then uses reflection for conversion.
   270  	originValueAndKind := reflection.OriginValueAndKind(any)
   271  	switch originValueAndKind.OriginKind {
   272  	case reflect.Slice, reflect.Array:
   273  		var (
   274  			length = originValueAndKind.OriginValue.Len()
   275  			slice  = make([]int32, length)
   276  		)
   277  		for i := 0; i < length; i++ {
   278  			slice[i] = Int32(originValueAndKind.OriginValue.Index(i).Interface())
   279  		}
   280  		return slice
   281  
   282  	default:
   283  		if originValueAndKind.OriginValue.IsZero() {
   284  			return []int32{}
   285  		}
   286  		return []int32{Int32(any)}
   287  	}
   288  }
   289  
   290  // Int64s converts `any` to []int64.
   291  func Int64s(any interface{}) []int64 {
   292  	if any == nil {
   293  		return nil
   294  	}
   295  	var (
   296  		array []int64 = nil
   297  	)
   298  	switch value := any.(type) {
   299  	case []string:
   300  		array = make([]int64, len(value))
   301  		for k, v := range value {
   302  			array[k] = Int64(v)
   303  		}
   304  	case []int:
   305  		array = make([]int64, len(value))
   306  		for k, v := range value {
   307  			array[k] = int64(v)
   308  		}
   309  	case []int8:
   310  		array = make([]int64, len(value))
   311  		for k, v := range value {
   312  			array[k] = int64(v)
   313  		}
   314  	case []int16:
   315  		array = make([]int64, len(value))
   316  		for k, v := range value {
   317  			array[k] = int64(v)
   318  		}
   319  	case []int32:
   320  		array = make([]int64, len(value))
   321  		for k, v := range value {
   322  			array[k] = int64(v)
   323  		}
   324  	case []int64:
   325  		array = value
   326  	case []uint:
   327  		array = make([]int64, len(value))
   328  		for k, v := range value {
   329  			array[k] = int64(v)
   330  		}
   331  	case []uint8:
   332  		if json.Valid(value) {
   333  			_ = json.UnmarshalUseNumber(value, &array)
   334  		} else {
   335  			array = make([]int64, len(value))
   336  			for k, v := range value {
   337  				array[k] = int64(v)
   338  			}
   339  		}
   340  	case []uint16:
   341  		array = make([]int64, len(value))
   342  		for k, v := range value {
   343  			array[k] = int64(v)
   344  		}
   345  	case []uint32:
   346  		array = make([]int64, len(value))
   347  		for k, v := range value {
   348  			array[k] = int64(v)
   349  		}
   350  	case []uint64:
   351  		array = make([]int64, len(value))
   352  		for k, v := range value {
   353  			array[k] = int64(v)
   354  		}
   355  	case []bool:
   356  		array = make([]int64, len(value))
   357  		for k, v := range value {
   358  			if v {
   359  				array[k] = 1
   360  			} else {
   361  				array[k] = 0
   362  			}
   363  		}
   364  	case []float32:
   365  		array = make([]int64, len(value))
   366  		for k, v := range value {
   367  			array[k] = Int64(v)
   368  		}
   369  	case []float64:
   370  		array = make([]int64, len(value))
   371  		for k, v := range value {
   372  			array[k] = Int64(v)
   373  		}
   374  	case []interface{}:
   375  		array = make([]int64, len(value))
   376  		for k, v := range value {
   377  			array[k] = Int64(v)
   378  		}
   379  	case [][]byte:
   380  		array = make([]int64, len(value))
   381  		for k, v := range value {
   382  			array[k] = Int64(v)
   383  		}
   384  	}
   385  	if array != nil {
   386  		return array
   387  	}
   388  	if v, ok := any.(localinterface.IInts); ok {
   389  		return Int64s(v.Ints())
   390  	}
   391  	if v, ok := any.(localinterface.IInterfaces); ok {
   392  		return Int64s(v.Interfaces())
   393  	}
   394  	// JSON format string value converting.
   395  	if checkJsonAndUnmarshalUseNumber(any, &array) {
   396  		return array
   397  	}
   398  	// Not a common type, it then uses reflection for conversion.
   399  	originValueAndKind := reflection.OriginValueAndKind(any)
   400  	switch originValueAndKind.OriginKind {
   401  	case reflect.Slice, reflect.Array:
   402  		var (
   403  			length = originValueAndKind.OriginValue.Len()
   404  			slice  = make([]int64, length)
   405  		)
   406  		for i := 0; i < length; i++ {
   407  			slice[i] = Int64(originValueAndKind.OriginValue.Index(i).Interface())
   408  		}
   409  		return slice
   410  
   411  	default:
   412  		if originValueAndKind.OriginValue.IsZero() {
   413  			return []int64{}
   414  		}
   415  		return []int64{Int64(any)}
   416  	}
   417  }