package component2
import (
+ "os"
"runtime"
"strings"
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)
}
}
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()
}
}
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 {
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()
+ }
}
)