From 76372aac044527363a106f27cb0e7b0c40864d4d Mon Sep 17 00:00:00 2001 From: aarifullin Date: Tue, 31 Oct 2023 02:03:28 +0300 Subject: [PATCH] [#13] interface: Add methods to CachedChainStorage interface * Introduce GetOverride, RemoveOverride, ListOverrides * Implement them in inmemory struct Signed-off-by: aarifullin --- inmemory.go | 40 ++++++++++++++++++++++++++++++++++------ interface.go | 6 ++++++ 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/inmemory.go b/inmemory.go index 4351489..a8e0684 100644 --- a/inmemory.go +++ b/inmemory.go @@ -1,12 +1,9 @@ package policyengine -import "sync/atomic" - type inmemory struct { - chainCounter atomic.Uint32 - namespace map[Name][]chain - resource map[Name][]chain - local map[Name][]*Chain + namespace map[Name][]chain + resource map[Name][]chain + local map[Name][]*Chain } type chain struct { @@ -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] +} diff --git a/interface.go b/interface.go index 1ea8640..d75a701 100644 --- a/interface.go +++ b/interface.go @@ -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 }