Treat empty or all-comments yaml file as no-op when loading it
This commit is contained in:
parent
88c6b7e1fe
commit
40b2db580e
@ -1,7 +1,9 @@
|
||||
package yamlutil
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
@ -10,14 +12,15 @@ import (
|
||||
// LoadYamlFile reads the file at the given path and unmarshals it into the
|
||||
// given pointer.
|
||||
func LoadYamlFile(into any, path string) error {
|
||||
|
||||
file, err := os.Open(path)
|
||||
if err != nil {
|
||||
return fmt.Errorf("opening file: %w", err)
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
if err = yaml.NewDecoder(file).Decode(into); err != nil {
|
||||
if err = yaml.NewDecoder(file).Decode(into); errors.Is(err, io.EOF) {
|
||||
return nil
|
||||
} else if err != nil {
|
||||
return fmt.Errorf("decoding yaml: %w", err)
|
||||
}
|
||||
|
||||
|
56
go/yamlutil/yamlutil_test.go
Normal file
56
go/yamlutil/yamlutil_test.go
Normal file
@ -0,0 +1,56 @@
|
||||
package yamlutil
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestLoadYamlFile(t *testing.T) {
|
||||
type structA struct {
|
||||
A int
|
||||
B string
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
body string
|
||||
want any
|
||||
}{
|
||||
{
|
||||
name: "empty body",
|
||||
body: "",
|
||||
want: structA{},
|
||||
},
|
||||
{
|
||||
name: "all comments",
|
||||
body: `# foo
|
||||
# bar`,
|
||||
want: structA{},
|
||||
},
|
||||
{
|
||||
name: "success",
|
||||
body: "a: 1\nb: foo\n",
|
||||
want: structA{A: 1, B: "foo"},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
var (
|
||||
dir = t.TempDir()
|
||||
path = filepath.Join(dir, "f.yaml")
|
||||
intoV = reflect.New(reflect.TypeOf(test.want))
|
||||
into = intoV.Interface()
|
||||
)
|
||||
|
||||
require.NoError(t, os.WriteFile(path, []byte(test.body), 0444))
|
||||
assert.NoError(t, LoadYamlFile(into, path))
|
||||
assert.Equal(t, test.want, intoV.Elem().Interface())
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user