Skip to content

Commit 821ce3e

Browse files
authored
Merge pull request #77 from gone-io/feature/update
feat(core): implement GetGonerByPattern method
2 parents a5bdfc2 + d0dd3fa commit 821ce3e

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

core.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,18 @@ func (s *core) GetGonerByType(t reflect.Type) any {
180180
return nil
181181
}
182182

183+
func (s *core) GetGonerByPattern(t reflect.Type, pattern string) (list []any) {
184+
coffins := s.iKeeper.getByTypeAndPattern(t, pattern)
185+
for _, co := range coffins {
186+
if v, err := co.Provide(false, "", t); err != nil {
187+
panic(err)
188+
} else {
189+
list = append(list, v)
190+
}
191+
}
192+
return
193+
}
194+
183195
func (s *core) ProvideNth(n int, t reflect.Type, funcName string) (reflect.Value, error) {
184196
field := reflect.StructField{
185197
Name: fmt.Sprintf("The%dthParameter", n),

core_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package gone
22

33
import (
44
"errors"
5+
"fmt"
56
"go.uber.org/mock/gomock"
67
"reflect"
78
"testing"
@@ -593,3 +594,43 @@ func Test_core_Install(t *testing.T) {
593594
})
594595
}
595596
}
597+
598+
func Test_core_GetGonerByPattern(t *testing.T) {
599+
type X struct {
600+
Flag
601+
Id int
602+
}
603+
604+
t.Run("success", func(t *testing.T) {
605+
NewApp().
606+
Load(&X{Id: 1}, Name("x1")).
607+
Load(&X{Id: 2}, Name("x2")).
608+
Load(&X{Id: 3}, Name("x311")).
609+
Load(&X{Id: 4}, Name("y3")).
610+
Run(func(k Keeper) {
611+
goners := k.GetGonerByPattern(reflect.TypeOf(&X{}), "x*")
612+
if !reflect.DeepEqual(goners, []any{&X{Id: 1}, &X{Id: 2}, &X{Id: 3}}) {
613+
t.Errorf("GetGonerByPattern error")
614+
}
615+
})
616+
})
617+
618+
t.Run("panic", func(t *testing.T) {
619+
provider := WrapFunctionProvider(func(tagConf string, param struct{}) (*X, error) {
620+
return nil, errors.New("error")
621+
})
622+
623+
err := SafeExecute(func() error {
624+
NewApp().
625+
Load(provider, Name("x110")).
626+
Run(func(k Keeper) {
627+
goners := k.GetGonerByPattern(reflect.TypeOf(&X{}), "x*")
628+
fmt.Printf("%#v", goners)
629+
})
630+
return nil
631+
})
632+
if err == nil {
633+
t.Errorf("should be error")
634+
}
635+
})
636+
}

interface.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,4 +676,8 @@ type GonerKeeper interface {
676676
// Returns:
677677
// - any: The component instance if found, nil otherwise
678678
GetGonerByType(t reflect.Type) any
679+
680+
GetGonerByPattern(t reflect.Type, pattern string) []any
679681
}
682+
683+
type Keeper = GonerKeeper

0 commit comments

Comments
 (0)