diff --git a/server/db/settings/setting.go b/server/db/settings/setting.go index e4979e642..b9f07c61d 100644 --- a/server/db/settings/setting.go +++ b/server/db/settings/setting.go @@ -15,8 +15,9 @@ import ( "github.com/evcc-io/evcc/server/db" "github.com/evcc-io/evcc/util" + "github.com/evcc-io/evcc/util/yaml" "github.com/samber/lo" - "go.yaml.in/yaml/v4" + goyaml "go.yaml.in/yaml/v4" "gorm.io/gorm" ) @@ -135,7 +136,7 @@ func SetJson(key string, val any) error { func SetYaml(key string, val any) error { var b bytes.Buffer - err := yaml.NewEncoder(&b).Encode(val) + err := goyaml.NewEncoder(&b).Encode(val) if err == nil { SetString(key, strings.TrimSpace(b.String())) } diff --git a/server/http_config_device_handler.go b/server/http_config_device_handler.go index cc322220d..6eb4128a1 100644 --- a/server/http_config_device_handler.go +++ b/server/http_config_device_handler.go @@ -22,9 +22,9 @@ import ( "github.com/evcc-io/evcc/tariff" "github.com/evcc-io/evcc/util/config" "github.com/evcc-io/evcc/util/templates" + "github.com/evcc-io/evcc/util/yaml" "github.com/evcc-io/evcc/vehicle" "github.com/gorilla/mux" - "go.yaml.in/yaml/v4" ) func devicesConfig[T any](class templates.Class, h config.Handler[T], hidePrivate bool) ([]map[string]any, error) { diff --git a/server/http_config_helper.go b/server/http_config_helper.go index 846a17da9..921449c55 100644 --- a/server/http_config_helper.go +++ b/server/http_config_helper.go @@ -16,9 +16,9 @@ import ( "github.com/evcc-io/evcc/util" "github.com/evcc-io/evcc/util/config" "github.com/evcc-io/evcc/util/templates" + "github.com/evcc-io/evcc/util/yaml" "github.com/go-viper/mapstructure/v2" "github.com/samber/lo" - "go.yaml.in/yaml/v4" ) const ( @@ -468,7 +468,7 @@ func decodeDeviceConfig(r io.Reader) (configReq, error) { // validate yaml syntax; tolerate whitespace/comment-only input var tmp map[string]any - if err := yaml.Unmarshal([]byte(res.Yaml), &tmp); err != nil && !strings.Contains(err.Error(), "no documents in stream") { + if err := yaml.Unmarshal([]byte(res.Yaml), &tmp); err != nil { return configReq{}, err } diff --git a/server/http_global_settings_handler.go b/server/http_global_settings_handler.go index a268e0821..5b3a1dc5a 100644 --- a/server/http_global_settings_handler.go +++ b/server/http_global_settings_handler.go @@ -12,8 +12,8 @@ import ( "github.com/evcc-io/evcc/core/keys" "github.com/evcc-io/evcc/server/db/settings" "github.com/evcc-io/evcc/util/redact" + "github.com/evcc-io/evcc/util/yaml" "github.com/gorilla/mux" - "go.yaml.in/yaml/v4" ) func settingsGetStringHandler(key string) http.HandlerFunc { diff --git a/util/config/custom.go b/util/config/custom.go index 6beed02a5..9a1bc34f5 100644 --- a/util/config/custom.go +++ b/util/config/custom.go @@ -1,8 +1,8 @@ package config import ( + "github.com/evcc-io/evcc/util/yaml" "github.com/spf13/cast" - "go.yaml.in/yaml/v4" ) // CustomDevice promotes an embedded yaml type to the top-level type diff --git a/util/templates/funcmap.go b/util/templates/funcmap.go index d345e7ff2..719a141ab 100644 --- a/util/templates/funcmap.go +++ b/util/templates/funcmap.go @@ -10,7 +10,7 @@ import ( "time" "github.com/Masterminds/sprig/v3" - "go.yaml.in/yaml/v4" + "github.com/evcc-io/evcc/util/yaml" ) func yamlQuote(value string) string { diff --git a/util/templates/render_instance.go b/util/templates/render_instance.go index ebde89e58..89ffaf19a 100644 --- a/util/templates/render_instance.go +++ b/util/templates/render_instance.go @@ -6,7 +6,7 @@ import ( "os" "github.com/evcc-io/evcc/util" - "go.yaml.in/yaml/v4" + "github.com/evcc-io/evcc/util/yaml" ) // Instance is an actual instantiated template diff --git a/util/yaml/yaml.go b/util/yaml/yaml.go new file mode 100644 index 000000000..d4c7b8284 --- /dev/null +++ b/util/yaml/yaml.go @@ -0,0 +1,20 @@ +package yaml + +import ( + "strings" + + goyaml "go.yaml.in/yaml/v4" +) + +func Marshal(data any) ([]byte, error) { + return goyaml.Marshal(data) +} + +// Unmarshal invokes unmarshaler, ignoring empty document errors +func Unmarshal(b []byte, res any) error { + err := goyaml.Unmarshal(b, res) + if err != nil && strings.Contains(err.Error(), "no documents in stream") { + err = nil + } + return err +}