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