Permalink
Browse files

Added `SetNoIndexProperties` option for excluding fields. (#3)

* Implemented noindex option for handler and entities
  • Loading branch information...
ajcrowe committed Dec 29, 2016
1 parent 24a76fe commit 87f8d7dc2ae6ddf0b1704f5e8d79e06c1b8f6c3f
Showing with 50 additions and 27 deletions.
  1. +0 −1 .travis.yml
  2. +9 −3 README.md
  3. +41 −23 datastore.go
View
@@ -1,6 +1,5 @@
language: go
go:
- 1.6
- 1.7
- master
script:
View
@@ -29,9 +29,15 @@ Then use this to create a new `Handler` for your resource binds
namespace := "default"
entity := "users"
// bind the users resource with the datastore handler
index.Bind("users", user, datastore.NewHandler(client, namespace, entity), resource.Conf{
AllowedModes: resource.ReadWrite,
})
index.Bind("users", user, datastore.NewHandler(client, namespace, entity), resource.DefaultConf)
```
You can also set a number of Datastore properties which you would like to exclude from being indexed with `SetNoIndexProperties` on your `handler` struct.
```go
// create a handler for the resource.
index.Bind("users", user, datastore.NewHandler(client, namespace, entity).SetNoIndexProperties([]string{"prop1", "prop2"}), resource.DefaultConf)
```
## Supported filter operators
View
@@ -18,9 +18,14 @@ func NewClient(ctx context.Context, projectID string, opts ...option.ClientOptio
// Handler handles resource storage in Google Datastore.
type Handler struct {
client *datastore.Client
entity string
// datastore.Client struct for executing our queries.
client *datastore.Client
// Kind of the entity this handler will create.
entity string
// Namespace in which these entities will be.
namespace string
// Properties which should not be indexed.
noIndexProps map[string]bool
}
// NewHandler creates a new Google Datastore handler
@@ -34,10 +39,11 @@ func NewHandler(client *datastore.Client, namespace, entity string) *Handler {
// Entity Is a representation of a Google Datastore entity
type Entity struct {
ID string
ETag string
Updated time.Time
Payload map[string]interface{}
ID string
ETag string
Updated time.Time
Payload map[string]interface{}
NoIndexProps map[string]bool
}
// Load implements the PropertyLoadSaver interface to process our dynamic payload data
@@ -82,39 +88,51 @@ func (e *Entity) Save() ([]datastore.Property, error) {
// Range over the payload and create the datastore.Properties
for k, v := range e.Payload {
prop := datastore.Property{
Name: k,
Value: v,
Name: k,
Value: v,
NoIndex: e.NoIndexProps[k],
}
ps = append(ps, prop)
}
return ps, nil
}
// newItem converts datastore entity into a resource.Item
func newItem(e *Entity) *resource.Item {
e.Payload["id"] = e.ID
return &resource.Item{
ID: e.ID,
ETag: e.ETag,
Updated: e.Updated,
Payload: e.Payload,
}
}
// newEntity converts a resource.Item into a Google datastore entity
func newEntity(i *resource.Item) *Entity {
func (d *Handler) newEntity(i *resource.Item) *Entity {
p := make(map[string]interface{}, len(i.Payload))
for k, v := range i.Payload {
if k != "id" {
p[k] = v
}
}
return &Entity{
ID: i.ID.(string),
ETag: i.ETag,
Updated: i.Updated,
Payload: p,
ID: i.ID.(string),
ETag: i.ETag,
Updated: i.Updated,
Payload: p,
NoIndexProps: d.noIndexProps,
}
}
// newItem converts datastore entity into a resource.Item
func newItem(e *Entity) *resource.Item {
e.Payload["id"] = e.ID
return &resource.Item{
ID: e.ID,
ETag: e.ETag,
Updated: e.Updated,
Payload: e.Payload,
// SetNoIndexProps sets the handlers properties which should have noindex set.
func (d *Handler) SetNoIndexProperties(props []string) *Handler {
p := make(map[string]bool, len(props))
for _, v := range props {
p[v] = true
}
d.noIndexProps = p
return d
}
// Insert inserts new entities
@@ -124,7 +142,7 @@ func (d *Handler) Insert(ctx context.Context, items []*resource.Item) error {
for i, item := range items {
mKeys[i] = datastore.NameKey(d.entity, item.ID.(string), nil)
mEntities[i] = newEntity(item)
mEntities[i] = d.newEntity(item)
}
_, err := d.client.PutMulti(ctx, mKeys, mEntities)
return err
@@ -134,7 +152,7 @@ func (d *Handler) Insert(ctx context.Context, items []*resource.Item) error {
func (d *Handler) Update(ctx context.Context, item *resource.Item, original *resource.Item) error {
var err error
entity := newEntity(item)
entity := d.newEntity(item)
// Run a transaction to update the Entity if the Entity exist and the ETags match
tx := func(tx *datastore.Transaction) error {
// Create a key for our current Entity

0 comments on commit 87f8d7d

Please sign in to comment.