File: registry.go

package info (click to toggle)
golang-github-kong-go-kong 0.15.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 620 kB
  • sloc: sh: 18; makefile: 4
file content (63 lines) | stat: -rw-r--r-- 1,683 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package custom

import "errors"

// Registry is a store of EntityCRUD objects
type Registry interface {
	// Register puts EntityCRUD in the internal
	// store and returns an error if the entity
	// of Type is already registered
	Register(Type, EntityCRUD) error
	// Lookup returns the EntityCRUD object associated
	// with typ, or nil if one is not Registered yet.
	Lookup(Type) EntityCRUD
	// Unregister unregisters the entity
	// registered with Type from the store.
	// It returns an
	// error if the Type was not registered
	// before this call.
	Unregister(Type) error
}

// defaultRegistry is an out of the box implementation
// of Registry object.
type defaultRegistry struct {
	store map[Type]EntityCRUD
}

// NewDefaultRegistry returns a default registry
func NewDefaultRegistry() Registry {
	return &defaultRegistry{
		store: make(map[Type]EntityCRUD),
	}
}

// Register puts EntityCRUD in the internal
// store and returns an error if the entity
// of Type is already registered.
func (r *defaultRegistry) Register(typ Type, def EntityCRUD) error {
	if _, ok := r.store[typ]; ok {
		return errors.New("type already registered")
	}
	r.store[typ] = def
	return nil
}

// Lookup returns the EntityCRUD object associated
// with typ, or nil if one is not Registered yet.
func (r *defaultRegistry) Lookup(typ Type) EntityCRUD {
	return r.store[typ]
}

// Unregister unregisters the entity
// registered with Type from the store.
// It returns an
// error if the Type was not registered
// before this call.
func (r *defaultRegistry) Unregister(typ Type) error {
	if _, ok := r.store[typ]; !ok {
		return errors.New("type not registered")
	}
	delete(r.store, typ)
	return nil
}