package comfunc
import (
"fmt"
"reflect"
"strconv"
"strings"
"time"
"github.com/gookit/goutil/comdef"
"github.com/gookit/goutil/internal/checkfn"
)
func Bool (v any ) bool {
bl , _ := ToBool (v )
return bl
}
func ToBool (v any ) (bool , error ) {
if bl , ok := v .(bool ); ok {
return bl , nil
}
if str , ok := v .(string ); ok {
return StrToBool (str )
}
return false , comdef .ErrConvType
}
func StrToBool (s string ) (bool , error ) {
lower := strings .ToLower (s )
switch lower {
case "1" , "on" , "yes" , "true" :
return true , nil
case "0" , "off" , "no" , "false" :
return false , nil
}
return false , fmt .Errorf ("'%s' cannot convert to bool" , s )
}
func FormatWithArgs (fmtAndArgs []any ) string {
ln := len (fmtAndArgs )
if ln == 0 {
return ""
}
first := fmtAndArgs [0 ]
if ln == 1 {
if str , ok := first .(string ); ok {
return str
}
return fmt .Sprintf ("%+v" , first )
}
if tplStr , ok := first .(string ); ok && strings .IndexByte (tplStr , '%' ) >= 0 {
return fmt .Sprintf (tplStr , fmtAndArgs [1 :]...)
}
return fmt .Sprint (fmtAndArgs ...)
}
type ConvOption struct {
NilAsFail bool
HandlePtr bool
UserConvFn comdef .ToStringFunc
}
type ConvOptionFn func (opt *ConvOption )
var StrBySprintFn = func (v any ) (string , error ) {
return fmt .Sprint (v ), nil
}
func WithUserConvFn (fn comdef .ToStringFunc ) ConvOptionFn {
return func (opt *ConvOption ) {
opt .UserConvFn = fn
}
}
func NewConvOption (optFns ...ConvOptionFn ) *ConvOption {
opt := &ConvOption {}
opt .WithOption (optFns ...)
return opt
}
func (opt *ConvOption ) WithOption (optFns ...ConvOptionFn ) {
for _ , fn := range optFns {
if fn != nil {
fn (opt )
}
}
}
func ToStringWith (in any , optFns ...ConvOptionFn ) (str string , err error ) {
switch value := in .(type ) {
case int :
str = strconv .Itoa (value )
case int8 :
str = strconv .Itoa (int (value ))
case int16 :
str = strconv .Itoa (int (value ))
case int32 :
str = strconv .Itoa (int (value ))
case int64 :
str = strconv .FormatInt (value , 10 )
case uint :
str = strconv .FormatUint (uint64 (value ), 10 )
case uint8 :
str = strconv .FormatUint (uint64 (value ), 10 )
case uint16 :
str = strconv .FormatUint (uint64 (value ), 10 )
case uint32 :
str = strconv .FormatUint (uint64 (value ), 10 )
case uint64 :
str = strconv .FormatUint (value , 10 )
case float32 :
str = strconv .FormatFloat (float64 (value ), 'f' , -1 , 32 )
case float64 :
str = strconv .FormatFloat (value , 'f' , -1 , 64 )
case bool :
str = strconv .FormatBool (value )
case string :
str = value
case *string :
str = *value
case []byte :
str = string (value )
case time .Duration :
str = strconv .FormatInt (int64 (value ), 10 )
case fmt .Stringer :
str = value .String ()
case error :
str = value .Error()
default :
if len (optFns ) == 0 && in == nil {
return "" , nil
}
opt := NewConvOption (optFns ...)
if in == nil {
if opt .NilAsFail {
err = comdef .ErrConvType
}
return
}
if opt .HandlePtr {
if rv := reflect .ValueOf (in ); rv .Kind () == reflect .Pointer {
rv = rv .Elem ()
if checkfn .IsSimpleKind (rv .Kind ()) {
return ToStringWith (rv .Interface (), optFns ...)
}
}
}
if opt .UserConvFn != nil {
str , err = opt .UserConvFn (in )
} else {
err = comdef .ErrConvType
}
}
return
}
The pages are generated with Golds v0.8.4 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .