mirror of
https://github.com/wader/fq.git
synced 2024-12-26 06:51:35 +03:00
Merge pull request #318 from wader/registry-naming
interp: Cleanup and clarify some format naming
This commit is contained in:
commit
90822a8cea
@ -9,5 +9,5 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestFQTests(t *testing.T) {
|
func TestFQTests(t *testing.T) {
|
||||||
fqtest.TestPath(t, interp.DefaultRegister)
|
fqtest.TestPath(t, interp.DefaultRegistry)
|
||||||
}
|
}
|
||||||
|
2
fq.go
2
fq.go
@ -9,5 +9,5 @@ import (
|
|||||||
const version = "0.0.7"
|
const version = "0.0.7"
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
cli.Main(interp.DefaultRegister, version)
|
cli.Main(interp.DefaultRegistry, version)
|
||||||
}
|
}
|
||||||
|
@ -52,10 +52,10 @@ func (i *Interp) _registry(c any) any {
|
|||||||
groups := map[string]any{}
|
groups := map[string]any{}
|
||||||
formats := map[string]any{}
|
formats := map[string]any{}
|
||||||
|
|
||||||
for groupName := range i.Registry.Groups {
|
for groupName := range i.Registry.FormatGroups {
|
||||||
var group []any
|
var group []any
|
||||||
|
|
||||||
for _, f := range i.Registry.MustGroup(groupName) {
|
for _, f := range i.Registry.MustFormatGroup(groupName) {
|
||||||
group = append(group, f.Name)
|
group = append(group, f.Name)
|
||||||
if _, ok := uniqueFormats[f.Name]; ok {
|
if _, ok := uniqueFormats[f.Name]; ok {
|
||||||
continue
|
continue
|
||||||
@ -258,7 +258,7 @@ func (i *Interp) _decode(c any, format string, opts decodeOpts) any {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
decodeFormat, err := i.Registry.Group(formatName)
|
decodeFormat, err := i.Registry.FormatGroup(formatName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -8,37 +8,37 @@ import (
|
|||||||
"github.com/wader/gojq"
|
"github.com/wader/gojq"
|
||||||
)
|
)
|
||||||
|
|
||||||
// DefaultRegister global registry used by formats
|
// DefaultRegistry global registry used by formats
|
||||||
var DefaultRegister = NewRegistry()
|
var DefaultRegistry = NewRegistry()
|
||||||
|
|
||||||
func RegisterFormat(format decode.Format) {
|
func RegisterFormat(format decode.Format) {
|
||||||
DefaultRegister.Format(format)
|
DefaultRegistry.Format(format)
|
||||||
}
|
}
|
||||||
|
|
||||||
func RegisterFS(fs fs.ReadDirFS) {
|
func RegisterFS(fs fs.ReadDirFS) {
|
||||||
DefaultRegister.FS(fs)
|
DefaultRegistry.FS(fs)
|
||||||
}
|
}
|
||||||
|
|
||||||
func RegisterFunc0[Tc any](name string, fn func(e *Interp, c Tc) any) {
|
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) {
|
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) {
|
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) {
|
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) {
|
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) {
|
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))
|
||||||
}
|
}
|
||||||
|
@ -9,5 +9,5 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestFQTests(t *testing.T) {
|
func TestFQTests(t *testing.T) {
|
||||||
fqtest.TestPath(t, interp.DefaultRegister)
|
fqtest.TestPath(t, interp.DefaultRegistry)
|
||||||
}
|
}
|
||||||
|
@ -14,9 +14,9 @@ import (
|
|||||||
type EnvFuncFn func(env *Interp) gojqextra.Function
|
type EnvFuncFn func(env *Interp) gojqextra.Function
|
||||||
|
|
||||||
type Registry struct {
|
type Registry struct {
|
||||||
Groups map[string]decode.Group
|
FormatGroups map[string]decode.Group
|
||||||
resolveOnce sync.Once
|
formatResolveOnce sync.Once
|
||||||
resolved bool
|
formatResolved bool
|
||||||
|
|
||||||
EnvFuncFns []EnvFuncFn
|
EnvFuncFns []EnvFuncFn
|
||||||
|
|
||||||
@ -25,18 +25,18 @@ type Registry struct {
|
|||||||
|
|
||||||
func NewRegistry() *Registry {
|
func NewRegistry() *Registry {
|
||||||
return &Registry{
|
return &Registry{
|
||||||
Groups: map[string]decode.Group{},
|
FormatGroups: map[string]decode.Group{},
|
||||||
resolveOnce: sync.Once{},
|
formatResolveOnce: sync.Once{},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Registry) register(groupName string, format decode.Format, single bool) decode.Format {
|
func (r *Registry) format(groupName string, format decode.Format, single bool) decode.Format {
|
||||||
if r.resolved {
|
if r.formatResolved {
|
||||||
// for now can't change after resolved
|
// for now can't change after resolved
|
||||||
panic("registry already resolved")
|
panic("registry already resolved")
|
||||||
}
|
}
|
||||||
|
|
||||||
group, ok := r.Groups[groupName]
|
group, ok := r.FormatGroups[groupName]
|
||||||
if ok {
|
if ok {
|
||||||
if !single {
|
if !single {
|
||||||
panic(fmt.Sprintf("%s: format already registered", groupName))
|
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{}
|
group = decode.Group{}
|
||||||
}
|
}
|
||||||
|
|
||||||
r.Groups[groupName] = append(group, format)
|
r.FormatGroups[groupName] = append(group, format)
|
||||||
|
|
||||||
return format
|
return format
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Registry) Format(format decode.Format) decode.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 {
|
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
|
return format
|
||||||
}
|
}
|
||||||
@ -77,13 +77,13 @@ func sortFormats(g decode.Group) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Registry) resolve() error {
|
func (r *Registry) resolveFormats() error {
|
||||||
for _, fs := range r.Groups {
|
for _, fs := range r.FormatGroups {
|
||||||
for _, f := range fs {
|
for _, f := range fs {
|
||||||
for _, d := range f.Dependencies {
|
for _, d := range f.Dependencies {
|
||||||
var group decode.Group
|
var group decode.Group
|
||||||
for _, dName := range d.Names {
|
for _, dName := range d.Names {
|
||||||
df, ok := r.Groups[dName]
|
df, ok := r.FormatGroups[dName]
|
||||||
if !ok {
|
if !ok {
|
||||||
return fmt.Errorf("%s: can't find format dependency %s", f.Name, dName)
|
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)
|
sortFormats(fs)
|
||||||
}
|
}
|
||||||
|
|
||||||
r.resolved = true
|
r.formatResolved = true
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Registry) Group(name string) (decode.Group, error) {
|
func (r *Registry) FormatGroup(name string) (decode.Group, error) {
|
||||||
r.resolveOnce.Do(func() {
|
r.formatResolveOnce.Do(func() {
|
||||||
if err := r.resolve(); err != nil {
|
if err := r.resolveFormats(); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
if g, ok := r.Groups[name]; ok {
|
if g, ok := r.FormatGroups[name]; ok {
|
||||||
return g, nil
|
return g, nil
|
||||||
}
|
}
|
||||||
return nil, errors.New("format group not found")
|
return nil, errors.New("format group not found")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Registry) MustGroup(name string) decode.Group {
|
func (r *Registry) MustFormatGroup(name string) decode.Group {
|
||||||
g, err := r.Group(name)
|
g, err := r.FormatGroup(name)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
return g
|
return g
|
||||||
}
|
}
|
||||||
@ -127,5 +127,5 @@ func (r *Registry) MustGroup(name string) decode.Group {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *Registry) MustAll() decode.Group {
|
func (r *Registry) MustAll() decode.Group {
|
||||||
return r.MustGroup("all")
|
return r.MustFormatGroup("all")
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user