model_slice.go
842 字节
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package orm
import (
"reflect"
"github.com/go-pg/pg/internal"
"github.com/go-pg/pg/types"
)
type sliceModel struct {
Discard
slice reflect.Value
nextElem func() reflect.Value
scan func(reflect.Value, types.Reader, int) error
}
var _ Model = (*sliceModel)(nil)
func newSliceModel(slice reflect.Value, elemType reflect.Type) *sliceModel {
return &sliceModel{
slice: slice,
scan: types.Scanner(elemType),
}
}
func (m *sliceModel) Init() error {
if m.slice.IsValid() && m.slice.Len() > 0 {
m.slice.Set(m.slice.Slice(0, 0))
}
return nil
}
func (m *sliceModel) NewModel() ColumnScanner {
return m
}
func (m *sliceModel) ScanColumn(colIdx int, _ string, rd types.Reader, n int) error {
if m.nextElem == nil {
m.nextElem = internal.MakeSliceNextElemFunc(m.slice)
}
v := m.nextElem()
return m.scan(v, rd, n)
}