-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdialect_registry.go
More file actions
35 lines (30 loc) · 930 Bytes
/
dialect_registry.go
File metadata and controls
35 lines (30 loc) · 930 Bytes
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
package gcode
import "sync"
// DialectRegistry is a thread-safe registry of named dialects.
type DialectRegistry struct {
mu sync.RWMutex
dialects map[string]*Dialect
}
// NewDialectRegistry creates a new empty dialect registry.
func NewDialectRegistry() *DialectRegistry {
return &DialectRegistry{
dialects: make(map[string]*Dialect),
}
}
// Register adds a dialect to the registry keyed by its name and
// returns r so calls can be chained. If a dialect with the same name
// already exists it is overwritten.
func (r *DialectRegistry) Register(d *Dialect) *DialectRegistry {
r.mu.Lock()
defer r.mu.Unlock()
r.dialects[d.Name()] = d
return r
}
// Lookup returns the dialect with the given name. The second return value
// indicates whether the dialect was found.
func (r *DialectRegistry) Lookup(name string) (*Dialect, bool) {
r.mu.RLock()
defer r.mu.RUnlock()
d, ok := r.dialects[name]
return d, ok
}