-
-
Notifications
You must be signed in to change notification settings - Fork 522
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor things a wee bit to add a decoder struct. This doesn't really *do* anything right now, but will be needed to add options, such as "strict mode" in #197 or "extended errors" in #299. Also add DecodeFS() as a companion to DecodeFile() to read from a fs.FS, and since using a reader is the default with NewDecoder() (and was already easy to do before with strings.NewReader()) I marked DecodeReader() as deprecated.
- Loading branch information
Showing
4 changed files
with
89 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// +build go1.16 | ||
|
||
package toml | ||
|
||
import ( | ||
"io/fs" | ||
) | ||
|
||
// DecodeFS is just like Decode, except it will automatically read the contents | ||
// of the file at `fpath` from a fs.FS instance. | ||
func DecodeFS(fsys fs.FS, fpath string, v interface{}) (MetaData, error) { | ||
fp, err := fsys.Open(fpath) | ||
if err != nil { | ||
return MetaData{}, err | ||
} | ||
defer fp.Close() | ||
return NewDecoder(fp).Decode(v) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// +build go1.16 | ||
|
||
package toml | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
"testing/fstest" | ||
) | ||
|
||
func TestDecodeFS(t *testing.T) { | ||
fsys := fstest.MapFS{ | ||
"test.toml": &fstest.MapFile{ | ||
Data: []byte("a = 42"), | ||
}, | ||
} | ||
|
||
var i struct{ A int } | ||
meta, err := DecodeFS(fsys, "test.toml", &i) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
have := fmt.Sprintf("%v %v %v", i, meta.Keys(), meta.Type("a")) | ||
want := "{42} [a] Integer" | ||
if have != want { | ||
t.Errorf("\nhave: %s\nwant: %s", have, want) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters