Fixed trimming of favorite namespaces in Config (#3065)

Co-authored-by: Fernand Galiana <fernand.galiana@gmail.com>
mine
assert-0 2025-01-19 17:55:33 +01:00 committed by GitHub
parent d72467a582
commit 4636a4d3bc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 30 additions and 6 deletions

View File

@ -75,6 +75,26 @@ func Test_contextMerge(t *testing.T) {
c2: &Context{},
e: NewContext(),
},
"too-many-favs": {
c1: &Context{
Namespace: &Namespace{
Active: "ns1",
Favorites: []string{"ns1", "ns2", "ns3", "ns4", "ns5", "ns6", "ns7", "ns8", "ns9"},
},
},
c2: &Context{
Namespace: &Namespace{
Active: "ns10",
Favorites: []string{"ns10", "ns11", "ns12"},
},
},
e: &Context{
Namespace: &Namespace{
Active: "ns1",
Favorites: []string{"ns1", "ns2", "ns3", "ns4", "ns5", "ns6", "ns7", "ns8", "ns9"},
},
},
},
}
for k, u := range uu {

View File

@ -52,6 +52,8 @@ func (n *Namespace) merge(old *Namespace) {
}
n.Favorites = append(n.Favorites, fav)
}
n.trimFavNs()
}
// Validate validates a namespace is setup correctly.
@ -69,12 +71,7 @@ func (n *Namespace) Validate(c client.Connection) {
}
}
if len(n.Favorites) > MaxFavoritesNS {
log.Debug().Msgf("[Namespace] Number of favorite exceeds hard limit of %v. Trimming.", MaxFavoritesNS)
for _, ns := range n.Favorites[MaxFavoritesNS:] {
n.rmFavNS(ns)
}
}
n.trimFavNs()
}
// SetActive set the active namespace.
@ -135,3 +132,10 @@ func (n *Namespace) rmFavNS(ns string) {
n.Favorites = append(n.Favorites[:victim], n.Favorites[victim+1:]...)
}
func (n *Namespace) trimFavNs() {
if len(n.Favorites) > MaxFavoritesNS {
log.Debug().Msgf("[Namespace] Number of favorite exceeds hard limit of %v. Trimming.", MaxFavoritesNS)
n.Favorites = n.Favorites[:MaxFavoritesNS]
}
}