correcting hooks calling
All checks were successful
lint / lint (pull_request) Successful in 1m2s
test / test (pull_request) Successful in 2m18s

This commit is contained in:
Денис Евстигнеев 2024-12-17 15:35:04 +03:00
parent 19b04fe070
commit 58eec62419
8 changed files with 45 additions and 7 deletions

View File

@ -109,7 +109,7 @@ func (m *memoryBroker) Init(opts ...broker.Option) error {
m.funcSubscribe = m.fnSubscribe m.funcSubscribe = m.fnSubscribe
m.funcBatchSubscribe = m.fnBatchSubscribe m.funcBatchSubscribe = m.fnBatchSubscribe
m.opts.Hooks.EachNext(func(hook options.Hook) { m.opts.Hooks.EachPrev(func(hook options.Hook) {
switch h := hook.(type) { switch h := hook.(type) {
case broker.HookPublish: case broker.HookPublish:
m.funcPublish = h(m.funcPublish) m.funcPublish = h(m.funcPublish)

View File

@ -59,7 +59,7 @@ func (b *NoopBroker) Init(opts ...Option) error {
b.funcSubscribe = b.fnSubscribe b.funcSubscribe = b.fnSubscribe
b.funcBatchSubscribe = b.fnBatchSubscribe b.funcBatchSubscribe = b.fnBatchSubscribe
b.opts.Hooks.EachNext(func(hook options.Hook) { b.opts.Hooks.EachPrev(func(hook options.Hook) {
switch h := hook.(type) { switch h := hook.(type) {
case HookPublish: case HookPublish:
b.funcPublish = h(b.funcPublish) b.funcPublish = h(b.funcPublish)

View File

@ -194,7 +194,7 @@ func (n *noopClient) Init(opts ...Option) error {
n.funcPublish = n.fnPublish n.funcPublish = n.fnPublish
n.funcBatchPublish = n.fnBatchPublish n.funcBatchPublish = n.fnBatchPublish
n.opts.Hooks.EachNext(func(hook options.Hook) { n.opts.Hooks.EachPrev(func(hook options.Hook) {
switch h := hook.(type) { switch h := hook.(type) {
case HookCall: case HookCall:
n.funcCall = h(n.funcCall) n.funcCall = h(n.funcCall)

View File

@ -37,7 +37,7 @@ func (c *defaultConfig) Init(opts ...Option) error {
c.funcLoad = c.fnLoad c.funcLoad = c.fnLoad
c.funcSave = c.fnSave c.funcSave = c.fnSave
c.opts.Hooks.EachNext(func(hook options.Hook) { c.opts.Hooks.EachPrev(func(hook options.Hook) {
switch h := hook.(type) { switch h := hook.(type) {
case HookLoad: case HookLoad:
c.funcLoad = h(c.funcLoad) c.funcLoad = h(c.funcLoad)

View File

@ -723,7 +723,7 @@ func (n *noopServer) createSubHandler(sb *subscriber, opts Options) broker.Handl
return nil return nil
} }
opts.Hooks.EachNext(func(hook options.Hook) { opts.Hooks.EachPrev(func(hook options.Hook) {
if h, ok := hook.(HookSubHandler); ok { if h, ok := hook.(HookSubHandler); ok {
fn = h(fn) fn = h(fn)
} }

View File

@ -9,6 +9,7 @@ import (
"go.unistack.org/micro/v3/client" "go.unistack.org/micro/v3/client"
"go.unistack.org/micro/v3/codec" "go.unistack.org/micro/v3/codec"
"go.unistack.org/micro/v3/logger" "go.unistack.org/micro/v3/logger"
"go.unistack.org/micro/v3/options"
"go.unistack.org/micro/v3/server" "go.unistack.org/micro/v3/server"
) )
@ -84,3 +85,40 @@ func TestNoopSub(t *testing.T) {
} }
}() }()
} }
func TestHooks_Wrap(t *testing.T) {
n := 5
fn1 := func(next server.FuncSubHandler) server.FuncSubHandler {
return func(ctx context.Context, msg server.Message) (err error) {
n *= 2
return next(ctx, msg)
}
}
fn2 := func(next server.FuncSubHandler) server.FuncSubHandler {
return func(ctx context.Context, msg server.Message) (err error) {
n -= 10
return next(ctx, msg)
}
}
hs := &options.Hooks{}
hs.Append(server.HookSubHandler(fn1), server.HookSubHandler(fn2))
var fn = func(ctx context.Context, msg server.Message) error {
return nil
}
hs.EachPrev(func(hook options.Hook) {
if h, ok := hook.(server.HookSubHandler); ok {
fn = h(fn)
}
})
if err := fn(nil, nil); err != nil {
t.Fatal(err)
}
if n != 0 {
t.Fatalf("uncorrected hooks call")
}
}

View File

@ -123,7 +123,7 @@ func (m *memoryStore) Init(opts ...store.Option) error {
m.funcList = m.fnList m.funcList = m.fnList
m.funcDelete = m.fnDelete m.funcDelete = m.fnDelete
m.opts.Hooks.EachNext(func(hook options.Hook) { m.opts.Hooks.EachPrev(func(hook options.Hook) {
switch h := hook.(type) { switch h := hook.(type) {
case store.HookRead: case store.HookRead:
m.funcRead = h(m.funcRead) m.funcRead = h(m.funcRead)

View File

@ -54,7 +54,7 @@ func (n *noopStore) Init(opts ...Option) error {
n.funcList = n.fnList n.funcList = n.fnList
n.funcDelete = n.fnDelete n.funcDelete = n.fnDelete
n.opts.Hooks.EachNext(func(hook options.Hook) { n.opts.Hooks.EachPrev(func(hook options.Hook) {
switch h := hook.(type) { switch h := hook.(type) {
case HookRead: case HookRead:
n.funcRead = h(n.funcRead) n.funcRead = h(n.funcRead)