Skip to content

Commit ed5b6ef

Browse files
authored
Merge pull request #74 from mbenson/loadReader
Load from io.Reader
2 parents 52d9efe + 1d2f669 commit ed5b6ef

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

load.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,15 @@ func (l *Loader) LoadBytes(buf []byte) (*Properties, error) {
5252
return l.loadBytes(buf, l.Encoding)
5353
}
5454

55+
// LoadReader reads an io.Reader into a Properties struct.
56+
func (l *Loader) LoadReader(r io.Reader) (*Properties, error) {
57+
if buf, err := io.ReadAll(r); err != nil {
58+
return nil, err
59+
} else {
60+
return l.loadBytes(buf, l.Encoding)
61+
}
62+
}
63+
5564
// LoadAll reads the content of multiple URLs or files in the given order into
5665
// a Properties struct. If IgnoreMissing is true then a 404 status code or
5766
// 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) {
185194
return l.LoadAll([]string{filename})
186195
}
187196

197+
// LoadReader reads an io.Reader into a Properties struct.
198+
func LoadReader(r io.Reader, enc Encoding) (*Properties, error) {
199+
l := &Loader{Encoding: enc}
200+
return l.LoadReader(r)
201+
}
202+
188203
// LoadFiles reads multiple files in the given order into
189204
// a Properties struct. If 'ignoreMissing' is true then
190205
// non-existent files will not be reported as error.
@@ -224,6 +239,12 @@ func MustLoadString(s string) *Properties {
224239
return must(LoadString(s))
225240
}
226241

242+
// MustLoadSReader reads an io.Reader into a Properties struct and
243+
// panics on error.
244+
func MustLoadReader(r io.Reader, enc Encoding) *Properties {
245+
return must(LoadReader(r, enc))
246+
}
247+
227248
// MustLoadFile reads a file into a Properties struct and
228249
// panics on error.
229250
func MustLoadFile(filename string, enc Encoding) *Properties {

load_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,21 @@ func TestLoadAll(t *testing.T) {
166166
assertKeyValues(t, "", p, "key", "value4", "key2", "value2")
167167
}
168168

169+
func TestLoadReader(t *testing.T) {
170+
tf := make(tempFiles, 0)
171+
defer tf.removeAll()
172+
173+
filename := tf.makeFile("key=value")
174+
r, err := os.Open(filename)
175+
if err != nil {
176+
t.Fatal(err)
177+
}
178+
p := MustLoadReader(r, ISO_8859_1)
179+
180+
assert.Equal(t, p.Len(), 1)
181+
assertKeyValues(t, "", p, "key", "value")
182+
}
183+
169184
type tempFiles []string
170185

171186
func (tf *tempFiles) removeAll() {

0 commit comments

Comments
 (0)