-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeval.go
82 lines (70 loc) · 1.55 KB
/
geval.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package geval
import (
"context"
"fmt"
"runtime"
)
type Params struct {
Err error
Success func(ctx *Context)
Failed func(ctx *Context)
Handler func(ctx *Context) error
Panic func(v any) error
Context *Context
Verbose bool
}
type Context struct {
Context context.Context
CancelFunc context.CancelFunc
Channel chan interface{}
}
func (cc *Context) Update(key interface{}, val interface{}) {
cc.Context = context.WithValue(cc.Context, key, val)
}
func (cc *Context) Read(key interface{}) interface{} {
return (cc.Context).Value(key)
}
func CreateContext() *Context {
ctx, cancel := context.WithCancel(context.Background())
return &Context{
Context: ctx,
CancelFunc: cancel,
Channel: make(chan interface{}),
}
}
func Run(params *Params) {
var err error = params.Err
var template string
var ctx = params.Context
if (ctx) == nil {
panic("Cannot Run wihout Context")
}
_, file, line, ok := runtime.Caller(1)
if ok {
template = fmt.Sprintf("%s:%d %%s\n", file, line)
}
if params.Err == nil && params.Handler != nil {
err = params.Handler(ctx)
}
if err != nil {
if params.Panic != nil {
if params.Verbose {
fmt.Printf("[VERBOSE] %s", fmt.Sprintf(template, "(Panic)"))
}
panic(params.Panic(err))
}
if params.Failed != nil {
if params.Verbose {
fmt.Printf("[VERBOSE] %s", fmt.Sprintf(template, "(Failed)"))
}
params.Failed(ctx)
}
} else {
if params.Success != nil {
if params.Verbose {
fmt.Printf("[VERBOSE] %s", fmt.Sprintf(template, "(Success)"))
}
params.Success(ctx)
}
}
}