diff --git a/load.go b/load.go index b2ef6c7..6567e0c 100644 --- a/load.go +++ b/load.go @@ -52,6 +52,15 @@ func (l *Loader) LoadBytes(buf []byte) (*Properties, error) { return l.loadBytes(buf, l.Encoding) } +// LoadReader reads an io.Reader into a Properties struct. +func (l *Loader) LoadReader(r io.Reader) (*Properties, error) { + if buf, err := io.ReadAll(r); err != nil { + return nil, err + } else { + return l.loadBytes(buf, l.Encoding) + } +} + // LoadAll reads the content of multiple URLs or files in the given order into // a Properties struct. If IgnoreMissing is true then a 404 status code or // missing file will not be reported as error. Encoding sets the encoding for @@ -185,6 +194,12 @@ func LoadFile(filename string, enc Encoding) (*Properties, error) { return l.LoadAll([]string{filename}) } +// LoadReader reads an io.Reader into a Properties struct. +func LoadReader(r io.Reader, enc Encoding) (*Properties, error) { + l := &Loader{Encoding: enc} + return l.LoadReader(r) +} + // LoadFiles reads multiple files in the given order into // a Properties struct. If 'ignoreMissing' is true then // non-existent files will not be reported as error. @@ -224,6 +239,12 @@ func MustLoadString(s string) *Properties { return must(LoadString(s)) } +// MustLoadSReader reads an io.Reader into a Properties struct and +// panics on error. +func MustLoadReader(r io.Reader, enc Encoding) *Properties { + return must(LoadReader(r, enc)) +} + // MustLoadFile reads a file into a Properties struct and // panics on error. func MustLoadFile(filename string, enc Encoding) *Properties { diff --git a/load_test.go b/load_test.go index 6863425..cdff623 100644 --- a/load_test.go +++ b/load_test.go @@ -166,6 +166,21 @@ func TestLoadAll(t *testing.T) { assertKeyValues(t, "", p, "key", "value4", "key2", "value2") } +func TestLoadReader(t *testing.T) { + tf := make(tempFiles, 0) + defer tf.removeAll() + + filename := tf.makeFile("key=value") + r, err := os.Open(filename) + if err != nil { + t.Fatal(err) + } + p := MustLoadReader(r, ISO_8859_1) + + assert.Equal(t, p.Len(), 1) + assertKeyValues(t, "", p, "key", "value") +} + type tempFiles []string func (tf *tempFiles) removeAll() {