Skip to content

Commit

Permalink
add util functions
Browse files Browse the repository at this point in the history
  • Loading branch information
forbearing committed Apr 5, 2024
1 parent 549b13b commit 0fca653
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ package util

import (
"bytes"
"fmt"
"os"
"reflect"
"runtime"
"unsafe"

uuid "github.com/satori/go.uuid"
"github.com/segmentio/ksuid"
Expand Down Expand Up @@ -74,3 +78,59 @@ func SplitByDoublePipe(data []byte, atEOF bool) (advance int, token []byte, err
// If no delimiter is found, return no data and wait for more input
return 0, nil, nil
}

// RunOrDie will panic when error encountered.
func RunOrDie(fn func() error) {
if err := fn(); err != nil {
panic(err)
}
}

// HandleErr will call os.Exit() when any error encountered.
func HandleErr(err error, notExit ...bool) {
var flag bool
if len(notExit) != 0 {
flag = notExit[0]
}
if err != nil {
fmt.Println(err)
if !flag {
os.Exit(1)
}
}
}

// CheckErr just check error and print it.
func CheckErr(err error) {
HandleErr(err, true)
}

// StringAny format anything to string.
func StringAny(x any) string {
if x == nil {
return ""
}
if v, ok := x.(fmt.Stringer); ok {
return v.String()
}

switch v := x.(type) {
case []byte:
return *(*string)(unsafe.Pointer(&v))
case string:
return v
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
return fmt.Sprintf("%d", x)
default:
return fmt.Sprintf("%v", x)
}
}

func GetFunctionName(x any) string {
switch v := x.(type) {
case uintptr:
return runtime.FuncForPC(v).Name()
default:
return runtime.FuncForPC(reflect.ValueOf(x).Pointer()).Name()
}
}

0 comments on commit 0fca653

Please sign in to comment.