github.com/zlyuancn/zstr@v0.0.0-20230412074414-14d6b645962f/any_to_str.go (about)

     1  /*
     2  -------------------------------------------------
     3     Author :       Zhang Fan
     4     date:         2020/7/17
     5     Description :
     6  -------------------------------------------------
     7  */
     8  
     9  package zstr
    10  
    11  import (
    12  	"fmt"
    13  	"reflect"
    14  	"strconv"
    15  	"strings"
    16  )
    17  
    18  // 任何值转字符串
    19  func AnyToStr(a interface{}, nilToEmpty ...bool) string {
    20  	return anyToString(a, nilToEmpty...)
    21  }
    22  
    23  // 任何值转字符串
    24  func anyToString(a interface{}, nilToEmpty ...bool) string {
    25  	switch v := a.(type) {
    26  
    27  	case nil:
    28  		if len(nilToEmpty) > 0 && nilToEmpty[0] {
    29  			return ""
    30  		}
    31  		return "nil"
    32  
    33  	case string:
    34  		return v
    35  	case []byte:
    36  		return *BytesToString(v)
    37  	case bool:
    38  		if v {
    39  			return "true"
    40  		}
    41  		return "false"
    42  
    43  	case int:
    44  		return strconv.FormatInt(int64(v), 10)
    45  	case int8:
    46  		return strconv.FormatInt(int64(v), 10)
    47  	case int16:
    48  		return strconv.FormatInt(int64(v), 10)
    49  	case int32:
    50  		return strconv.FormatInt(int64(v), 10)
    51  	case int64:
    52  		return strconv.FormatInt(v, 10)
    53  
    54  	case uint:
    55  		return strconv.FormatUint(uint64(v), 10)
    56  	case uint8:
    57  		return strconv.FormatUint(uint64(v), 10)
    58  	case uint16:
    59  		return strconv.FormatUint(uint64(v), 10)
    60  	case uint32:
    61  		return strconv.FormatUint(uint64(v), 10)
    62  	case uint64:
    63  		return strconv.FormatUint(v, 10)
    64  	}
    65  	return fmt.Sprint(a)
    66  }
    67  
    68  // 任何值转sql需要的字符串
    69  func AnyToSqlStr(a interface{}, str_crust ...bool) string {
    70  	return anyToSqlString(a, len(str_crust) > 0 && str_crust[0])
    71  }
    72  
    73  // 任何值转sql需要的字符串
    74  func anyToSqlString(a interface{}, str_crust bool) string {
    75  	switch v := a.(type) {
    76  
    77  	case nil:
    78  		return "null"
    79  
    80  	case string:
    81  		if str_crust {
    82  			return `'` + v + `'`
    83  		}
    84  		return v
    85  	case []byte:
    86  		if str_crust {
    87  			return `'` + *BytesToString(v) + `'`
    88  		}
    89  		return *BytesToString(v)
    90  	case bool:
    91  		if v {
    92  			return "true"
    93  		}
    94  		return "false"
    95  
    96  	case int:
    97  		return strconv.FormatInt(int64(v), 10)
    98  	case int8:
    99  		return strconv.FormatInt(int64(v), 10)
   100  	case int16:
   101  		return strconv.FormatInt(int64(v), 10)
   102  	case int32:
   103  		return strconv.FormatInt(int64(v), 10)
   104  	case int64:
   105  		return strconv.FormatInt(v, 10)
   106  
   107  	case uint:
   108  		return strconv.FormatUint(uint64(v), 10)
   109  	case uint8:
   110  		return strconv.FormatUint(uint64(v), 10)
   111  	case uint16:
   112  		return strconv.FormatUint(uint64(v), 10)
   113  	case uint32:
   114  		return strconv.FormatUint(uint64(v), 10)
   115  	case uint64:
   116  		return strconv.FormatUint(v, 10)
   117  	}
   118  
   119  	r_v := reflect.Indirect(reflect.ValueOf(a))
   120  	if r_v.Kind() != reflect.Slice && r_v.Kind() != reflect.Array {
   121  		return fmt.Sprint(a)
   122  	}
   123  
   124  	l := r_v.Len()
   125  	ss := make([]string, l)
   126  	for i := 0; i < l; i++ {
   127  		ss[i] = anyToSqlString(reflect.Indirect(r_v.Index(i)).Interface(), str_crust)
   128  	}
   129  	return `(` + strings.Join(ss, ", ") + `)`
   130  }