From b95666f2df07b683ef28a07ba38025f5a84381ea Mon Sep 17 00:00:00 2001 From: qydysky Date: Mon, 14 Aug 2023 15:19:22 +0800 Subject: [PATCH] add --- component/Component.go | 9 +++++---- component/Component_test.go | 8 +++++--- component/testdata/comp.go | 16 ++++++++++++++++ component/testdata/comp_test.go | 15 +++++++++++++++ component/testdata/eg/eg.go | 20 ++++++++++++++++++++ 5 files changed, 61 insertions(+), 7 deletions(-) create mode 100644 component/testdata/comp.go create mode 100644 component/testdata/comp_test.go create mode 100644 component/testdata/eg/eg.go diff --git a/component/Component.go b/component/Component.go index e6775e3..76f9ca4 100644 --- a/component/Component.go +++ b/component/Component.go @@ -61,6 +61,7 @@ func (t *components) Run(key string, ctx context.Context, ptr any) error { func (t *components) Link(link map[string][]string) error { if t.loadLink.CompareAndSwap(false, true) { t.link = maps.Clone(link) + return nil } return ErrLinked } @@ -82,10 +83,10 @@ func Link(link map[string][]string) error { return Comp.Link(link) } -func PKG[T any](sign ...string) (pkg string) { - pkg = reflect.TypeOf(*new(T)).PkgPath() - for i := 0; i < len(sign); i++ { - pkg += "." + sign[i] +func Sign[T any](exsign ...string) (sign string) { + sign = reflect.TypeOf(*new(T)).PkgPath() + for i := 0; i < len(exsign); i++ { + sign += "." + exsign[i] } return } diff --git a/component/Component_test.go b/component/Component_test.go index b4b5c6a..c6fa10e 100644 --- a/component/Component_test.go +++ b/component/Component_test.go @@ -40,9 +40,11 @@ func Test1(t *testing.T) { return errors.New("1.2.1") }) - Comp.Link(map[string][]string{ + if e := Comp.Link(map[string][]string{ `1`: {`1`}, - }) + }); e != nil { + t.Fatal(e) + } var s = 3 if e := Comp.Run(`1`, context.Background(), &s); e != nil { @@ -105,7 +107,7 @@ func Test3(t *testing.T) { func Test4(t *testing.T) { type empty struct{} - if pkg := PKG[empty](`1`, `2`); pkg != `github.com/qydysky/part/component.1.2` { + if pkg := Sign[empty](`1`, `2`); pkg != `empty github.com/qydysky/part/component.1.2` { t.Fatal(pkg) } } diff --git a/component/testdata/comp.go b/component/testdata/comp.go new file mode 100644 index 0000000..857f046 --- /dev/null +++ b/component/testdata/comp.go @@ -0,0 +1,16 @@ +package testdata + +import ( + eg "github.com/qydysky/part/component/testdata/eg" + + comp "github.com/qydysky/part/component" +) + +func init() { + var linkMap = map[string][]string{ + `test`: { + comp.Sign[eg.Sign](), + }, + } + comp.Link(linkMap) +} diff --git a/component/testdata/comp_test.go b/component/testdata/comp_test.go new file mode 100644 index 0000000..55eba5f --- /dev/null +++ b/component/testdata/comp_test.go @@ -0,0 +1,15 @@ +package testdata + +import ( + "context" + "testing" + + comp "github.com/qydysky/part/component" +) + +func TestMain(t *testing.T) { + var s = "s" + if e := comp.Run[string](`test`, context.Background(), &s); e != nil { + t.Fatal(e) + } +} diff --git a/component/testdata/eg/eg.go b/component/testdata/eg/eg.go new file mode 100644 index 0000000..d5764df --- /dev/null +++ b/component/testdata/eg/eg.go @@ -0,0 +1,20 @@ +package eg + +import ( + "context" + + comp "github.com/qydysky/part/component" +) + +type Sign struct{} + +func init() { + if e := comp.Put[string](comp.Sign[Sign](), deal); e != nil { + panic(e) + } +} + +func deal(ctx context.Context, ptr *string) error { + println(*ptr) + return nil +} -- 2.39.2