Skip to content

Commit

Permalink
eval: Allow map key as expression
Browse files Browse the repository at this point in the history
If the first level trying to eval has characters like `-` or `.` in them, it's currently impossible to eval
With this PR, we can do `-e '["my-key"]'`
  • Loading branch information
julienduchesne committed Aug 3, 2023
1 parent 08e6a00 commit c5c02ab
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
3 changes: 1 addition & 2 deletions cmd/tk/jsonnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"encoding/json"
"fmt"

"github.com/go-clix/cli"

Expand All @@ -25,7 +24,7 @@ func evalCmd() *cli.Command {
JsonnetOpts: getJsonnetOpts(),
}
if *evalPattern != "" {
jsonnetOpts.EvalScript = fmt.Sprintf(tanka.PatternEvalScript, *evalPattern)
jsonnetOpts.EvalScript = tanka.PatternEvalScript(*evalPattern)
}
raw, err := tanka.Eval(args[0], jsonnetOpts)

Expand Down
7 changes: 6 additions & 1 deletion pkg/tanka/evaluators.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,12 @@ function(%s)
return raw, nil
}

const PatternEvalScript = "main.%s"
func PatternEvalScript(expr string) string {
if strings.HasPrefix(expr, "[") {
return fmt.Sprintf("main%s", expr)
}
return fmt.Sprintf("main.%s", expr)
}

// MetadataEvalScript finds the Environment object (without its .data object)
const MetadataEvalScript = `
Expand Down

0 comments on commit c5c02ab

Please sign in to comment.