This repository has been archived by the owner on Jul 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtoggles.go
85 lines (70 loc) · 1.82 KB
/
toggles.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package gateways
import (
"context"
"strings"
"sync"
"github.com/int128/gradleupdate/domain/config"
"github.com/int128/gradleupdate/gateways/interfaces"
"github.com/pkg/errors"
"google.golang.org/appengine/datastore"
)
/*
NewToggles returns an implementation of gateways.Toggle.
You can create the following entity to enable the feature toggles,
* Kind = Toggle
* Key (string) = DEFAULT
with the following properties:
* BatchSendUpdatesOwners (string) = comma separated names (everyone if blank)
*/
func NewToggles() gateways.Toggles {
return &togglesCache{
Base: &togglesData{},
}
}
type togglesCache struct {
Base gateways.Toggles
l sync.Mutex
v *config.Toggles
}
func (r *togglesCache) Get(ctx context.Context) (*config.Toggles, error) {
r.l.Lock()
defer r.l.Unlock()
if r.v != nil {
return r.v, nil
}
v, err := r.Base.Get(ctx)
if err != nil {
return nil, errors.Wrapf(err, "error while getting toggles")
}
r.v = v
return r.v, nil
}
type togglesData struct{}
func (r *togglesData) Get(ctx context.Context) (*config.Toggles, error) {
var e togglesEntity
k := togglesKey(ctx, "DEFAULT")
if err := datastore.Get(ctx, k, &e); err != nil {
if err == datastore.ErrNoSuchEntity {
return &config.Toggles{}, nil
}
return nil, errors.Wrapf(err, "error while getting the entity")
}
if e.BatchSendUpdatesOwners == "" {
return &config.Toggles{}, nil
}
return &config.Toggles{
BatchSendUpdatesOwners: e.BatchSendUpdatesOwnersAsArray(),
}, nil
}
func togglesKey(ctx context.Context, name string) *datastore.Key {
return datastore.NewKey(ctx, "Toggles", name, 0, nil)
}
type togglesEntity struct {
BatchSendUpdatesOwners string
}
func (e *togglesEntity) BatchSendUpdatesOwnersAsArray() []string {
if e.BatchSendUpdatesOwners == "" {
return nil
}
return strings.Split(e.BatchSendUpdatesOwners, ",")
}