mirror of
https://github.com/imjasonh/csvstruct
synced 2026-07-22 16:00:05 +00:00
support DecodeNext(nil) to skip a line
This commit is contained in:
parent
905ef96dff
commit
2731c3991a
2 changed files with 37 additions and 2 deletions
|
|
@ -1,4 +1,3 @@
|
|||
// TODO: support DecodeNext(nil) to skip a line
|
||||
// TODO: NewEncoder/EncodeNext -- header will be fields in first item...
|
||||
// TODO: Encode/Decode map[string]string
|
||||
|
||||
|
|
@ -34,7 +33,13 @@ func NewDecoder(r io.Reader) Decoder {
|
|||
// DecodeNext reads the next CSV-encoded value from its input and stores it in the value pointed to by v.
|
||||
func (d *decoder) DecodeNext(v interface{}) error {
|
||||
rv := reflect.ValueOf(v)
|
||||
if rv.Kind() != reflect.Ptr || rv.IsNil() {
|
||||
|
||||
// v is nil, skip this line and proceed.
|
||||
if rv.Kind() == reflect.Invalid {
|
||||
_, err := d.read()
|
||||
return err
|
||||
}
|
||||
if rv.Kind() != reflect.Ptr {
|
||||
return errors.New("must be pointer")
|
||||
}
|
||||
rv = rv.Elem()
|
||||
|
|
|
|||
|
|
@ -58,6 +58,9 @@ d
|
|||
if !reflect.DeepEqual(rows, c.out) {
|
||||
t.Errorf("unexpected result, got %v, want %v", rows, c.out)
|
||||
}
|
||||
if !isDone(d) {
|
||||
t.Errorf("decoder unexpectedly not done")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -128,6 +131,33 @@ a,b`)).DecodeNext(&r); err != nil {
|
|||
}
|
||||
}
|
||||
|
||||
func TestDecode_DecodeNil(t *testing.T) {
|
||||
type row struct {
|
||||
Foo, Bar string
|
||||
}
|
||||
d := NewDecoder(strings.NewReader(`Foo,Bar
|
||||
ignore,this
|
||||
a,b`))
|
||||
if err := d.DecodeNext(nil); err != nil {
|
||||
t.Errorf("unexpected error while skipping line: %v", err)
|
||||
}
|
||||
var r row
|
||||
if err := d.DecodeNext(&r); err != nil {
|
||||
t.Errorf("unexpected error decoding after skip: %v", err)
|
||||
}
|
||||
exp := row{"a", "b"}
|
||||
if r != exp {
|
||||
t.Errorf("unexpected result, got %v, want %v", r, exp)
|
||||
}
|
||||
if !isDone(d) {
|
||||
t.Errorf("decoder unexpectedly not done")
|
||||
}
|
||||
}
|
||||
|
||||
func isDone(d Decoder) bool {
|
||||
return d.DecodeNext(nil) == io.EOF
|
||||
}
|
||||
|
||||
func ExampleDecoder_DecodeNext() {
|
||||
csv := `Foo,Bar,Baz
|
||||
a,b,c
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue