Skip to content

Commit

Permalink
format: Add panic if register after resolve
Browse files Browse the repository at this point in the history
  • Loading branch information
wader committed Sep 28, 2021
1 parent 13e98d4 commit 577c0f5
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions format/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
type Registry struct {
Groups map[string][]*decode.Format
resolveOnce sync.Once
resolved bool
}

func New() *Registry {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -68,17 +84,9 @@ func (r *Registry) resolve() error {
sortFormats(fs)
}

return nil
}
r.resolved = true

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
return nil
}

func (r *Registry) Group(name string) ([]*decode.Format, error) {
Expand Down

0 comments on commit 577c0f5

Please sign in to comment.