gitee.com/go-spring2/spring-base@v1.1.3/cast/uint.go (about)

     1  /*
     2   * Copyright 2012-2019 the original author or authors.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *      https://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package cast
    18  
    19  import (
    20  	"fmt"
    21  	"strconv"
    22  )
    23  
    24  func UintPtr(s uint) *uint       { return &s }
    25  func Uint8Ptr(s uint8) *uint8    { return &s }
    26  func Uint16Ptr(s uint16) *uint16 { return &s }
    27  func Uint32Ptr(s uint32) *uint32 { return &s }
    28  func Uint64Ptr(s uint64) *uint64 { return &s }
    29  
    30  // ToUint casts an interface{} to an uint.
    31  // When type is clear, it is recommended to use standard library functions.
    32  func ToUint(i interface{}) uint {
    33  	v, _ := ToUint64E(i)
    34  	return uint(v)
    35  }
    36  
    37  // ToUint8 casts an interface{} to an uint8.
    38  // When type is clear, it is recommended to use standard library functions.
    39  func ToUint8(i interface{}) uint8 {
    40  	v, _ := ToUint64E(i)
    41  	return uint8(v)
    42  }
    43  
    44  // ToUint16 casts an interface{} to an uint16.
    45  // When type is clear, it is recommended to use standard library functions.
    46  func ToUint16(i interface{}) uint16 {
    47  	v, _ := ToUint64E(i)
    48  	return uint16(v)
    49  }
    50  
    51  // ToUint32 casts an interface{} to an uint32.
    52  // When type is clear, it is recommended to use standard library functions.
    53  func ToUint32(i interface{}) uint32 {
    54  	v, _ := ToUint64E(i)
    55  	return uint32(v)
    56  }
    57  
    58  // ToUint64 casts an interface{} to an uint64.
    59  // When type is clear, it is recommended to use standard library functions.
    60  func ToUint64(i interface{}) uint64 {
    61  	v, _ := ToUint64E(i)
    62  	return v
    63  }
    64  
    65  // ToUint64E casts an interface{} to an uint64.
    66  // When type is clear, it is recommended to use standard library functions.
    67  func ToUint64E(i interface{}) (uint64, error) {
    68  	switch s := i.(type) {
    69  	case nil:
    70  		return 0, nil
    71  	case int:
    72  		return uint64(s), nil
    73  	case int8:
    74  		return uint64(s), nil
    75  	case int16:
    76  		return uint64(s), nil
    77  	case int32:
    78  		return uint64(s), nil
    79  	case int64:
    80  		return uint64(s), nil
    81  	case *int:
    82  		return uint64(*s), nil
    83  	case *int8:
    84  		return uint64(*s), nil
    85  	case *int16:
    86  		return uint64(*s), nil
    87  	case *int32:
    88  		return uint64(*s), nil
    89  	case *int64:
    90  		return uint64(*s), nil
    91  	case uint:
    92  		return uint64(s), nil
    93  	case uint8:
    94  		return uint64(s), nil
    95  	case uint16:
    96  		return uint64(s), nil
    97  	case uint32:
    98  		return uint64(s), nil
    99  	case uint64:
   100  		return s, nil
   101  	case *uint:
   102  		return uint64(*s), nil
   103  	case *uint8:
   104  		return uint64(*s), nil
   105  	case *uint16:
   106  		return uint64(*s), nil
   107  	case *uint32:
   108  		return uint64(*s), nil
   109  	case *uint64:
   110  		return *s, nil
   111  	case float32:
   112  		return uint64(s), nil
   113  	case float64:
   114  		return uint64(s), nil
   115  	case *float32:
   116  		return uint64(*s), nil
   117  	case *float64:
   118  		return uint64(*s), nil
   119  	case string:
   120  		return strconv.ParseUint(s, 0, 0)
   121  	case *string:
   122  		return strconv.ParseUint(*s, 0, 0)
   123  	case bool:
   124  		if s {
   125  			return 1, nil
   126  		}
   127  		return 0, nil
   128  	case *bool:
   129  		if *s {
   130  			return 1, nil
   131  		}
   132  		return 0, nil
   133  	}
   134  	return 0, fmt.Errorf("unable to cast type (%T) to uint64", i)
   135  }