-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
File load, key lookup, and switch statement (#103)
* File load, key lookup, and switch statement * Flag to disable expressions external loading
- Loading branch information
Showing
12 changed files
with
296 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#test 22 | ||
bob 33 | ||
jill 44 | ||
|
||
cat 55 | ||
key |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package stdlib | ||
|
||
import ( | ||
"bufio" | ||
"io" | ||
"os" | ||
"rare/pkg/expressions" | ||
"strings" | ||
) | ||
|
||
var DisableLoad = false | ||
|
||
// {load "filename"} | ||
// loads static file as string | ||
func kfLoadFile(args []expressions.KeyBuilderStage) (expressions.KeyBuilderStage, error) { | ||
if DisableLoad { | ||
return stageErrorf(ErrFile, "loading disabled") | ||
} | ||
|
||
if len(args) != 1 { | ||
return stageErrArgCount(args, 1) | ||
} | ||
|
||
filename, ok := expressions.EvalStaticStage(args[0]) | ||
if !ok { | ||
return stageError(ErrConst) | ||
} | ||
|
||
f, err := os.Open(filename) | ||
if err != nil { | ||
return stageErrorf(ErrFile, "Unable to open file: "+filename) | ||
} | ||
defer f.Close() | ||
|
||
content, err := io.ReadAll(f) | ||
if err != nil { | ||
return stageErrorf(ErrFile, "Error reading file: "+filename) | ||
} | ||
|
||
sContent := string(content) | ||
|
||
return func(context expressions.KeyBuilderContext) string { | ||
return sContent | ||
}, nil | ||
} | ||
|
||
func buildLookupTable(content string, commentPrefix string) map[string]string { | ||
lookup := make(map[string]string) | ||
scanner := bufio.NewScanner(strings.NewReader(content)) | ||
|
||
for scanner.Scan() { | ||
line := scanner.Text() | ||
|
||
if commentPrefix != "" && strings.HasPrefix(line, commentPrefix) { | ||
continue | ||
} | ||
|
||
parts := strings.Fields(line) | ||
switch len(parts) { | ||
case 0: //noop | ||
case 1: | ||
lookup[parts[0]] = "" | ||
case 2: | ||
lookup[parts[0]] = parts[1] | ||
} | ||
} | ||
return lookup | ||
} | ||
|
||
// {lookup key "table" [commentPrefix]} | ||
func kfLookupKey(args []expressions.KeyBuilderStage) (expressions.KeyBuilderStage, error) { | ||
if !isArgCountBetween(args, 2, 3) { | ||
return stageErrArgRange(args, "2-3") | ||
} | ||
|
||
content, ok := expressions.EvalStaticStage(args[1]) | ||
if !ok { | ||
return stageArgError(ErrConst, 1) | ||
} | ||
|
||
commentPrefix := expressions.EvalStageIndexOrDefault(args, 2, "") | ||
|
||
lookup := buildLookupTable(content, commentPrefix) | ||
|
||
return func(context expressions.KeyBuilderContext) string { | ||
key := args[0](context) | ||
return lookup[key] | ||
}, nil | ||
} | ||
|
||
// {haskey key "table" [commentprefix]} | ||
func kfHasKey(args []expressions.KeyBuilderStage) (expressions.KeyBuilderStage, error) { | ||
if !isArgCountBetween(args, 2, 3) { | ||
return stageErrArgRange(args, "2-3") | ||
} | ||
|
||
content, ok := expressions.EvalStaticStage(args[1]) | ||
if !ok { | ||
return stageArgError(ErrConst, 1) | ||
} | ||
|
||
commentPrefix := expressions.EvalStageIndexOrDefault(args, 2, "") | ||
|
||
lookup := buildLookupTable(content, commentPrefix) | ||
|
||
return func(context expressions.KeyBuilderContext) string { | ||
key := args[0](context) | ||
_, has := lookup[key] | ||
return expressions.TruthyStr(has) | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package stdlib | ||
|
||
import ( | ||
"rare/pkg/testutil" | ||
"testing" | ||
) | ||
|
||
func TestLoadFile(t *testing.T) { | ||
testExpression(t, mockContext(), "{load ../../../cmd/testdata/graph.txt}", "bob 22\njack 93\njill 3\nmaria 19") | ||
testExpressionErr(t, mockContext(), "{load {0}}", "<CONST>", ErrConst) | ||
testExpressionErr(t, mockContext(), "{load a b}", "<ARGN>", ErrArgCount) | ||
testExpressionErr(t, mockContext(), "{load notarealfile.txt}", "<FILE>", ErrFile) | ||
|
||
testutil.SwitchGlobal(&DisableLoad, true) | ||
defer testutil.RestoreGlobals() | ||
testExpressionErr(t, mockContext(), "{load ../../../cmd/testdata/graph.txt}", "<FILE>", ErrFile) | ||
} | ||
|
||
func TestLookup(t *testing.T) { | ||
testExpression(t, mockContext("bob"), "{lookup {0} {load ../../../cmd/testdata/lookup.txt}}", "33") | ||
testExpression(t, mockContext("bobert"), "{lookup {0} {load ../../../cmd/testdata/lookup.txt}}", "") | ||
testExpression(t, mockContext("#test"), "{lookup {0} {load ../../../cmd/testdata/lookup.txt}}", "22") | ||
testExpression(t, mockContext("#test"), "{lookup {0} {load ../../../cmd/testdata/lookup.txt} \"#\"}", "") | ||
testExpressionErr(t, mockContext(), "{lookup a}", "<ARGN>", ErrArgCount) | ||
testExpressionErr(t, mockContext(), "{lookup a b c d}", "<ARGN>", ErrArgCount) | ||
testExpressionErr(t, mockContext("fn"), "{lookup fn {0}}", "<CONST>", ErrConst) | ||
} | ||
|
||
func TestHasKey(t *testing.T) { | ||
testExpression(t, mockContext("bob"), "{haskey {0} {load ../../../cmd/testdata/lookup.txt}}", "1") | ||
testExpression(t, mockContext("nop"), "{haskey {0} {load ../../../cmd/testdata/lookup.txt}}", "") | ||
testExpression(t, mockContext("key"), "{haskey {0} {load ../../../cmd/testdata/lookup.txt}}", "1") | ||
testExpression(t, mockContext("#test"), "{haskey {0} {load ../../../cmd/testdata/lookup.txt}}", "1") | ||
testExpression(t, mockContext("#test"), "{haskey {0} {load ../../../cmd/testdata/lookup.txt} #}", "") | ||
testExpressionErr(t, mockContext(), "{haskey a}", "<ARGN>", ErrArgCount) | ||
testExpressionErr(t, mockContext(), "{haskey a b c d}", "<ARGN>", ErrArgCount) | ||
testExpressionErr(t, mockContext("fn"), "{haskey fn {0}}", "<CONST>", ErrConst) | ||
} | ||
|
||
func BenchmarkLoadFile(b *testing.B) { | ||
benchmarkExpression(b, mockContext(), "{load ../../../cmd/testdata/graph.txt}", "bob 22\njack 93\njill 3\nmaria 19") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package expressions | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestTruthy(t *testing.T) { | ||
assert.True(t, Truthy(TruthyVal)) | ||
assert.False(t, Truthy(FalsyVal)) | ||
assert.False(t, Truthy(" ")) | ||
} | ||
|
||
func TestTruthyStr(t *testing.T) { | ||
assert.Equal(t, TruthyVal, TruthyStr(true)) | ||
assert.Equal(t, FalsyVal, TruthyStr(false)) | ||
} |