From 3e7c78a549dcfb4d49064693d10a7c2e346cd782 Mon Sep 17 00:00:00 2001 From: qydysky Date: Wed, 20 Nov 2024 01:40:52 +0800 Subject: [PATCH] 1 --- component2/comp.go | 80 ++++++++++++++++++++++++++++++++++++++--- component2/comp_test.go | 26 ++++++-------- errors/errors.go | 23 +++++++++--- 3 files changed, 104 insertions(+), 25 deletions(-) diff --git a/component2/comp.go b/component2/comp.go index f91b794..5d150e6 100644 --- a/component2/comp.go +++ b/component2/comp.go @@ -1,6 +1,7 @@ package component2 import ( + "os" "runtime" "strings" @@ -34,13 +35,82 @@ func Register[TargetInterface any](id string, _interface TargetInterface) error return nil } -func Get[TargetInterface any](id string, init ...func(TargetInterface) TargetInterface) (_interface TargetInterface) { - if tmp, ok := pkgInterfaceMap[id].(TargetInterface); ok { - for i := 0; i < len(init); i++ { - tmp = init[i](tmp) +func Get[TargetInterface any](id string, prefunc ...PreFunc[TargetInterface]) (_interface TargetInterface) { + if len(prefunc) == 0 { + prefunc = append(prefunc, PreFuncErr[TargetInterface]{}) + } + if o, ok := pkgInterfaceMap[id]; !ok { + for i := 0; i < len(prefunc); i++ { + prefunc[i].ErrNoFound(id) + } + } else if tmp, ok := o.(TargetInterface); ok { + for i := 0; i < len(prefunc); i++ { + tmp = prefunc[i].Init(tmp) } return tmp } else { - panic(ErrGet.WithReason("ErrGet:" + id)) + for i := 0; i < len(prefunc); i++ { + prefunc[i].ErrTypeAssertion(id) + } + } + return *new(TargetInterface) +} + +type PreFunc[TargetInterface any] interface { + Init(TargetInterface) TargetInterface + ErrNoFound(id string) + ErrTypeAssertion(id string) +} + +type PreFuncPanic[TargetInterface any] struct{} + +func (PreFuncPanic[TargetInterface]) Init(s TargetInterface) TargetInterface { + return s +} + +func (PreFuncPanic[TargetInterface]) ErrNoFound(id string) { + panic(ErrGet.WithReason("ErrNoFound")) +} + +func (PreFuncPanic[TargetInterface]) ErrTypeAssertion(id string) { + panic(ErrGet.WithReason("ErrTypeAssertion")) +} + +type PreFuncErr[TargetInterface any] struct{} + +func (PreFuncErr[TargetInterface]) Init(s TargetInterface) TargetInterface { + return s +} + +func (PreFuncErr[TargetInterface]) ErrNoFound(id string) { + os.Stderr.WriteString(perrors.ErrorFormat(ErrGet.WithReason("ErrNoFound "+id), perrors.ErrActionSimplifyFunc)) +} + +func (PreFuncErr[TargetInterface]) ErrTypeAssertion(id string) { + os.Stderr.WriteString(perrors.ErrorFormat(ErrGet.WithReason("ErrTypeAssertion "+id), perrors.ErrActionSimplifyFunc)) +} + +type PreFuncCu[TargetInterface any] struct { + Initf func(TargetInterface) TargetInterface + ErrNoFoundf func(id string) + ErrTypeAssertionf func(id string) +} + +func (t PreFuncCu[TargetInterface]) Init(s TargetInterface) TargetInterface { + if t.Initf != nil { + return t.Initf(s) + } + return s +} + +func (t PreFuncCu[TargetInterface]) ErrNoFound(id string) { + if t.ErrNoFoundf != nil { + t.ErrNoFoundf(id) + } +} + +func (t PreFuncCu[TargetInterface]) ErrTypeAssertion(id string) { + if t.ErrTypeAssertionf != nil { + t.ErrTypeAssertionf(id) } } diff --git a/component2/comp_test.go b/component2/comp_test.go index 8d69e8f..759f47e 100644 --- a/component2/comp_test.go +++ b/component2/comp_test.go @@ -6,28 +6,22 @@ import ( type B struct{} -func (b B) AddOne(a int) int { - return a + 1 +func (b B) AddOne(any) int { + return 2 } -func init() { - if e := Register[a]("github.com/qydysky/part/component2.aa", B{}); e != nil { +func Test(t *testing.T) { + if e := Register[interface { + AddOne(any) int + }]("aa", B{}); e != nil { panic(e) } - aa = Get[a](pkgid) -} - -type a interface { - AddOne(int) int -} - -// or var aa = Get[a](pkgid) -var aa a -var pkgid = PkgId("aa") + aa := Get[interface { + AddOne(any) int + }]("aa") -func Test(t *testing.T) { - if aa.AddOne(1) != 2 { + if aa.AddOne(func(i int) int { return i + 1 }) != 2 { t.Fatal() } } diff --git a/errors/errors.go b/errors/errors.go index 44a6efe..1a41de6 100644 --- a/errors/errors.go +++ b/errors/errors.go @@ -93,11 +93,12 @@ func ErrorFormat(e error, format ...ErrFormat) (s string) { if se, ok := e.(interface { Unwrap() []error }); ok { - for _, v := range se.Unwrap() { + es := se.Unwrap() + for i := 0; i < len(es); i++ { if len(format) > 0 { - s += format[0](v) + s += format[0](es[i]) } else { - s += v.Error() + "\n" + s += es[i].Error() + "\n" } } } else if len(format) > 0 { @@ -115,7 +116,21 @@ var ( ErrSimplifyFunc = func(e error) string { return e.Error() + "\n" } + ErrActionSimplifyFunc = func(e error) string { + if err, ok := e.(Error); ok { + return err.action + ":" + e.Error() + "\n" + } else { + return e.Error() + "\n" + } + } ErrInLineFunc = func(e error) string { - return " > " + e.Error() + return "> " + e.Error() + } + ErrActionInLineFunc = func(e error) string { + if err, ok := e.(Error); ok { + return "> " + err.action + ":" + e.Error() + } else { + return "> " + e.Error() + } } ) -- 2.39.2