]> 127.0.0.1 Git - part/.git/commitdiff
1 v0.28.20240417144109
authorqydysky <qydysky@foxmail.com>
Wed, 17 Apr 2024 14:35:57 +0000 (14:35 +0000)
committerqydysky <qydysky@foxmail.com>
Wed, 17 Apr 2024 14:35:57 +0000 (14:35 +0000)
.github/workflows/test.yml
component2/comp.go [new file with mode: 0644]
component2/comp_test.go [new file with mode: 0644]

index 6bbdcc18bf45535355ad400f99f717c0c7e27096..5386514e866986e30abc23c94b5b0d1932d16982 100644 (file)
@@ -38,6 +38,7 @@ jobs:
         go test -count 1 -timeout 10s -v -race github.com/qydysky/part/sql
         go test -count 1 -timeout 10s -v -race github.com/qydysky/part/rpc
         go test -count 1 -timeout 5s -v -race github.com/qydysky/part/component
+        go test -count 1 -timeout 5s -v -race github.com/qydysky/part/component2
         go test -count 1 -timeout 15s -v -race github.com/qydysky/part/ctx
         go test -count 1 -timeout 5s -v -race github.com/qydysky/part/slice
         go test -count 1 -timeout 5s -v -race github.com/qydysky/part/bools
@@ -74,6 +75,7 @@ jobs:
         go test -count 1 -timeout 10s -v -race github.com/qydysky/part/sql
         go test -count 1 -timeout 10s -v -race github.com/qydysky/part/rpc
         go test -count 1 -timeout 5s -v -race github.com/qydysky/part/component
+        go test -count 1 -timeout 5s -v -race github.com/qydysky/part/component2
         go test -count 1 -timeout 15s -v -race github.com/qydysky/part/ctx
         go test -count 1 -timeout 5s -v -race github.com/qydysky/part/slice
         go test -count 1 -timeout 5s -v -race github.com/qydysky/part/bools
@@ -110,6 +112,7 @@ jobs:
         go test -count 1 -timeout 10s -v -race github.com/qydysky/part/sql
         go test -count 1 -timeout 10s -v -race github.com/qydysky/part/rpc
         go test -count 1 -timeout 5s -v -race github.com/qydysky/part/component
+        go test -count 1 -timeout 5s -v -race github.com/qydysky/part/component2
         go test -count 1 -timeout 15s -v -race github.com/qydysky/part/ctx
         go test -count 1 -timeout 5s -v -race github.com/qydysky/part/slice
         go test -count 1 -timeout 5s -v -race github.com/qydysky/part/bools
diff --git a/component2/comp.go b/component2/comp.go
new file mode 100644 (file)
index 0000000..3231da9
--- /dev/null
@@ -0,0 +1,43 @@
+package component2
+
+import (
+       "errors"
+       "runtime"
+       "strings"
+)
+
+var pkgInterfaceMap = make(map[string]any)
+
+var (
+       ErrEmptyPkgId = errors.New("ErrEmptyPkgId")
+       ErrRegistered = errors.New("ErrRegistered")
+)
+
+func PkgId() string {
+       if pc, _, _, ok := runtime.Caller(1); ok {
+               return strings.TrimSuffix(runtime.FuncForPC(pc).Name(), ".init")
+       }
+       return ""
+}
+
+func Register[TargetInterface any](pkgId string, _interface TargetInterface) error {
+       if pkgId == "" {
+               return ErrEmptyPkgId
+       }
+       if _interfaceReg, ok := pkgInterfaceMap[pkgId]; ok && _interfaceReg != nil {
+               return ErrRegistered
+       } else {
+               pkgInterfaceMap[pkgId] = _interface
+       }
+       return nil
+}
+
+func Get[TargetInterface any](pkgId string, defaultInterface ...TargetInterface) (_interface TargetInterface) {
+       if tmp, ok := pkgInterfaceMap[pkgId].(TargetInterface); ok {
+               return tmp
+       }
+       if len(defaultInterface) > 0 {
+               return defaultInterface[0]
+       }
+       return
+}
diff --git a/component2/comp_test.go b/component2/comp_test.go
new file mode 100644 (file)
index 0000000..65f1fa2
--- /dev/null
@@ -0,0 +1,33 @@
+package component2
+
+import (
+       "testing"
+)
+
+type B struct{}
+
+func (b B) AddOne(a int) int {
+       return a + 1
+}
+
+func init() {
+       if e := Register[a]("github.com/qydysky/part/component2", 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()
+
+func Test(t *testing.T) {
+       if aa.AddOne(1) != 2 {
+               t.Fatal()
+       }
+}