Skip to content

Commit

Permalink
feat(misc): convert given value to string
Browse files Browse the repository at this point in the history
Signed-off-by: Chin-Ya Huang <[email protected]>
  • Loading branch information
c3y1huang committed Apr 25, 2024
1 parent 18485c1 commit 651ed84
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions utils/misc.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package utils

import (
"crypto/rand"
"fmt"
"math/big"
"path/filepath"
"reflect"
"runtime"
"strconv"

"github.com/google/uuid"
"github.com/pkg/errors"
Expand Down Expand Up @@ -98,3 +100,21 @@ func GenerateRandomNumber(lower, upper int64) (int64, error) {
}
return (lower + randNum.Int64()), nil
}

// ConvertTypeToString converts the given value to string.
func ConvertTypeToString[T any](value T) string {
v := reflect.ValueOf(value)

switch v.Kind() {
case reflect.String:
return v.String()
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return strconv.FormatInt(v.Int(), 10)
case reflect.Float32, reflect.Float64:
return strconv.FormatFloat(v.Float(), 'f', -1, 64)
case reflect.Bool:
return strconv.FormatBool(v.Bool())
default:
return fmt.Sprintf("Unsupported type: %v", v.Kind())
}
}

0 comments on commit 651ed84

Please sign in to comment.