-
Notifications
You must be signed in to change notification settings - Fork 0
/
registry.go
104 lines (80 loc) · 2.55 KB
/
registry.go
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
Copyright (c) 2020-2021 The yaff Authors (Neil Hemming)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package yaff
import (
"sync"
"github.com/nehemming/lpax"
"github.com/nehemming/yaff/langpack"
)
// Registry holds a list of available formatters.
// It is possible to create multiple formatters, but by default the
// default shared yaff.Formatters registry can be used
// Registry implementors must be threadsafe across all calls.
type Registry interface {
// Register adds or updates an available format.
// The NewFormatter factory is used to create an
// instance oif the registered formatter.
Register(format Format, factory NewFormatter)
// GetFormatter returns the formatter supporting the format or an error if no formatter can be found.
GetFormatter(format Format) (Formatter, error)
// Formats returns a slice of supported formats.
Formats() []Format
}
type registry struct {
factories map[Format]NewFormatter
mu sync.Mutex
}
// NewRegistry creates a new registry.
func NewRegistry() Registry {
return ®istry{
factories: make(map[Format]NewFormatter),
}
}
func (r *registry) Formats() []Format {
// Lock to maintain thread safety.
r.mu.Lock()
defer r.mu.Unlock()
c := make([]Format, len(r.factories))
i := 0
for k := range r.factories {
c[i] = k
i++
}
return c
}
func (r *registry) Register(format Format, factory NewFormatter) {
// Lock to maintain thread safety
r.mu.Lock()
defer r.mu.Unlock()
// Passing nil will "deregister" the factory
r.factories[format] = factory
}
func (r *registry) GetFormatter(format Format) (Formatter, error) {
// getFactory locks registry, but keeps call to factory outside lock
factory := r.getFactory(format)
if factory == nil {
return nil, lpax.Errorf(langpack.ErrorUnknownFormatter, format)
}
return factory()
}
func (r *registry) getFactory(format Format) NewFormatter {
// Lock to maintain thread safety
r.mu.Lock()
defer r.mu.Unlock()
return r.factories[format]
}
var sharedRegistry = NewRegistry()
// Formatters is the shared registry of formatters.
func Formatters() Registry {
return sharedRegistry
}