代码拉取完成,页面将自动刷新
package sq
import (
"context"
"errors"
"log"
"reflect"
"strings"
)
type Hook struct {
db *DB
Errs []error
ctx context.Context
}
func NewHook(ctx context.Context, db *DB) *Hook {
return &Hook{
db: db,
ctx: ctx,
}
}
func (h *Hook) callMethod(methodName string, reflectValue reflect.Value) {
// Only get address from non-pointer
if reflectValue.CanAddr() && reflectValue.Kind() != reflect.Ptr {
reflectValue = reflectValue.Addr()
}
if methodValue := reflectValue.MethodByName(methodName); methodValue.IsValid() {
switch method := methodValue.Interface().(type) {
case func():
method()
case func() error:
h.Err(method())
case func(db *DB):
method(h.db)
case func(db *DB) error:
h.Err(method(h.db))
case func(ctx context.Context):
method(h.ctx)
case func(ctx context.Context) error:
h.Err(method(h.ctx))
case func(ctx context.Context, db *DB):
method(h.ctx, h.db)
case func(ctx context.Context, db *DB) error:
h.Err(method(h.ctx, h.db))
default:
log.Panicf("unsupported function %v", methodName)
}
}
}
// Err add error
func (h *Hook) Err(err error) {
if err != nil {
h.Errs = append(h.Errs, err)
}
}
// HasError has errors
func (h *Hook) HasError() bool {
return len(h.Errs) > 0
}
// Error format happened errors
func (h *Hook) Error() error {
var errs = make([]string, 0)
for _, e := range h.Errs {
errs = append(errs, e.Error())
}
return errors.New(strings.Join(errs, "; "))
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。