mirror of
https://github.com/shazow/ssh-chat.git
synced 2025-07-03 04:04:34 +02:00
parent
a55b78ccdb
commit
a978f2ce00
@ -49,8 +49,9 @@ func (s *IdSet) Len() int {
|
|||||||
|
|
||||||
// In checks if an item exists in this set.
|
// In checks if an item exists in this set.
|
||||||
func (s *IdSet) In(item Identified) bool {
|
func (s *IdSet) In(item Identified) bool {
|
||||||
|
id := s.normalize(item.Id())
|
||||||
s.RLock()
|
s.RLock()
|
||||||
_, ok := s.lookup[item.Id()]
|
_, ok := s.lookup[id]
|
||||||
s.RUnlock()
|
s.RUnlock()
|
||||||
return ok
|
return ok
|
||||||
}
|
}
|
||||||
@ -58,7 +59,7 @@ func (s *IdSet) In(item Identified) bool {
|
|||||||
// Get returns an item with the given Id.
|
// Get returns an item with the given Id.
|
||||||
func (s *IdSet) Get(id string) (Identified, error) {
|
func (s *IdSet) Get(id string) (Identified, error) {
|
||||||
s.RLock()
|
s.RLock()
|
||||||
item, ok := s.lookup[id]
|
item, ok := s.lookup[s.normalize(id)]
|
||||||
s.RUnlock()
|
s.RUnlock()
|
||||||
|
|
||||||
if !ok {
|
if !ok {
|
||||||
@ -73,12 +74,13 @@ func (s *IdSet) Add(item Identified) error {
|
|||||||
s.Lock()
|
s.Lock()
|
||||||
defer s.Unlock()
|
defer s.Unlock()
|
||||||
|
|
||||||
_, found := s.lookup[item.Id()]
|
id := s.normalize(item.Id())
|
||||||
|
_, found := s.lookup[id]
|
||||||
if found {
|
if found {
|
||||||
return ErrIdTaken
|
return ErrIdTaken
|
||||||
}
|
}
|
||||||
|
|
||||||
s.lookup[item.Id()] = item
|
s.lookup[id] = item
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -86,7 +88,7 @@ func (s *IdSet) Add(item Identified) error {
|
|||||||
func (s *IdSet) Remove(item Identified) error {
|
func (s *IdSet) Remove(item Identified) error {
|
||||||
s.Lock()
|
s.Lock()
|
||||||
defer s.Unlock()
|
defer s.Unlock()
|
||||||
id := item.Id()
|
id := s.normalize(item.Id())
|
||||||
_, found := s.lookup[id]
|
_, found := s.lookup[id]
|
||||||
if !found {
|
if !found {
|
||||||
return ErrIdentifiedMissing
|
return ErrIdentifiedMissing
|
||||||
@ -98,11 +100,14 @@ func (s *IdSet) Remove(item Identified) error {
|
|||||||
// Replace item from old id with new Identified.
|
// Replace item from old id with new Identified.
|
||||||
// Used for moving the same Identified to a new Id, such as a rename.
|
// Used for moving the same Identified to a new Id, such as a rename.
|
||||||
func (s *IdSet) Replace(oldId string, item Identified) error {
|
func (s *IdSet) Replace(oldId string, item Identified) error {
|
||||||
|
id := s.normalize(item.Id())
|
||||||
|
oldId = s.normalize(oldId)
|
||||||
|
|
||||||
s.Lock()
|
s.Lock()
|
||||||
defer s.Unlock()
|
defer s.Unlock()
|
||||||
|
|
||||||
// Check if it already exists
|
// Check if it already exists
|
||||||
_, found := s.lookup[item.Id()]
|
_, found := s.lookup[id]
|
||||||
if found {
|
if found {
|
||||||
return ErrIdTaken
|
return ErrIdTaken
|
||||||
}
|
}
|
||||||
@ -115,7 +120,7 @@ func (s *IdSet) Replace(oldId string, item Identified) error {
|
|||||||
delete(s.lookup, oldId)
|
delete(s.lookup, oldId)
|
||||||
|
|
||||||
// Add new Identified
|
// Add new Identified
|
||||||
s.lookup[item.Id()] = item
|
s.lookup[id] = item
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -147,3 +152,7 @@ func (s *IdSet) ListPrefix(prefix string) []Identified {
|
|||||||
|
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *IdSet) normalize(id string) string {
|
||||||
|
return strings.ToLower(id)
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user