From bee11f02795f2bd88de73b0038ca90d9e3304ff7 Mon Sep 17 00:00:00 2001 From: qydysky Date: Wed, 17 Apr 2024 14:35:57 +0000 Subject: [PATCH] 1 --- .github/workflows/test.yml | 3 +++ component2/comp.go | 43 ++++++++++++++++++++++++++++++++++++++ component2/comp_test.go | 33 +++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+) create mode 100644 component2/comp.go create mode 100644 component2/comp_test.go diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 6bbdcc1..5386514 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -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 index 0000000..3231da9 --- /dev/null +++ b/component2/comp.go @@ -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 index 0000000..65f1fa2 --- /dev/null +++ b/component2/comp_test.go @@ -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() + } +} -- 2.39.2