Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
.vscode/
*.log
*.ff
main
rare
rare.exe
dist/
/testdata/
*.prof
coverage.txt
rare.1.gz
rare.1.gz
25 changes: 24 additions & 1 deletion cmd/expressions.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"rare/pkg/expressions/funclib"
"rare/pkg/humanize"
"rare/pkg/minijson"
"sort"
"strconv"
"strings"
"time"
Expand All @@ -28,8 +29,17 @@ func expressionFunction(c *cli.Context) error {
stats = c.Bool("stats")
skipNewline = c.Bool("skip-newline")
detailed = stats || benchmark
listFuncs = c.Bool("listfuncs")
)

if listFuncs {
fmt.Println(color.Wrap(color.Bold, "Builtin: "), strings.Join(extractFuncNames(funclib.Builtins), ", "))
if len(funclib.Additional) > 0 {
fmt.Println(color.Wrap(color.Bold, "FuncsFile:"), strings.Join(extractFuncNames(funclib.Additional), ", "))
}
return nil
}

if c.NArg() != 1 {
return errors.New("expected exactly 1 expression argument. Use - for stdin")
}
Expand Down Expand Up @@ -136,16 +146,29 @@ func buildSpecialKeyJson(matches []string, values map[string]string) string {
return json.String()
}

func extractFuncNames(lib funclib.FunctionSet) []string {
ret := make([]string, 0, len(lib))
for name := range lib {
ret = append(ret, name)
}
sort.Strings(ret)
return ret
}

func expressionCommand() *cli.Command {
return &cli.Command{
Name: "expression",
Usage: "Evaluate and benchmark expressions",
Description: "Given an expression, and optionally some data, test the output and performance of an expression",
ArgsUsage: "<expression|->",
Aliases: []string{"exp"},
Aliases: []string{"exp", "expr"},
Action: expressionFunction,
Category: cmdCatHelp,
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "listfuncs",
Usage: "Lists all available expression functions",
},
&cli.BoolFlag{
Name: "skip-newline",
Aliases: []string{"n"},
Expand Down
14 changes: 14 additions & 0 deletions cmd/expressions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"os"
"rare/pkg/expressions/funclib"
"rare/pkg/testutil"
"testing"

Expand Down Expand Up @@ -69,6 +70,19 @@ func TestExpressionErrors(t *testing.T) {
assert.NotEmpty(t, e)
}

func TestListFuncs(t *testing.T) {
testutil.StoreGlobal(&funclib.Additional)
defer testutil.RestoreGlobals()

funclib.Additional["test"] = nil

o, e, err := testCommandCapture(expressionCommand(), "--listfuncs")
assert.NoError(t, err)
assert.Empty(t, e)
assert.Contains(t, o, "Builtin:")
assert.Contains(t, o, "FuncsFile: test")
}

func TestKeyParser(t *testing.T) {
k, v := parseKeyValue("")
assert.Empty(t, k)
Expand Down
Binary file modified docs/cli-help.md
Binary file not shown.
9 changes: 5 additions & 4 deletions pkg/expressions/funcfile/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ func LoadDefinitionsFile(compiler *expressions.KeyBuilder, filename string) (map
}
defer f.Close()

return LoadDefinitions(compiler, f)
return LoadDefinitions(compiler, f, filename)
}

func LoadDefinitions(compiler *expressions.KeyBuilder, r io.Reader) (map[string]expressions.KeyBuilderFunction, error) {
func LoadDefinitions(compiler *expressions.KeyBuilder, r io.Reader, source string) (map[string]expressions.KeyBuilderFunction, error) {
scanner := bufio.NewScanner(r)
ret := make(map[string]expressions.KeyBuilderFunction)

Expand Down Expand Up @@ -53,21 +53,22 @@ func LoadDefinitions(compiler *expressions.KeyBuilder, r io.Reader) (map[string]
// Split arguments
args := strings.SplitN(phrase, " ", 2)
if len(args) != 2 {
logger.Printf("%s:%d Missing expression for '%s'", source, linenum, phrase)
continue
}

// Compile and save
fnc, err := createAndAddFunc(compiler, args[0], args[1])
if err != nil {
logger.Printf("Error creating function '%s', line %d: %s", args[0], linenum, err)
logger.Printf("%s:%d Error creating function '%s': %s", source, linenum, args[0], err)
errors++
} else {
ret[args[0]] = fnc
}
}

if errors > 0 {
return ret, fmt.Errorf("%d compile errors while loading func spec", errors)
return ret, fmt.Errorf("%s: Had %d error(s)", source, errors)
}
return ret, nil
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/expressions/funcfile/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestLoadDefinitions(t *testing.T) {
{0} {0}}
`
kb := stdlib.NewStdKeyBuilder()
funcs, err := LoadDefinitions(kb, strings.NewReader(data))
funcs, err := LoadDefinitions(kb, strings.NewReader(data), "test")
assert.NoError(t, err)
assert.Len(t, funcs, 2)
assert.Contains(t, funcs, "quad")
Expand All @@ -39,7 +39,7 @@ func TestLoadDefinitionsErrs(t *testing.T) {

`
kb := stdlib.NewStdKeyBuilder()
funcs, err := LoadDefinitions(kb, strings.NewReader(data))
funcs, err := LoadDefinitions(kb, strings.NewReader(data), "test")
assert.NotNil(t, err)
assert.Len(t, funcs, 0)
}
Expand Down
7 changes: 5 additions & 2 deletions pkg/expressions/funclib/builder.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package funclib

import "rare/pkg/expressions"
import (
"rare/pkg/expressions"
)

func NewKeyBuilderEx(autoOptimize bool) *expressions.KeyBuilder {
kb := expressions.NewKeyBuilderEx(autoOptimize)
kb.Funcs(Functions)
kb.Funcs(Builtins)
kb.Funcs(Additional)
return kb
}

Expand Down
6 changes: 4 additions & 2 deletions pkg/expressions/funclib/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import (

type FunctionSet map[string]expressions.KeyBuilderFunction

var Functions FunctionSet = mapMerge(
var Builtins FunctionSet = mapMerge(
stdlib.StandardFunctions)

var Additional FunctionSet = make(FunctionSet)

func AddFunctions(funcs FunctionSet) {
for name, fnc := range funcs {
Functions[name] = fnc
Additional[name] = fnc
}
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/expressions/funclib/funcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func voidFunc(args []expressions.KeyBuilderStage) (expressions.KeyBuilderStage,
}

func TestFunctionSet(t *testing.T) {
assert.NotZero(t, Functions)
assert.NotZero(t, Builtins)
}

func TestAddFunction(t *testing.T) {
Expand Down