forked from ravendb/ravendb-go-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmulti_loader_with_include.go
66 lines (56 loc) · 1.63 KB
/
multi_loader_with_include.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package ravendb
import (
"reflect"
)
// ILoaderWithInclude is NewMultiLoaderWithInclude
type MultiLoaderWithInclude struct {
session *DocumentSession
includes []string
}
func NewMultiLoaderWithInclude(session *DocumentSession) *MultiLoaderWithInclude {
return &MultiLoaderWithInclude{
session: session,
}
}
func (l *MultiLoaderWithInclude) Include(path string) *MultiLoaderWithInclude {
l.includes = append(l.includes, path)
return l
}
// results should be map[string]*struct
func (l *MultiLoaderWithInclude) LoadMulti(results interface{}, ids []string) error {
if len(ids) == 0 {
return newIllegalArgumentError("ids cannot be empty array")
}
if err := checkValidLoadMultiArg(results, "results"); err != nil {
return err
}
return l.session.loadInternalMulti(results, ids, l.includes)
}
// TODO: needs a test
// TODO: better implementation
func (l *MultiLoaderWithInclude) Load(result interface{}, id string) error {
if id == "" {
return newIllegalArgumentError("id cannot be empty string")
}
// TODO: should allow map[string]interface{} as argument? (and therefore use checkValidLoadArg)
if err := checkIsPtrPtrStruct(result, "result"); err != nil {
return err
}
// create a map[string]typeof(result)
rt := reflect.TypeOf(result)
rt = rt.Elem() // it's now ptr-to-struct
mapType := reflect.MapOf(stringType, rt)
m := reflect.MakeMap(mapType)
ids := []string{id}
err := l.session.loadInternalMulti(m.Interface(), ids, l.includes)
if err != nil {
return err
}
key := reflect.ValueOf(id)
res := m.MapIndex(key)
if res.IsNil() {
//return ErrNotFound
return nil
}
return setInterfaceToValue(result, res.Interface())
}