The open and composable observability and data visualization platform. Visualize metrics, logs, and traces from multiple sources like Prometheus, Loki, Elasticsearch, InfluxDB, Postgres and many more.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
grafana/pkg/apis/query/v0alpha1/template/format.go

68 lines
1.3 KiB

package template
import (
"bytes"
"encoding/csv"
"encoding/json"
"strings"
)
func FormatVariables(fmt VariableFormat, input []string) string {
if len(input) < 1 {
return ""
}
// MultiValued formats
// nolint: exhaustive
switch fmt {
case FormatJSON:
v, _ := json.Marshal(input)
return string(v)
case FormatDoubleQuote:
sb := bytes.NewBufferString("")
for idx, val := range input {
if idx > 0 {
_, _ = sb.WriteRune(',')
}
_, _ = sb.WriteRune('"')
_, _ = sb.WriteString(strings.ReplaceAll(val, `"`, `\"`))
_, _ = sb.WriteRune('"')
}
return sb.String()
case FormatSingleQuote:
sb := bytes.NewBufferString("")
for idx, val := range input {
if idx > 0 {
_, _ = sb.WriteRune(',')
}
_, _ = sb.WriteRune('\'')
_, _ = sb.WriteString(strings.ReplaceAll(val, `'`, `\'`))
_, _ = sb.WriteRune('\'')
}
return sb.String()
case FormatCSV:
sb := bytes.NewBufferString("")
w := csv.NewWriter(sb)
_ = w.Write(input)
w.Flush()
v := sb.Bytes()
return string(v[:len(v)-1])
}
// Single valued formats
if len(input) == 1 {
return input[0]
}
// nolint: exhaustive
switch fmt {
case FormatPipe:
return strings.Join(input, "|")
}
// Raw output (joined with a comma)
return strings.Join(input, ",")
}