1
1
mirror of https://github.com/wader/fq.git synced 2024-12-23 13:22:58 +03:00

format: Add panic if register after resolve

This commit is contained in:
Mattias Wadman 2021-09-28 13:04:51 +02:00
parent 13e98d44be
commit 577c0f5315

View File

@ -12,6 +12,7 @@ import (
type Registry struct {
Groups map[string][]*decode.Format
resolveOnce sync.Once
resolved bool
}
func New() *Registry {
@ -22,6 +23,11 @@ func New() *Registry {
}
func (r *Registry) register(groupName string, format *decode.Format, single bool) *decode.Format { //nolint:unparam
if r.resolved {
// for now can't change after resolved
panic("registry already resolved")
}
formats, ok := r.Groups[groupName]
if ok {
if !single {
@ -36,6 +42,16 @@ func (r *Registry) register(groupName string, format *decode.Format, single bool
return format
}
func (r *Registry) MustRegister(format *decode.Format) *decode.Format {
r.register(format.Name, format, false)
for _, g := range format.Groups {
r.register(g, format, true)
}
r.register("all", format, true)
return format
}
func sortFormats(fs []*decode.Format) {
sort.Slice(fs, func(i, j int) bool {
if fs[i].ProbeOrder == fs[j].ProbeOrder {
@ -68,19 +84,11 @@ func (r *Registry) resolve() error {
sortFormats(fs)
}
r.resolved = true
return nil
}
func (r *Registry) MustRegister(format *decode.Format) *decode.Format {
r.register(format.Name, format, false)
for _, g := range format.Groups {
r.register(g, format, true)
}
r.register("all", format, true)
return format
}
func (r *Registry) Group(name string) ([]*decode.Format, error) {
r.resolveOnce.Do(func() {
if err := r.resolve(); err != nil {