Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/delphinus/go-entity-generator
Generator to yield all entities from Datastore
https://github.com/delphinus/go-entity-generator
Last synced: 7 days ago
JSON representation
Generator to yield all entities from Datastore
- Host: GitHub
- URL: https://github.com/delphinus/go-entity-generator
- Owner: delphinus
- Created: 2017-05-23T04:37:10.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2017-06-01T11:02:28.000Z (over 7 years ago)
- Last Synced: 2024-06-20T11:12:51.456Z (5 months ago)
- Language: Go
- Size: 21.5 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# go-entity-generator
[![CircleCI](https://circleci.com/gh/delphinus/go-entity-generator.svg?style=svg)](https://circleci.com/gh/delphinus/go-entity-generator)
[![Coverage Status](https://coveralls.io/repos/github/delphinus/go-entity-generator/badge.svg?branch=master)](https://coveralls.io/github/delphinus/go-entity-generator?branch=master)
[![GoDoc](https://godoc.org/github.com/delphinus/go-entity-generator?status.svg)](https://godoc.org/github.com/delphinus/go-entity-generator)Generator to yield all entities from Datastore.
docs in [godoc](https://godoc.org/github.com/delphinus/go-entity-generator).
## example
```go
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
}
}
}
}
```