]> 127.0.0.1 Git - part/.git/commitdiff
1
authorqydysky <qydysky@foxmail.com>
Tue, 19 Nov 2024 17:40:52 +0000 (01:40 +0800)
committerqydysky <qydysky@foxmail.com>
Tue, 19 Nov 2024 17:40:52 +0000 (01:40 +0800)
component2/comp.go
component2/comp_test.go
errors/errors.go

index f91b79458c05a9a4d095cbf933e0bfb10a8fa380..5d150e64735eaf0239f264a2808eee1d22f3503f 100644 (file)
@@ -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)
        }
 }
index 8d69e8f30075a64c252109bbb3b317f43dc96e44..759f47e4d10d1b93af36150e01b6ce253bf4ff2c 100644 (file)
@@ -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()
        }
 }
index 44a6efe73dcbf109ef27d4527dbd16184d15f4f1..1a41de6f9e29035f3af2943bd2a2f8b614e9bf7f 100644 (file)
@@ -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()
+               }
        }
 )