Skip to content

Commit

Permalink
fq: Make format/0 native for performance
Browse files Browse the repository at this point in the history
  • Loading branch information
wader committed Sep 12, 2021
1 parent 45afbe6 commit dfcefc1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
13 changes: 13 additions & 0 deletions pkg/interp/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func (i *Interp) makeFunctions(registry *registry.Registry) []Function {
{[]string{"open"}, 0, 0, i._open, nil},
{[]string{"decode"}, 0, 1, i.makeDecodeFn(registry, registry.MustGroup(format.PROBE)), nil},

{[]string{"format"}, 0, 0, i.format, nil},
{[]string{"display", "d"}, 0, 1, nil, i.makeDisplayFn(nil)},
{[]string{"verbose", "v"}, 0, 1, nil, i.makeDisplayFn(map[string]interface{}{"verbose": true})},
{[]string{"preview", "p"}, 0, 1, nil, i.preview},
Expand Down Expand Up @@ -540,6 +541,18 @@ func (i *Interp) makeDecodeFn(registry *registry.Registry, decodeFormats []*deco
}
}

func (i *Interp) format(c interface{}, a []interface{}) interface{} {
cj, ok := c.(gojq.JQValue)
if !ok {
return nil
}
f, ok := cj.JQValueKey("_format").(string)
if !ok {
return nil
}
return f
}

func (i *Interp) makeDisplayFn(fnOpts map[string]interface{}) func(c interface{}, a []interface{}) gojq.Iter {
return func(c interface{}, a []interface{}) gojq.Iter {
opts, err := i.Options(append([]interface{}{fnOpts}, a...)...)
Expand Down
6 changes: 0 additions & 6 deletions pkg/interp/funcs.jq
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@

def format:
if type == "object" then ._format
else ._format? // null
end;

# integer division
# inspried by https://github.com/itchyny/gojq/issues/63#issuecomment-765066351
def intdiv($a; $b): ($a - ($a % $b)) / $b;
Expand Down

0 comments on commit dfcefc1

Please sign in to comment.