MCPcopy Index your code
hub / github.com/TruthHun/DocHub / execConvert

Function execConvert

helper/valid.go:356–386  ·  view source on GitHub ↗

数据类型转换 @param val 需要转换的字符串 @param rule 需要转换的类型,如"int" @return 返回转化后的数据类型 @return 返回错误

(val, rule string)

Source from the content-addressed store, hash-verified

354//@return 返回转化后的数据类型
355//@return 返回错误
356func execConvert(val, rule string) (interface{}, error) {
357 rule = strings.ToLower(rule)
358 switch rule {
359 case "int", "int32", "int8", "int64":
360 bit, _ := strconv.Atoi(strings.TrimPrefix(rule, "int"))
361 if bit == 0 {
362 bit = 32
363 }
364 num, err := strconv.ParseInt(val, 10, 64)
365 switch bit {
366 case 8:
367 return int8(num), err
368 case 32:
369 return int(num), err
370 default:
371 return num, err
372 }
373 //转化成浮点型
374 case "float", "float32", "float64":
375 bit := 32
376 if bit, _ := strconv.Atoi(strings.TrimPrefix(rule, "float")); bit == 0 {
377 bit = 32
378 }
379 num, err := strconv.ParseFloat(val, 64)
380 if bit == 32 {
381 return float32(num), err
382 }
383 return num, err
384 }
385 return val, nil
386}

Callers 1

ValidFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected