woj-server/pkg/cast/cast.go

34 lines
617 B
Go
Raw Normal View History

2022-09-07 23:34:37 +08:00
package cast
import (
"fmt"
2023-07-14 21:47:11 +08:00
"git.0x7f.app/WOJ/woj-server/internal/e"
2022-09-07 23:34:37 +08:00
"strconv"
)
// ToString Only supports some primitives and internal types
func ToString(obj interface{}) string {
switch t := obj.(type) {
case bool:
return strconv.FormatBool(t)
case []byte:
return string(t)
2022-09-20 14:15:21 +08:00
case e.Status:
2022-09-07 23:34:37 +08:00
return t.String()
case error:
return t.Error()
case float32:
return strconv.FormatFloat(float64(t), 'f', -1, 32)
case float64:
return strconv.FormatFloat(t, 'f', -1, 64)
case int:
return strconv.Itoa(t)
case string:
return t
case nil:
return ""
default:
return fmt.Sprintf("%v", obj)
}
}