mirror of
https://github.com/Kyren223/eko.git
synced 2026-08-28 10:21:31 +00:00
refactor: finished refactoring server-side code
This commit is contained in:
@@ -1,25 +1,94 @@
|
||||
package assert
|
||||
|
||||
import "log"
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"reflect"
|
||||
"runtime/debug"
|
||||
"sync"
|
||||
)
|
||||
|
||||
func Assert(assertion bool, message string, a ...any) {
|
||||
var (
|
||||
writer io.Writer = os.Stderr
|
||||
|
||||
flushes = []io.Closer{}
|
||||
flushMu sync.Mutex
|
||||
|
||||
assertData = map[string]any{}
|
||||
mapMu sync.Mutex
|
||||
)
|
||||
|
||||
func AddData(key string, value any) {
|
||||
mapMu.Lock()
|
||||
assertData[key] = value
|
||||
mapMu.Unlock()
|
||||
}
|
||||
|
||||
func RemoveData(key string) {
|
||||
mapMu.Lock()
|
||||
delete(assertData, key)
|
||||
mapMu.Unlock()
|
||||
}
|
||||
|
||||
func AddFlush(flusher io.Closer) {
|
||||
flushMu.Lock()
|
||||
flushes = append(flushes, flusher)
|
||||
flushMu.Unlock()
|
||||
}
|
||||
|
||||
func SetWriter(w io.Writer) {
|
||||
writer = w
|
||||
}
|
||||
|
||||
func runAssert(message string, args ...any) {
|
||||
flushMu.Lock()
|
||||
for len(flushes) != 0 {
|
||||
flusher := flushes[len(flushes)-1]
|
||||
flusher.Close()
|
||||
flushes = flushes[:len(flushes)-1]
|
||||
}
|
||||
flushMu.Unlock()
|
||||
|
||||
values := []any{
|
||||
"msg", message,
|
||||
}
|
||||
values = append(values, args...)
|
||||
mapMu.Lock()
|
||||
for k, v := range assertData {
|
||||
values = append(values, k, v)
|
||||
}
|
||||
mapMu.Unlock()
|
||||
|
||||
fmt.Fprintf(writer, "ARGS: %+v\n", args)
|
||||
fmt.Fprintf(writer, "ASSERT\n")
|
||||
for i := 0; i < len(values); i += 2 {
|
||||
fmt.Fprintf(writer, " %s=%v\n", values[i], values[i+1])
|
||||
}
|
||||
fmt.Fprintln(writer, string(debug.Stack()))
|
||||
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
func Assert(assertion bool, message string, args ...any) {
|
||||
if !assertion {
|
||||
log.Fatalf(message+"\n", a...)
|
||||
runAssert(message, args...)
|
||||
}
|
||||
}
|
||||
|
||||
func NoError(err error, message string, a ...any) {
|
||||
func NoError(err error, message string, args ...any) {
|
||||
if err != nil {
|
||||
log.Fatalf(message+": "+err.Error()+"\n", a...)
|
||||
args = append(args, "error", err)
|
||||
runAssert(message, args...)
|
||||
}
|
||||
}
|
||||
|
||||
func Never(message string, a ...any) {
|
||||
log.Fatalf(message+"\n", a...)
|
||||
func Never(message string, args ...any) {
|
||||
runAssert(message, args...)
|
||||
}
|
||||
|
||||
func NotNil(value any, message string, a ...any) {
|
||||
if value == nil {
|
||||
log.Fatalf(message+"\n", a...)
|
||||
func NotNil(value any, message string, args ...any) {
|
||||
if value == nil || reflect.ValueOf(value).Kind() == reflect.Ptr && reflect.ValueOf(value).IsNil() {
|
||||
runAssert(message, args...)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user