Yaml: fix decoding empty stream everywhere (#29690)

This commit is contained in:
andig 2026-05-06 10:08:23 +02:00 • committed by GitHub
parent 0d3bcfe8dd
commit 42b5cb2b49
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 30 additions and 9 deletions

View file

@ -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()))
}

View file

@ -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) {

View file

@ -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
}

View file

@ -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 {

View file

@ -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

View file

@ -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 {

View file

@ -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

20
util/yaml/yaml.go Normal file
View file

@ -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
}