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

io.Reader #4

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
9 changes: 5 additions & 4 deletions struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"errors"
"fmt"
"io"
"io/ioutil"
"os"

Expand Down Expand Up @@ -57,16 +58,16 @@ func (l *Parser) ReadStruct(filename, format string) (interface{}, error) {
return nil, err
}
defer f.Close()
byteValue, _ := ioutil.ReadAll(f)
return l.ParseStruct(byteValue, format)
return l.ParseStruct(f, format)
}

// ParseStruct parses byte slice into map or slice
func (l *Parser) ParseStruct(content []byte, format string) (interface{}, error) {
func (l *Parser) ParseStruct(content io.Reader, format string) (interface{}, error) {
byteValue, _ := ioutil.ReadAll(content)
var out interface{}
var err error
if parser, ok := l.parsers[format]; ok {
out, err = parser(content)
out, err = parser(byteValue)
} else {
return nil, errors.New("Unknown file")
}
Expand Down
2 changes: 1 addition & 1 deletion struct_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func TestParseStruct(t *testing.T) {
[]string{"c", "5"}},
},
Reg: map[string]ParserFunc{
"bfk": func(content []byte) (interface{}, error) {
"bfk": func(content io.Reader) (interface{}, error) {
ret := map[string]interface{}{}
lines := strings.Split(string(content), "\n")
for _, line := range lines {
Expand Down
2 changes: 1 addition & 1 deletion template.go
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,7 @@ func xmlArray(v interface{}, roottag, itemtag string) (string, error) {

func decode(s, format string) (interface{}, error) {
p := NewParser()
res, err := p.ParseStruct([]byte(s), format)
res, err := p.ParseStruct(strings.NewReader(s), format)
if err != nil {
return nil, fmt.Errorf("Unable to parse json '%s': %v", s, err)
}
Expand Down