github.com/annwntech/go-micro/v2@v2.9.5/util/scope/scope.go (about) 1 package scope 2 3 import ( 4 "fmt" 5 6 "github.com/annwntech/go-micro/v2/store" 7 ) 8 9 // Scope extends the store, applying a prefix to each request 10 type Scope struct { 11 store.Store 12 prefix string 13 } 14 15 // NewScope returns an initialised scope 16 func NewScope(s store.Store, prefix string) Scope { 17 return Scope{Store: s, prefix: prefix} 18 } 19 20 func (s *Scope) Options() store.Options { 21 o := s.Store.Options() 22 o.Table = s.prefix 23 return o 24 } 25 26 func (s *Scope) Read(key string, opts ...store.ReadOption) ([]*store.Record, error) { 27 key = fmt.Sprintf("%v/%v", s.prefix, key) 28 return s.Store.Read(key, opts...) 29 } 30 31 func (s *Scope) Write(r *store.Record, opts ...store.WriteOption) error { 32 r.Key = fmt.Sprintf("%v/%v", s.prefix, r.Key) 33 return s.Store.Write(r, opts...) 34 } 35 36 func (s *Scope) Delete(key string, opts ...store.DeleteOption) error { 37 key = fmt.Sprintf("%v/%v", s.prefix, key) 38 return s.Store.Delete(key, opts...) 39 } 40 41 func (s *Scope) List(opts ...store.ListOption) ([]string, error) { 42 var lops store.ListOptions 43 for _, o := range opts { 44 o(&lops) 45 } 46 47 key := fmt.Sprintf("%v/%v", s.prefix, lops.Prefix) 48 opts = append(opts, store.ListPrefix(key)) 49 50 return s.Store.List(opts...) 51 }