-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipe.go
49 lines (39 loc) · 935 Bytes
/
pipe.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
package trafo
import (
ae "appengine"
ds "appengine/datastore"
)
// DocsPipe can load/save datastore entries from/to entities.
type DocsPipe struct {
ctx ae.Context
Docs []*Doc
}
// Properties returns a sequence of datastore.PropertyLoadSaver.
func (p *DocsPipe) Properties() []ds.PropertyLoadSaver {
pipes := make([]ds.PropertyLoadSaver, len(p.Docs))
for i, doc := range p.Docs {
pipes[i] = &docPipe{p.ctx, doc}
}
return pipes
}
// docPipe can load/save datastore properties from/to an entity.
// It implements the datastore's PropertyLoadSaver.
type docPipe struct {
ctx ae.Context
doc *Doc
}
var _ ds.PropertyLoadSaver = (*docPipe)(nil)
func (p *docPipe) Load(c <-chan ds.Property) error {
return p.doc.Load(c)
}
func (p *docPipe) Save(c chan<- ds.Property) error {
defer close(c)
props, err := p.doc.Save(p.ctx)
if err != nil {
return err
}
for _, prop := range props {
c <- *prop
}
return nil
}