Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a -max_executions flag and config option to limit evaluations #51

Merged
merged 4 commits into from
Apr 26, 2024
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
15 changes: 10 additions & 5 deletions mito.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func Main() int {
}
use := flag.String("use", "all", "libraries to use")
data := flag.String("data", "", "path to a JSON object holding input (exposed as the label "+root+")")
maxExecutions := flag.Int("max_executions", -1, "maximum number of evaluations, or no maximum if -1")
cfgPath := flag.String("cfg", "", "path to a YAML file holding configuration for global vars and regular expressions")
insecure := flag.Bool("insecure", false, "disable TLS verification in the HTTP client")
version := flag.Bool("version", false, "print version and exit")
Expand Down Expand Up @@ -144,6 +145,9 @@ func Main() int {
libMap["http"] = lib.HTTP(setClientInsecure(client, *insecure), nil, nil)
}
}
if *maxExecutions == -1 && cfg.MaxExecutions != nil {
*maxExecutions = *cfg.MaxExecutions
}
}
if libMap["http"] == nil {
libMap["http"] = lib.HTTP(setClientInsecure(nil, *insecure), nil, nil)
Expand Down Expand Up @@ -183,7 +187,7 @@ func Main() int {
input = map[string]interface{}{root: input}
}

for {
for n := int(0); *maxExecutions < 0 || n < *maxExecutions; n++ {
res, val, err := eval(string(b), root, input, libs...)
if err != nil {
fmt.Fprintln(os.Stderr, err)
Expand Down Expand Up @@ -386,10 +390,11 @@ func toUpper(p []byte) {
}

type config struct {
Globals map[string]interface{} `yaml:"globals"`
Regexps map[string]string `yaml:"regexp"`
XSDs map[string]string `yaml:"xsd"`
Auth *authConfig `yaml:"auth"`
Globals map[string]interface{} `yaml:"globals"`
Regexps map[string]string `yaml:"regexp"`
XSDs map[string]string `yaml:"xsd"`
Auth *authConfig `yaml:"auth"`
MaxExecutions *int `yaml:"max_executions"`
}

type authConfig struct {
Expand Down
20 changes: 20 additions & 0 deletions testdata/want_more_max.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
mito -data state.json -max_executions 2 src.cel
! stderr .
cmp stdout want.txt

-- state.json --
{"n": 0}
-- src.cel --
int(state.n).as(n, {
"n": n+1,
"want_more": n+1 < 5,
})
-- want.txt --
{
"n": 1,
"want_more": true
}
{
"n": 2,
"want_more": true
}
Loading