Issue #11: Add LoadString
diff --git a/CHANGELOG.md b/CHANGELOG.md index 160cd83..b82f095 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md
@@ -1,5 +1,9 @@ ## Changelog +### [1.6.1](https://github.com/magiconair/properties/tags/v1.6.1) - unreleased + + * [Issue #11](https://github.com/magiconair/properties/issues/11): Add [LoadString](http://godoc.org/github.com/magiconair/properties#Properties.LoadString) method to load properties from an UTF8 string. + ### [1.6.0](https://github.com/magiconair/properties/tags/v1.6.0) - 11 Dec 2015 * Add [Decode](http://godoc.org/github.com/magiconair/properties#Properties.Decode) method to populate struct from properties via tags.
diff --git a/load.go b/load.go index 431d462..c983d78 100644 --- a/load.go +++ b/load.go
@@ -26,6 +26,11 @@ return loadBuf(buf, enc) } +// LoadString reads an UTF8 string into a properties struct. +func LoadString(s string) (*Properties, error) { + return loadBuf([]byte(s), UTF8) +} + // LoadFile reads a file into a Properties struct. func LoadFile(filename string, enc Encoding) (*Properties, error) { return loadFiles([]string{filename}, enc, false) @@ -38,27 +43,30 @@ return loadFiles(filenames, enc, ignoreMissing) } +// MustLoadString reads an UTF8 string into a Properties struct and +// panics on error. +func MustLoadString(s string) *Properties { + return must(LoadString(s)) +} + // MustLoadFile reads a file into a Properties struct and // panics on error. func MustLoadFile(filename string, enc Encoding) *Properties { - return mustLoadFiles([]string{filename}, enc, false) + return must(LoadFile(filename, enc)) } // MustLoadFiles reads multiple files in the given order into // a Properties struct and panics on error. If 'ignoreMissing' // is true then non-existent files will not be reported as error. func MustLoadFiles(filenames []string, enc Encoding, ignoreMissing bool) *Properties { - return mustLoadFiles(filenames, enc, ignoreMissing) + return must(LoadFiles(filenames, enc, ignoreMissing)) } -// ---------------------------------------------------------------------------- - func loadBuf(buf []byte, enc Encoding) (*Properties, error) { p, err := parse(convert(buf, enc)) if err != nil { return nil, err } - return p, p.check() } @@ -88,8 +96,7 @@ return loadBuf(buff, enc) } -func mustLoadFiles(filenames []string, enc Encoding, ignoreMissing bool) *Properties { - p, err := loadFiles(filenames, enc, ignoreMissing) +func must(p *Properties, err error) *Properties { if err != nil { ErrorHandler(err) }
diff --git a/load_test.go b/load_test.go index e6dae8d..01fe321 100644 --- a/load_test.go +++ b/load_test.go
@@ -17,11 +17,7 @@ tempFiles []string } -var ( - _ = Suite(&LoadSuite{}) -) - -// ---------------------------------------------------------------------------- +var _ = Suite(&LoadSuite{}) func (s *LoadSuite) TestLoadFailsWithNotExistingFile(c *C) { _, err := LoadFile("doesnotexist.properties", ISO_8859_1) @@ -29,25 +25,26 @@ c.Assert(err, ErrorMatches, "open.*no such file or directory") } -// ---------------------------------------------------------------------------- - func (s *LoadSuite) TestLoadFilesFailsOnNotExistingFile(c *C) { _, err := LoadFiles([]string{"doesnotexist.properties"}, ISO_8859_1, false) c.Assert(err, NotNil) c.Assert(err, ErrorMatches, "open.*no such file or directory") } -// ---------------------------------------------------------------------------- - func (s *LoadSuite) TestLoadFilesDoesNotFailOnNotExistingFileAndIgnoreMissing(c *C) { p, err := LoadFiles([]string{"doesnotexist.properties"}, ISO_8859_1, true) c.Assert(err, IsNil) c.Assert(p.Len(), Equals, 0) } -// ---------------------------------------------------------------------------- +func (s *LoadSuite) TestLoadString(c *C) { + x := "key=äüö" + p1 := MustLoadString(x) + p2 := must(Load([]byte(x), UTF8)) + c.Assert(p1, DeepEquals, p2) +} -func (s *LoadSuite) TestLoad(c *C) { +func (s *LoadSuite) TestLoadFile(c *C) { filename := s.makeFile(c, "key=value") p := MustLoadFile(filename, ISO_8859_1) @@ -55,8 +52,6 @@ assertKeyValues(c, "", p, "key", "value") } -// ---------------------------------------------------------------------------- - func (s *LoadSuite) TestLoadFiles(c *C) { filename := s.makeFile(c, "key=value") filename2 := s.makeFile(c, "key2=value2") @@ -64,8 +59,6 @@ assertKeyValues(c, "", p, "key", "value", "key2", "value2") } -// ---------------------------------------------------------------------------- - func (s *LoadSuite) TestLoadExpandedFile(c *C) { filename := s.makeFilePrefix(c, os.Getenv("USER"), "key=value") filename = strings.Replace(filename, os.Getenv("USER"), "${USER}", -1) @@ -73,8 +66,6 @@ assertKeyValues(c, "", p, "key", "value") } -// ---------------------------------------------------------------------------- - func (s *LoadSuite) TestLoadFilesAndIgnoreMissing(c *C) { filename := s.makeFile(c, "key=value") filename2 := s.makeFile(c, "key2=value2") @@ -82,14 +73,10 @@ assertKeyValues(c, "", p, "key", "value", "key2", "value2") } -// ---------------------------------------------------------------------------- - func (s *LoadSuite) SetUpSuite(c *C) { s.tempFiles = make([]string, 0) } -// ---------------------------------------------------------------------------- - func (s *LoadSuite) TearDownSuite(c *C) { for _, path := range s.tempFiles { err := os.Remove(path) @@ -99,14 +86,10 @@ } } -// ---------------------------------------------------------------------------- - func (s *LoadSuite) makeFile(c *C, data string) string { return s.makeFilePrefix(c, "properties", data) } -// ---------------------------------------------------------------------------- - func (s *LoadSuite) makeFilePrefix(c *C, prefix, data string) string { f, err := ioutil.TempFile("", prefix) if err != nil {