]> 127.0.0.1 Git - part/.git/commitdiff
add
authorqydysky <qydysky@foxmail.com>
Mon, 14 Aug 2023 07:19:22 +0000 (15:19 +0800)
committerqydysky <qydysky@foxmail.com>
Mon, 14 Aug 2023 07:19:22 +0000 (15:19 +0800)
component/Component.go
component/Component_test.go
component/testdata/comp.go [new file with mode: 0644]
component/testdata/comp_test.go [new file with mode: 0644]
component/testdata/eg/eg.go [new file with mode: 0644]

index e6775e36d4860c2803dc4c4337241deec5f1df53..76f9ca452f12006add82cb39dbc538704d77fc86 100644 (file)
@@ -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
 }
index b4b5c6a91f93d781e6cd23c2dd3e46ce67becc7a..c6fa10e15eff83cf016c98c31241f8f8c416bb17 100644 (file)
@@ -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 (file)
index 0000000..857f046
--- /dev/null
@@ -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 (file)
index 0000000..55eba5f
--- /dev/null
@@ -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 (file)
index 0000000..d5764df
--- /dev/null
@@ -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
+}