Skip to content

Commit

Permalink
Attempt to unquote predict input strings
Browse files Browse the repository at this point in the history
We want to unescape things like `\n` that are escaped to `\\n` when
serialized, but still fall back to the raw value if that fails for any
reason.

Signed-off-by: F <[email protected]>
  • Loading branch information
erbridge committed Aug 23, 2023
1 parent a6a26b7 commit 845af15
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions pkg/cli/predict.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"os"
"os/signal"
"strconv"
"strings"
"syscall"

Expand Down Expand Up @@ -278,19 +279,24 @@ func handleMultipleFileOutput(prediction *predict.Response, outputSchema *openap
func parseInputFlags(inputs []string) (predict.Inputs, error) {
keyVals := map[string]string{}
for _, input := range inputs {
var name, value string

// Default input name is "input"
if !strings.Contains(input, "=") {
return nil, fmt.Errorf("Failed to parse input '%s', expected format is 'name=value'", input)
}

split := strings.SplitN(input, "=", 2)
name = split[0]
value = split[1]

if strings.HasPrefix(value, `"`) && strings.HasSuffix(value, `"`) {
value = value[1 : len(value)-1]
name := split[0]
rawValue := split[1]

value := rawValue
// If the value isn't already wrapped in quotation marks, do it now, so
// `strconv.Unquote` will parse it correctly.
if !strings.HasPrefix(value, `"`) && !strings.HasSuffix(value, `"`) {
value = `"` + value + `"`
}
value, err := strconv.Unquote(value)
if err != nil {
// Fall back to the raw value if anything goes wrong.
value = rawValue
}

keyVals[name] = value
Expand Down

0 comments on commit 845af15

Please sign in to comment.