evcc-io/util/templates/utils.go
andig 560ba89734
Some checks failed
Release / call-build-workflow (push) Has been cancelled
Release / Publish Docker :release (push) Has been cancelled
Release / Github & APT (push) Has been cancelled
Release / Demo (push) Has been cancelled
Release / Hassio Addon (push) Has been cancelled
Fritzdect: fix double-quoting of ain
2023-02-24 17:43:13 +01:00

33 lines
641 B
Go

package templates
import (
"fmt"
"strings"
"gopkg.in/yaml.v3"
)
func quote(value string) string {
quoted := strings.ReplaceAll(value, `'`, `''`)
return fmt.Sprintf("'%s'", quoted)
}
// yamlQuote quotes strings for yaml if they would otherwise by modified by the unmarshaler
func yamlQuote(value string) string {
input := fmt.Sprintf("key: %s", value)
var res struct {
Value string `yaml:"key"`
}
if err := yaml.Unmarshal([]byte(input), &res); err != nil || value != res.Value {
return quote(value)
}
// fix 0815, but not 0
if strings.HasPrefix(value, "0") && len(value) > 1 {
return quote(value)
}
return value
}