forked from go-pg/pg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model_many.go
44 lines (36 loc) · 880 Bytes
/
model_many.go
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
44
package orm
import (
"fmt"
"reflect"
)
type manyModel struct {
*SliceModel
rel *Relation
dstValues map[string][]reflect.Value
}
var _ TableModel = (*manyModel)(nil)
func newManyModel(join *Join) *manyModel {
joinModel := join.JoinModel.(*SliceModel)
dstValues := dstValues(joinModel.Root(), joinModel.Path(), join.BaseModel.Table().PKs)
return &manyModel{
SliceModel: joinModel,
rel: join.Rel,
dstValues: dstValues,
}
}
func (m *manyModel) NewModel() ColumnScanner {
m.strct = reflect.New(m.table.Type).Elem()
m.StructModel.NewModel()
return m
}
func (m *manyModel) AddModel(_ ColumnScanner) error {
id := string(modelId(nil, m.strct, m.rel.FKs))
dstValues, ok := m.dstValues[id]
if !ok {
return fmt.Errorf("pg: can't find dst value for model id=%q", id)
}
for _, v := range dstValues {
v.Set(reflect.Append(v, m.strct))
}
return nil
}