Skip to content

delphinus/go-entity-generator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

51 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go-entity-generator

CircleCI Coverage Status GoDoc

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
      }
    }
  }
}

About

Generator to yield all entities from Datastore

Resources

Stars

Watchers

Forks

Packages

No packages published