1
1
mirror of https://github.com/wader/fq.git synced 2024-12-25 14:23:18 +03:00

Merge pull request #318 from wader/registry-naming

interp: Cleanup and clarify some format naming
This commit is contained in:
Mattias Wadman 2022-07-16 22:12:03 +02:00 committed by GitHub
commit 90822a8cea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 40 additions and 40 deletions

View File

@ -9,5 +9,5 @@ import (
)
func TestFQTests(t *testing.T) {
fqtest.TestPath(t, interp.DefaultRegister)
fqtest.TestPath(t, interp.DefaultRegistry)
}

2
fq.go
View File

@ -9,5 +9,5 @@ import (
const version = "0.0.7"
func main() {
cli.Main(interp.DefaultRegister, version)
cli.Main(interp.DefaultRegistry, version)
}

View File

@ -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
}

View File

@ -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))
}

View File

@ -9,5 +9,5 @@ import (
)
func TestFQTests(t *testing.T) {
fqtest.TestPath(t, interp.DefaultRegister)
fqtest.TestPath(t, interp.DefaultRegistry)
}

View File

@ -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")
}