-
Notifications
You must be signed in to change notification settings - Fork 14
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
- Loading branch information
Showing
9 changed files
with
228 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,5 @@ | ||
#test 22 | ||
bob 33 | ||
jill 44 | ||
|
||
cat 55 |
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,105 @@ | ||
package stdlib | ||
|
||
import ( | ||
"bufio" | ||
"io" | ||
"os" | ||
"rare/pkg/expressions" | ||
"strings" | ||
) | ||
|
||
// {load "filename"} | ||
// loads static file as string | ||
func kfLoadFile(args []expressions.KeyBuilderStage) (expressions.KeyBuilderStage, error) { | ||
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 stageError(newFuncErr("<FILE>", "Unable to open file: "+filename)) | ||
} | ||
defer f.Close() | ||
|
||
content, err := io.ReadAll(f) | ||
if err != nil { | ||
return stageError(newFuncErr("<FILE>", "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 len(args) != 2 { | ||
return stageErrArgCount(args, 2) | ||
} | ||
|
||
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,19 @@ | ||
package stdlib | ||
|
||
import "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) | ||
} | ||
|
||
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} \"#\"}", "") | ||
} | ||
|
||
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