Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add Type function to store interface #2160

Merged
merged 2 commits into from
Oct 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions api/internal/core/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type Pagination struct {
}

type Interface interface {
Type() HubKey
Get(ctx context.Context, key string) (interface{}, error)
List(ctx context.Context, input ListInput) (*ListOutput, error)
Create(ctx context.Context, obj interface{}) (interface{}, error)
Expand All @@ -64,6 +65,7 @@ type GenericStoreOption struct {
KeyFunc func(obj interface{}) string
StockCheck func(obj interface{}, stockObj interface{}) error
Validator Validator
HubKey HubKey
}

func NewGenericStore(opt GenericStoreOption) (*GenericStore, error) {
Expand Down Expand Up @@ -142,6 +144,10 @@ func (s *GenericStore) Init() error {
return nil
}

func (s *GenericStore) Type() HubKey {
return s.opt.HubKey
}

func (s *GenericStore) Get(_ context.Context, key string) (interface{}, error) {
ret, ok := s.cache.Load(key)
if !ok {
Expand Down
5 changes: 5 additions & 0 deletions api/internal/core/store/store_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ import (

type MockInterface struct {
mock.Mock
HubKey HubKey
}

func (m *MockInterface) Type() HubKey {
return m.HubKey
}

func (m *MockInterface) Get(_ context.Context, key string) (interface{}, error) {
Expand Down
1 change: 1 addition & 0 deletions api/internal/core/store/storehub.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func InitStore(key HubKey, opt GenericStoreOption) error {
}
opt.Validator = validator
}
opt.HubKey = key
s, err := NewGenericStore(opt)
if err != nil {
log.Errorf("NewGenericStore error: %s", err)
Expand Down