github.com/go-spring/spring-base@v1.1.3/cast/int.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 IntPtr(s int) *int       { return &s }
    25  func Int8Ptr(s int8) *int8    { return &s }
    26  func Int16Ptr(s int16) *int16 { return &s }
    27  func Int32Ptr(s int32) *int32 { return &s }
    28  func Int64Ptr(s int64) *int64 { return &s }
    29  
    30  // ToInt casts an interface{} to an int.
    31  // When type is clear, it is recommended to use standard library functions.
    32  func ToInt(i interface{}) int {
    33  	v, _ := ToInt64E(i)
    34  	return int(v)
    35  }
    36  
    37  // ToInt8 casts an interface{} to an int8.
    38  // When type is clear, it is recommended to use standard library functions.
    39  func ToInt8(i interface{}) int8 {
    40  	v, _ := ToInt64E(i)
    41  	return int8(v)
    42  }
    43  
    44  // ToInt16 casts an interface{} to an int16.
    45  // When type is clear, it is recommended to use standard library functions.
    46  func ToInt16(i interface{}) int16 {
    47  	v, _ := ToInt64E(i)
    48  	return int16(v)
    49  }
    50  
    51  // ToInt32 casts an interface{} to an int32.
    52  // When type is clear, it is recommended to use standard library functions.
    53  func ToInt32(i interface{}) int32 {
    54  	v, _ := ToInt64E(i)
    55  	return int32(v)
    56  }
    57  
    58  // ToInt64 casts an interface{} to an int64.
    59  // When type is clear, it is recommended to use standard library functions.
    60  func ToInt64(i interface{}) int64 {
    61  	v, _ := ToInt64E(i)
    62  	return v
    63  }
    64  
    65  // ToInt64E casts an interface{} to an int64.
    66  // When type is clear, it is recommended to use standard library functions.
    67  func ToInt64E(i interface{}) (int64, error) {
    68  	switch s := i.(type) {
    69  	case nil:
    70  		return 0, nil
    71  	case int:
    72  		return int64(s), nil
    73  	case int8:
    74  		return int64(s), nil
    75  	case int16:
    76  		return int64(s), nil
    77  	case int32:
    78  		return int64(s), nil
    79  	case int64:
    80  		return s, nil
    81  	case *int:
    82  		return int64(*s), nil
    83  	case *int8:
    84  		return int64(*s), nil
    85  	case *int16:
    86  		return int64(*s), nil
    87  	case *int32:
    88  		return int64(*s), nil
    89  	case *int64:
    90  		return *s, nil
    91  	case uint:
    92  		return int64(s), nil
    93  	case uint8:
    94  		return int64(s), nil
    95  	case uint16:
    96  		return int64(s), nil
    97  	case uint32:
    98  		return int64(s), nil
    99  	case uint64:
   100  		return int64(s), nil
   101  	case *uint:
   102  		return int64(*s), nil
   103  	case *uint8:
   104  		return int64(*s), nil
   105  	case *uint16:
   106  		return int64(*s), nil
   107  	case *uint32:
   108  		return int64(*s), nil
   109  	case *uint64:
   110  		return int64(*s), nil
   111  	case float32:
   112  		return int64(s), nil
   113  	case float64:
   114  		return int64(s), nil
   115  	case *float32:
   116  		return int64(*s), nil
   117  	case *float64:
   118  		return int64(*s), nil
   119  	case string:
   120  		return strconv.ParseInt(s, 0, 0)
   121  	case *string:
   122  		return strconv.ParseInt(*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 int64", i)
   135  }