github.com/cs3org/reva/v2@v2.27.7/pkg/mentix/entity/registry.go (about) 1 // Copyright 2018-2021 CERN 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 // 15 // In applying this license, CERN does not waive the privileges and immunities 16 // granted to it by virtue of its status as an Intergovernmental Organization 17 // or submit itself to any jurisdiction. 18 19 package entity 20 21 import "fmt" 22 23 // Registry represents a simple id->entity map. 24 type Registry struct { 25 Entities map[string]Entity 26 } 27 28 // Register registers a new entity. 29 func (r *Registry) Register(entity Entity) { 30 r.Entities[entity.GetID()] = entity 31 } 32 33 // FindEntities returns all entities matching the provided IDs. 34 // If an entity with a certain ID doesn't exist and mustExist is true, an error is returned. 35 func (r *Registry) FindEntities(ids []string, mustExist bool, anyRequired bool) ([]Entity, error) { 36 var entities []Entity 37 for _, id := range ids { 38 if entity, ok := r.Entities[id]; ok { 39 entities = append(entities, entity) 40 } else if mustExist { 41 return nil, fmt.Errorf("no entity with ID '%v' registered", id) 42 } 43 } 44 45 if anyRequired && len(entities) == 0 { // At least one entity must be configured 46 return nil, fmt.Errorf("no entities available") 47 } 48 49 return entities, nil 50 } 51 52 // NewRegistry returns a new entity registry. 53 func NewRegistry() *Registry { 54 return &Registry{ 55 Entities: make(map[string]Entity), 56 } 57 }