# README
go-entity-generator
Generator to yield all entities from Datastore.
docs in godoc.
example
type SomeItem struct {
ID int64 `datastore:"-" goon:"id"`
Name string `datastore:",noindex"`
}
func appender(ctx context.Context, entities []interface{}, i int, k *datastore.Key, parentKey *datastore.Key) []interface{} {
if k.IntID() == 0 {
log.Warningf(ctx, "SomeItem{} needs int64 key. But items[%d] has a string key: %v", i, k.StringID())
return entities
}
return append(entities, &SomeItem{ID: k.IntID()})
}
func someFunc(w http.ResponseWriter, r *http.Request) {
ctx := appengine.NewContext(r)
ctx, cancel := context.WithCancel(ctx)
defer cancel() // you should cancel before finishing.
ch := generator.New(ctx, &generator.Options{
Appender: appender,
Query: datastore.NewQuery("SomeItem"),
})
for unit := range ch {
if unit.Err != nil {
panic(err)
}
for _, e := range unit.Entities {
if s, ok := e.(*SomeItem); ok {
// some nice handling
}
}
}
}
# Functions
New returns a channel that does as a generator to yield a chunk of entities and an error if exists.
# Type aliases
Appender is needed to create entity for real.