From f5be5180f028c3b87c1ebdb720ba9261d245b885 Mon Sep 17 00:00:00 2001 From: Mattias Wadman Date: Sat, 16 Jul 2022 22:03:30 +0200 Subject: [PATCH] interp: Cleanup and clarify some format naming --- format/fqtest_test.go | 2 +- fq.go | 2 +- pkg/interp/decode.go | 6 ++--- pkg/interp/default_register.go | 20 +++++++------- pkg/interp/fqtest_test.go | 2 +- pkg/interp/registry.go | 48 +++++++++++++++++----------------- 6 files changed, 40 insertions(+), 40 deletions(-) diff --git a/format/fqtest_test.go b/format/fqtest_test.go index 92ca9cf5..49c130f3 100644 --- a/format/fqtest_test.go +++ b/format/fqtest_test.go @@ -9,5 +9,5 @@ import ( ) func TestFQTests(t *testing.T) { - fqtest.TestPath(t, interp.DefaultRegister) + fqtest.TestPath(t, interp.DefaultRegistry) } diff --git a/fq.go b/fq.go index 34f45f68..eb779044 100644 --- a/fq.go +++ b/fq.go @@ -9,5 +9,5 @@ import ( const version = "0.0.7" func main() { - cli.Main(interp.DefaultRegister, version) + cli.Main(interp.DefaultRegistry, version) } diff --git a/pkg/interp/decode.go b/pkg/interp/decode.go index f2d7fc0f..436fa3da 100644 --- a/pkg/interp/decode.go +++ b/pkg/interp/decode.go @@ -52,10 +52,10 @@ func (i *Interp) _registry(c any) any { groups := map[string]any{} formats := map[string]any{} - for groupName := range i.Registry.Groups { + for groupName := range i.Registry.FormatGroups { var group []any - for _, f := range i.Registry.MustGroup(groupName) { + for _, f := range i.Registry.MustFormatGroup(groupName) { group = append(group, f.Name) if _, ok := uniqueFormats[f.Name]; ok { continue @@ -258,7 +258,7 @@ func (i *Interp) _decode(c any, format string, opts decodeOpts) any { if err != nil { return err } - decodeFormat, err := i.Registry.Group(formatName) + decodeFormat, err := i.Registry.FormatGroup(formatName) if err != nil { return err } diff --git a/pkg/interp/default_register.go b/pkg/interp/default_register.go index 0bf0b2ce..839d0f71 100644 --- a/pkg/interp/default_register.go +++ b/pkg/interp/default_register.go @@ -8,37 +8,37 @@ import ( "github.com/wader/gojq" ) -// DefaultRegister global registry used by formats -var DefaultRegister = NewRegistry() +// DefaultRegistry global registry used by formats +var DefaultRegistry = NewRegistry() func RegisterFormat(format decode.Format) { - DefaultRegister.Format(format) + DefaultRegistry.Format(format) } func RegisterFS(fs fs.ReadDirFS) { - DefaultRegister.FS(fs) + DefaultRegistry.FS(fs) } func RegisterFunc0[Tc any](name string, fn func(e *Interp, c Tc) any) { - DefaultRegister.Func(gojqextra.Func0(name, fn)) + DefaultRegistry.Func(gojqextra.Func0(name, fn)) } func RegisterFunc1[Tc any, Ta0 any](name string, fn func(e *Interp, c Tc, a0 Ta0) any) { - DefaultRegister.Func(gojqextra.Func1(name, fn)) + DefaultRegistry.Func(gojqextra.Func1(name, fn)) } func RegisterFunc2[Tc any, Ta0 any, Ta1 any](name string, fn func(e *Interp, c Tc, a0 Ta0, a1 Ta1) any) { - DefaultRegister.Func(gojqextra.Func2(name, fn)) + DefaultRegistry.Func(gojqextra.Func2(name, fn)) } func RegisterIter0[Tc any](name string, fn func(e *Interp, c Tc) gojq.Iter) { - DefaultRegister.Func(gojqextra.Iter0(name, fn)) + DefaultRegistry.Func(gojqextra.Iter0(name, fn)) } func RegisterIter1[Tc any, Ta0 any](name string, fn func(e *Interp, c Tc, a0 Ta0) gojq.Iter) { - DefaultRegister.Func(gojqextra.Iter1(name, fn)) + DefaultRegistry.Func(gojqextra.Iter1(name, fn)) } func RegisterIter2[Tc any, Ta0 any, Ta1 any](name string, fn func(e *Interp, c Tc, a0 Ta0, a1 Ta1) gojq.Iter) { - DefaultRegister.Func(gojqextra.Iter2(name, fn)) + DefaultRegistry.Func(gojqextra.Iter2(name, fn)) } diff --git a/pkg/interp/fqtest_test.go b/pkg/interp/fqtest_test.go index 5867e804..d3c09105 100644 --- a/pkg/interp/fqtest_test.go +++ b/pkg/interp/fqtest_test.go @@ -9,5 +9,5 @@ import ( ) func TestFQTests(t *testing.T) { - fqtest.TestPath(t, interp.DefaultRegister) + fqtest.TestPath(t, interp.DefaultRegistry) } diff --git a/pkg/interp/registry.go b/pkg/interp/registry.go index 32662490..e323ecc8 100644 --- a/pkg/interp/registry.go +++ b/pkg/interp/registry.go @@ -14,9 +14,9 @@ import ( type EnvFuncFn func(env *Interp) gojqextra.Function type Registry struct { - Groups map[string]decode.Group - resolveOnce sync.Once - resolved bool + FormatGroups map[string]decode.Group + formatResolveOnce sync.Once + formatResolved bool EnvFuncFns []EnvFuncFn @@ -25,18 +25,18 @@ type Registry struct { func NewRegistry() *Registry { return &Registry{ - Groups: map[string]decode.Group{}, - resolveOnce: sync.Once{}, + FormatGroups: map[string]decode.Group{}, + formatResolveOnce: sync.Once{}, } } -func (r *Registry) register(groupName string, format decode.Format, single bool) decode.Format { - if r.resolved { +func (r *Registry) format(groupName string, format decode.Format, single bool) decode.Format { + if r.formatResolved { // for now can't change after resolved panic("registry already resolved") } - group, ok := r.Groups[groupName] + group, ok := r.FormatGroups[groupName] if ok { if !single { panic(fmt.Sprintf("%s: format already registered", groupName)) @@ -45,17 +45,17 @@ func (r *Registry) register(groupName string, format decode.Format, single bool) group = decode.Group{} } - r.Groups[groupName] = append(group, format) + r.FormatGroups[groupName] = append(group, format) return format } func (r *Registry) Format(format decode.Format) decode.Format { - r.register(format.Name, format, false) + r.format(format.Name, format, false) for _, g := range format.Groups { - r.register(g, format, true) + r.format(g, format, true) } - r.register("all", format, true) + r.format("all", format, true) return format } @@ -77,13 +77,13 @@ func sortFormats(g decode.Group) { }) } -func (r *Registry) resolve() error { - for _, fs := range r.Groups { +func (r *Registry) resolveFormats() error { + for _, fs := range r.FormatGroups { for _, f := range fs { for _, d := range f.Dependencies { var group decode.Group for _, dName := range d.Names { - df, ok := r.Groups[dName] + df, ok := r.FormatGroups[dName] if !ok { return fmt.Errorf("%s: can't find format dependency %s", f.Name, dName) } @@ -96,30 +96,30 @@ func (r *Registry) resolve() error { } } - for _, fs := range r.Groups { + for _, fs := range r.FormatGroups { sortFormats(fs) } - r.resolved = true + r.formatResolved = true return nil } -func (r *Registry) Group(name string) (decode.Group, error) { - r.resolveOnce.Do(func() { - if err := r.resolve(); err != nil { +func (r *Registry) FormatGroup(name string) (decode.Group, error) { + r.formatResolveOnce.Do(func() { + if err := r.resolveFormats(); err != nil { panic(err) } }) - if g, ok := r.Groups[name]; ok { + if g, ok := r.FormatGroups[name]; ok { return g, nil } return nil, errors.New("format group not found") } -func (r *Registry) MustGroup(name string) decode.Group { - g, err := r.Group(name) +func (r *Registry) MustFormatGroup(name string) decode.Group { + g, err := r.FormatGroup(name) if err == nil { return g } @@ -127,5 +127,5 @@ func (r *Registry) MustGroup(name string) decode.Group { } func (r *Registry) MustAll() decode.Group { - return r.MustGroup("all") + return r.MustFormatGroup("all") }