/
store.go
56 lines (44 loc) · 1.13 KB
/
store.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
55
56
package main
import (
"context"
"time"
"cloud.google.com/go/firestore"
)
type Store struct {
c *firestore.Client
}
func NewStore(c *firestore.Client) *Store {
return &Store{c: c}
}
const (
MapEntryCollection = "map_entry"
)
type Digest struct {
Code int64 `firestore:"0"`
Digest []byte `firestore:"1"`
}
type MapEntry struct {
PublicKey []byte `firestore:"0"`
Label string `firestore:"1"`
Target Digest `firestore:"2"`
EntrySignature []byte `firestore:"3"`
CreationTime time.Time `firestore:"4"`
ClientIPAddress string `firestore:"5"`
RequestBytes []byte `firestore:"6"`
}
func (s *Store) GetMapEntry(ctx context.Context, publicKey []byte, label string) (*MapEntry, error) {
docs := s.c.Collection(MapEntryCollection).Query.Where("0", "==", publicKey).Where("1", "==", label).Documents(ctx)
doc, err := docs.Next()
if err != nil { // Not found.
return nil, nil
}
e := MapEntry{}
if err := doc.DataTo(&e); err != nil {
return nil, err
}
return &e, nil
}
func (s *Store) SetMapEntry(ctx context.Context, e *MapEntry) error {
_, _, err := s.c.Collection(MapEntryCollection).Add(ctx, e)
return err
}