Introduce ChainID type and several methods for interface #13

Merged
fyrchik merged 1 commit from aarifullin/policy-engine:feature/chain_interface into master 2024-09-04 19:51:23 +00:00
2 changed files with 40 additions and 6 deletions
Showing only changes of commit 76372aac04 - Show all commits

View file

@ -1,9 +1,6 @@
package policyengine
import "sync/atomic"
type inmemory struct {
chainCounter atomic.Uint32
namespace map[Name][]chain
resource map[Name][]chain
local map[Name][]*Chain
@ -77,3 +74,34 @@ func (s *inmemory) AddNameSpaceChain(name Name, namespace string, c *Chain) {
func (s *inmemory) AddOverride(name Name, c *Chain) {
s.local[name] = append(s.local[name], c)
}
func (s *inmemory) GetOverride(name Name, chainID ChainID) (chain *Chain, found bool) {
chains := s.local[name]
for _, chain = range chains {
if chain.ID == chainID {
found = true
return
}
}
return
}
func (s *inmemory) RemoveOverride(name Name, chainID ChainID) (found bool) {
chains := s.local[name]
for i, chain := range chains {
if chain.ID == chainID {
s.local[name] = append(chains[:i], chains[i+1:]...)
found = true
return
}
}
return
}
func (s *inmemory) ListOverrides(name Name) []*Chain {
return s.local[name]
}

View file

@ -9,4 +9,10 @@ type CachedChainStorage interface {
AddNameSpaceChain(name Name, namespace string, c *Chain)
// Adds a local policy chain used for all operations with this service.
AddOverride(name Name, c *Chain)
// Gets the local override with given chain id.
GetOverride(name Name, chainID ChainID) (chain *Chain, found bool)
// Remove the local override with given chain id.
RemoveOverride(name Name, chainID ChainID) (removed bool)
// ListOverrides returns the list of local overrides.
ListOverrides(name Name) []*Chain
}