-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransformer.go
39 lines (33 loc) · 951 Bytes
/
transformer.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
package transformer
import "cloud.google.com/go/datastore"
// Transformer is the interface that wraps methos Transform
//
// Transform performs transformations on the Entity
type Transformer interface {
Transform(e *Entity) error
}
// TransformerFunc type is an adapter to allow the use of functions
// as Transformer
type TransformerFunc func(e *Entity) error
func (f TransformerFunc) Transform(e *Entity) error {
return f(e)
}
// RemoveField returns a TransformerFunc that remove field of the Entity
func RemoveField(field string) TransformerFunc {
return func(e *Entity) error {
delete(e.Properties, field)
return nil
}
}
// SetField returns a TransformerFunc that sets a field to the Entity
func SetField(field string, value interface{}, index bool) TransformerFunc {
return func(e *Entity) error {
f := datastore.Property{
Name: field,
Value: value,
NoIndex: !index,
}
e.Properties[field] = f
return nil
}
}