2016-11-14 00:04:21 -06:00
|
|
|
package repl
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"sort"
|
2018-05-03 22:44:55 -05:00
|
|
|
"strconv"
|
2016-11-14 00:04:21 -06:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// FormatResult formats the given result value for human-readable output.
|
|
|
|
//
|
|
|
|
// The value must currently be a string, list, map, and any nested values
|
|
|
|
// with those same types.
|
|
|
|
func FormatResult(value interface{}) (string, error) {
|
2018-03-28 14:03:29 -05:00
|
|
|
return formatResult(value, false)
|
2016-11-14 00:04:21 -06:00
|
|
|
}
|
|
|
|
|
2018-03-28 14:03:29 -05:00
|
|
|
func formatResult(value interface{}, nested bool) (string, error) {
|
2018-05-03 22:44:55 -05:00
|
|
|
if value == nil {
|
|
|
|
return "null", nil
|
|
|
|
}
|
2016-11-14 00:04:21 -06:00
|
|
|
switch output := value.(type) {
|
|
|
|
case string:
|
2018-03-28 14:03:29 -05:00
|
|
|
if nested {
|
|
|
|
return fmt.Sprintf("%q", output), nil
|
|
|
|
}
|
2016-11-14 00:04:21 -06:00
|
|
|
return output, nil
|
2018-05-03 22:44:55 -05:00
|
|
|
case int:
|
|
|
|
return strconv.Itoa(output), nil
|
|
|
|
case float64:
|
|
|
|
return fmt.Sprintf("%g", output), nil
|
|
|
|
case bool:
|
|
|
|
switch {
|
|
|
|
case output == true:
|
|
|
|
return "true", nil
|
|
|
|
default:
|
|
|
|
return "false", nil
|
|
|
|
}
|
2016-11-14 00:04:21 -06:00
|
|
|
case []interface{}:
|
|
|
|
return formatListResult(output)
|
|
|
|
case map[string]interface{}:
|
|
|
|
return formatMapResult(output)
|
|
|
|
default:
|
|
|
|
return "", fmt.Errorf("unknown value type: %T", value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func formatListResult(value []interface{}) (string, error) {
|
|
|
|
var outputBuf bytes.Buffer
|
|
|
|
outputBuf.WriteString("[")
|
|
|
|
if len(value) > 0 {
|
|
|
|
outputBuf.WriteString("\n")
|
|
|
|
}
|
|
|
|
|
2018-03-28 14:03:29 -05:00
|
|
|
for _, v := range value {
|
|
|
|
raw, err := formatResult(v, true)
|
2016-11-14 00:04:21 -06:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
outputBuf.WriteString(indent(raw))
|
2018-03-28 14:03:29 -05:00
|
|
|
outputBuf.WriteString(",\n")
|
2016-11-14 00:04:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
outputBuf.WriteString("]")
|
|
|
|
return outputBuf.String(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func formatMapResult(value map[string]interface{}) (string, error) {
|
|
|
|
ks := make([]string, 0, len(value))
|
|
|
|
for k, _ := range value {
|
|
|
|
ks = append(ks, k)
|
|
|
|
}
|
|
|
|
sort.Strings(ks)
|
|
|
|
|
|
|
|
var outputBuf bytes.Buffer
|
|
|
|
outputBuf.WriteString("{")
|
|
|
|
if len(value) > 0 {
|
|
|
|
outputBuf.WriteString("\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, k := range ks {
|
|
|
|
v := value[k]
|
2018-03-28 14:03:29 -05:00
|
|
|
rawK, err := formatResult(k, true)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
rawV, err := formatResult(v, true)
|
2016-11-14 00:04:21 -06:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2018-03-28 14:03:29 -05:00
|
|
|
outputBuf.WriteString(indent(fmt.Sprintf("%s = %s", rawK, rawV)))
|
|
|
|
outputBuf.WriteString("\n")
|
2016-11-14 00:04:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
outputBuf.WriteString("}")
|
|
|
|
return outputBuf.String(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func indent(value string) string {
|
|
|
|
var outputBuf bytes.Buffer
|
|
|
|
s := bufio.NewScanner(strings.NewReader(value))
|
2018-03-28 14:03:29 -05:00
|
|
|
newline := false
|
2016-11-14 00:04:21 -06:00
|
|
|
for s.Scan() {
|
2018-03-28 14:03:29 -05:00
|
|
|
if newline {
|
|
|
|
outputBuf.WriteByte('\n')
|
|
|
|
}
|
2016-11-14 00:04:21 -06:00
|
|
|
outputBuf.WriteString(" " + s.Text())
|
2018-03-28 14:03:29 -05:00
|
|
|
newline = true
|
2016-11-14 00:04:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return outputBuf.String()
|
|
|
|
}
|