Skip to content

Commit

Permalink
Merge pull request #32 from wader/refactor-move-registry-to-decode
Browse files Browse the repository at this point in the history
interp: Move _registry to decode
  • Loading branch information
wader authored Dec 25, 2021
2 parents 46b59d0 + 55b1d5c commit 1a4b332
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 83 deletions.
84 changes: 83 additions & 1 deletion pkg/interp/decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"math/big"
"strings"
"time"
Expand All @@ -22,9 +23,10 @@ import (
func init() {
functionRegisterFns = append(functionRegisterFns, func(i *Interp) []Function {
return []Function{
{"_registry", 0, 0, i._registry, nil},
{"_tovalue", 1, 1, i._toValue, nil},
{"_decode", 2, 2, i._decode, nil},
{"_is_decode_value", 0, 0, i._isDecodeValue, nil},
{"_tovalue", 1, 1, i._toValue, nil},
}
})
}
Expand All @@ -46,6 +48,86 @@ type DecodeValue interface {
DecodeValue() *decode.Value
}

func (i *Interp) _registry(c interface{}, a []interface{}) interface{} {
uniqueFormats := map[string]decode.Format{}

groups := map[string]interface{}{}
formats := map[string]interface{}{}

for fsName := range i.registry.Groups {
var group []interface{}

for _, f := range i.registry.MustGroup(fsName) {
group = append(group, f.Name)
if _, ok := uniqueFormats[f.Name]; ok {
continue
}
uniqueFormats[f.Name] = f
}

groups[fsName] = group
}

for _, f := range uniqueFormats {
vf := map[string]interface{}{
"name": f.Name,
"description": f.Description,
"probe_order": f.ProbeOrder,
"root_name": f.RootName,
"root_array": f.RootArray,
}

var dependenciesVs []interface{}
for _, d := range f.Dependencies {
var dNamesVs []interface{}
for _, n := range d.Names {
dNamesVs = append(dNamesVs, n)
}
dependenciesVs = append(dependenciesVs, dNamesVs)
}
if len(dependenciesVs) > 0 {
vf["dependencies"] = dependenciesVs
}
var groupsVs []interface{}
for _, n := range f.Groups {
groupsVs = append(groupsVs, n)
}
if len(groupsVs) > 0 {
vf["groups"] = groupsVs
}

if f.Files != nil {
files := map[string]interface{}{}

entries, err := f.Files.ReadDir(".")
if err != nil {
return err
}

for _, e := range entries {
f, err := f.Files.Open(e.Name())
if err != nil {
return err
}
b, err := ioutil.ReadAll(f)
if err != nil {
return err
}
files[e.Name()] = string(b)
}

vf["files"] = files
}

formats[f.Name] = vf
}

return map[string]interface{}{
"groups": groups,
"formats": formats,
}
}

func (i *Interp) _toValue(c interface{}, a []interface{}) interface{} {
v, _ := toValue(
func() Options { return i.Options(a[0]) },
Expand Down
82 changes: 0 additions & 82 deletions pkg/interp/interp.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"fmt"
"io"
"io/fs"
"io/ioutil"
"math/big"
"path/filepath"
"strconv"
Expand Down Expand Up @@ -61,7 +60,6 @@ func init() {
{"_extkeys", 0, 0, i._extKeys, nil},
{"_exttype", 0, 0, i._extType, nil},
{"_global_state", 0, 1, i.makeStateFn(i.state), nil},
{"_registry", 0, 0, i._registry, nil},
{"history", 0, 0, i.history, nil},
{"_display", 1, 1, nil, i._display},
{"_can_display", 0, 0, i._canDisplay, nil},
Expand Down Expand Up @@ -539,86 +537,6 @@ func (i *Interp) makeStateFn(state *interface{}) func(c interface{}, a []interfa
}
}

func (i *Interp) _registry(c interface{}, a []interface{}) interface{} {
uniqueFormats := map[string]decode.Format{}

groups := map[string]interface{}{}
formats := map[string]interface{}{}

for fsName := range i.registry.Groups {
var group []interface{}

for _, f := range i.registry.MustGroup(fsName) {
group = append(group, f.Name)
if _, ok := uniqueFormats[f.Name]; ok {
continue
}
uniqueFormats[f.Name] = f
}

groups[fsName] = group
}

for _, f := range uniqueFormats {
vf := map[string]interface{}{
"name": f.Name,
"description": f.Description,
"probe_order": f.ProbeOrder,
"root_name": f.RootName,
"root_array": f.RootArray,
}

var dependenciesVs []interface{}
for _, d := range f.Dependencies {
var dNamesVs []interface{}
for _, n := range d.Names {
dNamesVs = append(dNamesVs, n)
}
dependenciesVs = append(dependenciesVs, dNamesVs)
}
if len(dependenciesVs) > 0 {
vf["dependencies"] = dependenciesVs
}
var groupsVs []interface{}
for _, n := range f.Groups {
groupsVs = append(groupsVs, n)
}
if len(groupsVs) > 0 {
vf["groups"] = groupsVs
}

if f.Files != nil {
files := map[string]interface{}{}

entries, err := f.Files.ReadDir(".")
if err != nil {
return err
}

for _, e := range entries {
f, err := f.Files.Open(e.Name())
if err != nil {
return err
}
b, err := ioutil.ReadAll(f)
if err != nil {
return err
}
files[e.Name()] = string(b)
}

vf["files"] = files
}

formats[f.Name] = vf
}

return map[string]interface{}{
"groups": groups,
"formats": formats,
}
}

func (i *Interp) makeStdioFn(t Terminal) func(c interface{}, a []interface{}) gojq.Iter {
return func(c interface{}, a []interface{}) gojq.Iter {
if c == nil {
Expand Down

0 comments on commit 1a4b332

Please sign in to comment.