// In checks if an item exists in this set.
func (s *IdSet) In(item Identified) bool {
+ id := s.normalize(item.Id())
s.RLock()
- _, ok := s.lookup[item.Id()]
+ _, ok := s.lookup[id]
s.RUnlock()
return ok
}
// Get returns an item with the given Id.
func (s *IdSet) Get(id string) (Identified, error) {
s.RLock()
- item, ok := s.lookup[id]
+ item, ok := s.lookup[s.normalize(id)]
s.RUnlock()
if !ok {
s.Lock()
defer s.Unlock()
- _, found := s.lookup[item.Id()]
+ id := s.normalize(item.Id())
+ _, found := s.lookup[id]
if found {
return ErrIdTaken
}
- s.lookup[item.Id()] = item
+ s.lookup[id] = item
return nil
}
func (s *IdSet) Remove(item Identified) error {
s.Lock()
defer s.Unlock()
- id := item.Id()
+ id := s.normalize(item.Id())
_, found := s.lookup[id]
if !found {
return ErrIdentifiedMissing
// Replace item from old id with new Identified.
// Used for moving the same Identified to a new Id, such as a rename.
func (s *IdSet) Replace(oldId string, item Identified) error {
+ id := s.normalize(item.Id())
+ oldId = s.normalize(oldId)
+
s.Lock()
defer s.Unlock()
// Check if it already exists
- _, found := s.lookup[item.Id()]
+ _, found := s.lookup[id]
if found {
return ErrIdTaken
}
delete(s.lookup, oldId)
// Add new Identified
- s.lookup[item.Id()] = item
+ s.lookup[id] = item
return nil
}
return r
}
+
+func (s *IdSet) normalize(id string) string {
+ return strings.ToLower(id)
+}