Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Load from io.Reader #74

Merged
merged 2 commits into from
Dec 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions load.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package properties

import (
"fmt"
"io/ioutil"
"io"
"net/http"
"os"
"strings"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -91,7 +100,7 @@ func (l *Loader) LoadAll(names []string) (*Properties, error) {
// If IgnoreMissing is true then a missing file will not be
// reported as error.
func (l *Loader) LoadFile(filename string) (*Properties, error) {
data, err := ioutil.ReadFile(filename)
data, err := os.ReadFile(filename)
if err != nil {
if l.IgnoreMissing && os.IsNotExist(err) {
LogPrintf("properties: %s not found. skipping", filename)
Expand Down Expand Up @@ -126,7 +135,7 @@ func (l *Loader) LoadURL(url string) (*Properties, error) {
return nil, fmt.Errorf("properties: %s returned %d", url, resp.StatusCode)
}

body, err := ioutil.ReadAll(resp.Body)
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("properties: %s error reading response. %s", url, err)
}
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down
20 changes: 17 additions & 3 deletions load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package properties

import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
Expand Down Expand Up @@ -167,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() {
Expand All @@ -183,9 +197,9 @@ func (tf *tempFiles) makeFile(data string) string {
}

func (tf *tempFiles) makeFilePrefix(prefix, data string) string {
f, err := ioutil.TempFile("", prefix)
f, err := os.CreateTemp("", prefix)
if err != nil {
panic("ioutil.TempFile: " + err.Error())
panic("os.TempFile: " + err.Error())
}

// remember the temp file so that we can remove it later
Expand Down