github.com/team-ide/go-dialect@v1.9.20/dialect/util.go (about) 1 package dialect 2 3 import ( 4 "database/sql" 5 "encoding/json" 6 "errors" 7 "fmt" 8 "github.com/google/uuid" 9 "reflect" 10 "regexp" 11 "strconv" 12 "strings" 13 "time" 14 ) 15 16 func StringToInt(str string) (res int, err error) { 17 i64, err := StringToInt64(str) 18 if err != nil { 19 return 20 } 21 res = int(i64) 22 return 23 } 24 25 func StringToInt64(str string) (res int64, err error) { 26 if str == "null" { 27 return 28 } 29 res, err = strconv.ParseInt(str, 10, 64) 30 if err != nil { 31 return 32 } 33 return 34 } 35 func StringToUint64(str string) (res uint64, err error) { 36 if str == "null" { 37 return 38 } 39 res, err = strconv.ParseUint(str, 10, 64) 40 if err != nil { 41 return 42 } 43 return 44 } 45 46 // StringsIndex Returns the index position of the string val in array 47 func StringsIndex(array []string, val string) (index int) { 48 index = -1 49 for i := 0; i < len(array); i++ { 50 if array[i] == val { 51 index = i 52 return 53 } 54 } 55 return 56 } 57 58 // BytesIndex Returns the index position of the string val in array 59 func BytesIndex(array []byte, val byte) (index int) { 60 index = -1 61 for i := 0; i < len(array); i++ { 62 if array[i] == val { 63 index = i 64 return 65 } 66 } 67 return 68 } 69 70 func ReplaceStringByRegex(str, rule, replace string) string { 71 reg, err := regexp.Compile(rule) 72 if reg == nil || err != nil { 73 return str 74 } 75 return reg.ReplaceAllString(str, replace) 76 } 77 78 func GetStringValue(value interface{}) string { 79 if value == nil { 80 return "" 81 } 82 vOf := reflect.ValueOf(value) 83 if vOf.Kind() == reflect.Ptr { 84 if vOf.IsNil() { 85 return "" 86 } 87 return GetStringValue(vOf.Elem().Interface()) 88 } 89 switch v := value.(type) { 90 case int: 91 return strconv.FormatInt(int64(v), 10) 92 case uint: 93 return strconv.FormatInt(int64(v), 10) 94 case int8: 95 return strconv.FormatInt(int64(v), 10) 96 case uint8: 97 return strconv.FormatUint(uint64(v), 10) 98 case int16: 99 return strconv.FormatInt(int64(v), 10) 100 case uint16: 101 return strconv.FormatUint(uint64(v), 10) 102 case int32: 103 return strconv.FormatInt(int64(v), 10) 104 case uint32: 105 return strconv.FormatUint(uint64(v), 10) 106 case int64: 107 return strconv.FormatInt(v, 10) 108 case uint64: 109 return strconv.FormatUint(v, 10) 110 case float32: 111 return strconv.FormatFloat(float64(v), 'f', -1, 64) 112 case float64: 113 return strconv.FormatFloat(v, 'f', -1, 64) 114 case bool: 115 if v { 116 return "1" 117 } 118 return "0" 119 case time.Time: 120 if v.IsZero() { 121 return "" 122 } 123 return v.Format("2006-01-02 15:04:05") 124 case string: 125 return v 126 case []byte: 127 return string(v) 128 case sql.NullString: 129 return v.String 130 case []interface{}: 131 bs, _ := json.Marshal(v) 132 return string(bs) 133 default: 134 baseValue, isBaseType := GetBaseTypeValue(value) 135 if isBaseType { 136 return GetStringValue(baseValue) 137 } 138 err := errors.New("value type [" + reflect.TypeOf(value).String() + "] not support,value :" + fmt.Sprintf("%s", value)) 139 fmt.Println("GetStringValue error ", err) 140 panic(err) 141 } 142 return "" 143 } 144 145 func GetBaseTypeValue(data interface{}) (res interface{}, is bool) { 146 if data == nil { 147 return 148 } 149 switch v := data.(type) { 150 case string, int, int8, int16, int32, int64, float32, float64, bool, uint, uint8, uint16, uint32, uint64: 151 res = v 152 is = true 153 return 154 } 155 dataValue := reflect.ValueOf(data) 156 if dataValue.Kind() == reflect.Ptr { 157 if dataValue.IsNil() { 158 return 159 } 160 return GetBaseTypeValue(dataValue.Elem().Interface()) 161 } 162 163 is = true 164 kindName := reflect.TypeOf(data).Kind().String() 165 //fmt.Println("kindName:", kindName) 166 switch kindName { 167 case "string": 168 res = dataValue.String() 169 break 170 case "int": 171 res = int(dataValue.Int()) 172 break 173 case "int8": 174 res = int8(dataValue.Int()) 175 break 176 case "int16": 177 res = int16(dataValue.Int()) 178 break 179 case "int32": 180 res = int32(dataValue.Int()) 181 break 182 case "int64": 183 res = dataValue.Int() 184 break 185 case "float32": 186 res = float32(dataValue.Float()) 187 break 188 case "float64": 189 res = dataValue.Float() 190 break 191 case "bool": 192 res = dataValue.Bool() 193 break 194 case "[]bytes": 195 res = dataValue.Bytes() 196 break 197 case "uint": 198 res = uint(dataValue.Uint()) 199 break 200 case "uint8": 201 res = uint8(dataValue.Uint()) 202 break 203 case "uint16": 204 res = uint16(dataValue.Uint()) 205 break 206 case "uint32": 207 res = uint32(dataValue.Uint()) 208 break 209 case "uint64": 210 res = dataValue.Uint() 211 break 212 default: 213 is = false 214 res = dataValue.Interface() 215 break 216 } 217 return 218 } 219 220 // UUID 生成UUID 221 func UUID() (res string) { 222 res = uuid.NewString() 223 res = strings.ReplaceAll(res, "-", "") 224 return 225 }