github.com/qxnw/lib4go@v0.0.0-20180426074627-c80c7e84b925/types/types.go (about)

     1  package types
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  	"strings"
     7  )
     8  
     9  //DecodeString 判断变量的值与指定相等时设置为另一个值,否则使用原值
    10  func DecodeString(def interface{}, a interface{}, b interface{}, e ...interface{}) string {
    11  	values := make([]interface{}, 0, len(e)+2)
    12  	values = append(values, a)
    13  	values = append(values, b)
    14  	values = append(values, e...)
    15  
    16  	for i := 0; i < len(values)-1; i = i + 2 {
    17  		if def == values[i] {
    18  			return fmt.Sprint(values[i+1])
    19  		}
    20  	}
    21  	if len(values)%2 == 1 {
    22  		return fmt.Sprint(values[len(values)-1])
    23  	}
    24  	if s, ok := def.(string); ok {
    25  		return s
    26  	}
    27  	return ""
    28  }
    29  
    30  //DecodeInt 判断变量的值与指定相等时设置为另一个值,否则使用原值
    31  func DecodeInt(def interface{}, a interface{}, b interface{}, e ...interface{}) int {
    32  	values := make([]interface{}, 0, len(e)+2)
    33  	values = append(values, a)
    34  	values = append(values, b)
    35  	values = append(values, e...)
    36  
    37  	for i := 0; i < len(values)-1; i = i + 2 {
    38  		if def == values[i] {
    39  			v, err := Convert2Int(values[i+1])
    40  			if err == nil {
    41  				return v
    42  			}
    43  		}
    44  	}
    45  	if len(values)%2 == 1 {
    46  		v, err := Convert2Int(values[len(values)-1])
    47  		if err == nil {
    48  			return v
    49  		}
    50  	}
    51  	if r, ok := def.(int); ok {
    52  		return r
    53  	}
    54  	return 0
    55  }
    56  
    57  //Convert2Int 转换为int类型
    58  func Convert2Int(i interface{}) (int, error) {
    59  	switch i.(type) {
    60  	case int:
    61  		return i.(int), nil
    62  	case string:
    63  		return strconv.Atoi(i.(string))
    64  	default:
    65  		return strconv.Atoi(fmt.Sprint(i))
    66  	}
    67  }
    68  
    69  func ToInt(i interface{}, def ...int) int {
    70  	v, err := Convert2Int(i)
    71  	if err != nil {
    72  		if len(def) > 0 {
    73  			return def[0]
    74  		}
    75  		return 0
    76  	}
    77  	return v
    78  }
    79  
    80  //IsEmpty 当前对像是否是字符串空
    81  func IsEmpty(v interface{}) bool {
    82  	if v == nil {
    83  		return true
    84  	}
    85  	if t, ok := v.(string); ok && len(t) == 0 {
    86  		return true
    87  	}
    88  	if t, ok := v.([]interface{}); ok && len(t) == 0 {
    89  		return true
    90  	}
    91  	return false
    92  }
    93  func GetString(i interface{}) string {
    94  	if i == nil {
    95  		return ""
    96  	}
    97  	switch i.(type) {
    98  	case []string:
    99  		return strings.Join(i.([]string), ";")
   100  	default:
   101  		return fmt.Sprint(i)
   102  	}
   103  }
   104  func IntContains(input []int, v int) bool {
   105  	for _, i := range input {
   106  		if i == v {
   107  			return true
   108  		}
   109  	}
   110  	return false
   111  }