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

Add slice iterator for dbiter #9

Merged
merged 1 commit into from
Jan 31, 2024
Merged
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
70 changes: 68 additions & 2 deletions dbutil/iter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

package dbutil

import "errors"

var ErrAlreadyIterated = errors.New("this iterator has been already iterated")

// RowIter is a wrapper for [Rows] that allows conveniently iterating over rows
// with a predefined scanner function.
type RowIter[T any] interface {
Expand All @@ -23,13 +27,20 @@ type RowIter[T any] interface {
type rowIterImpl[T any] struct {
Rows
ConvertRow func(Scannable) (T, error)

err error
}

// NewRowIter creates a new RowIter from the given Rows and scanner function.
func NewRowIter[T any](rows Rows, convertFn func(Scannable) (T, error)) RowIter[T] {
return &rowIterImpl[T]{Rows: rows, ConvertRow: convertFn}
}

// NewRowIterWithError creates a new RowIter from the given Rows and scanner function with default error. If not nil, it will be returned without calling iterator function.
func NewRowIterWithError[T any](rows Rows, convertFn func(Scannable) (T, error), err error) RowIter[T] {
return &rowIterImpl[T]{Rows: rows, ConvertRow: convertFn, err: err}
}

func ScanSingleColumn[T any](rows Scannable) (val T, err error) {
err = rows.Scan(&val)
return
Expand All @@ -46,21 +57,32 @@ func ScanDataStruct[T NewableDataStruct[T]](rows Scannable) (T, error) {
}

func (i *rowIterImpl[T]) Iter(fn func(T) (bool, error)) error {
if i == nil || i.Rows == nil {
if i == nil {
return nil
} else if i.Rows == nil || i.err != nil {
return i.err
}
defer i.Rows.Close()

for i.Rows.Next() {
if item, err := i.ConvertRow(i.Rows); err != nil {
i.err = err
return err
} else if cont, err := fn(item); err != nil {
i.err = err
return err
} else if !cont {
break
}
}
return i.Rows.Err()

err := i.Rows.Err()
if err != nil {
i.err = err
} else {
i.err = ErrAlreadyIterated
}
return err
}

func (i *rowIterImpl[T]) AsList() (list []T, err error) {
Expand All @@ -70,3 +92,47 @@ func (i *rowIterImpl[T]) AsList() (list []T, err error) {
})
return
}

type sliceIterImpl[T any] struct {
items []T
err error
}

func NewSliceIter[T any](items []T) RowIter[T] {
return &sliceIterImpl[T]{items: items}
}

func NewSliceIterWithError[T any](items []T, err error) RowIter[T] {
return &sliceIterImpl[T]{items: items, err: err}
}

func (i *sliceIterImpl[T]) Iter(fn func(T) (bool, error)) error {
if i == nil {
return nil
} else if i.err != nil {
return i.err
}

for _, item := range i.items {
if cont, err := fn(item); err != nil {
i.err = err
return err
} else if !cont {
break
}
}

i.err = ErrAlreadyIterated
return nil
}

func (i *sliceIterImpl[T]) AsList() ([]T, error) {
if i == nil {
return nil, nil
} else if i.err != nil {
return nil, i.err
}

i.err = ErrAlreadyIterated
return i.items, nil
}
Loading