forked from MixinNetwork/ocean.one
-
Notifications
You must be signed in to change notification settings - Fork 0
/
property.go
54 lines (45 loc) · 1.22 KB
/
property.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package persistence
import (
"context"
"time"
"cloud.google.com/go/spanner"
"google.golang.org/api/iterator"
)
type Property struct {
Key string
Value string
UpdatedAt time.Time
}
func ReadProperty(ctx context.Context, key string) (string, error) {
it := Spanner(ctx).Single().Read(ctx, "properties", spanner.Key{key}, []string{"value"})
defer it.Stop()
row, err := it.Next()
if err == iterator.Done {
return "", nil
} else if err != nil {
return "", err
}
var value string
err = row.Column(0, &value)
return value, err
}
func WriteProperty(ctx context.Context, key, value string) error {
_, err := Spanner(ctx).Apply(ctx, []*spanner.Mutation{
spanner.InsertOrUpdate("properties", []string{"key", "value", "updated_at"}, []interface{}{key, value, time.Now()}),
})
return err
}
func ReadPropertyAsTime(ctx context.Context, key string) (time.Time, error) {
var offset time.Time
timestamp, err := ReadProperty(ctx, key)
if err != nil {
return offset, err
}
if timestamp != "" {
return time.Parse(time.RFC3339Nano, timestamp)
}
return offset, nil
}
func WriteTimeProperty(ctx context.Context, key string, value time.Time) error {
return WriteProperty(ctx, key, value.UTC().Format(time.RFC3339Nano))
}