-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathfeature_flag_repo.go
126 lines (104 loc) · 3.64 KB
/
feature_flag_repo.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package repository
import (
"context"
"fmt"
"github.com/harness/ff-proxy/v2/cache"
clientgen "github.com/harness/ff-proxy/v2/gen/client"
"github.com/harness/ff-proxy/v2/domain"
)
// FeatureFlagRepo is a repository that stores FeatureFlags
type FeatureFlagRepo struct {
cache cache.Cache
}
// NewFeatureFlagRepo creates a FeatureFlagRepo. It can optionally preload the repo with data
// from the passed config
func NewFeatureFlagRepo(c cache.Cache) FeatureFlagRepo {
return FeatureFlagRepo{cache: c}
}
// Get gets all the FeatureFlag for a given key
func (f FeatureFlagRepo) Get(ctx context.Context, envID string) ([]domain.FeatureFlag, error) {
var featureFlags []domain.FeatureFlag
key := domain.NewFeatureConfigsKey(envID)
err := f.cache.Get(ctx, string(key), &featureFlags)
if err != nil {
return []domain.FeatureFlag{}, err
}
return featureFlags, nil
}
// GetByIdentifier gets a FeatureFlag for a given key and identifier
func (f FeatureFlagRepo) GetByIdentifier(ctx context.Context, envID string, identifier string) (domain.FeatureFlag, error) {
featureFlag := domain.FeatureFlag{}
key := domain.NewFeatureConfigKey(envID, identifier)
if err := f.cache.Get(ctx, string(key), &featureFlag); err != nil {
return domain.FeatureFlag{}, err
}
// some sdks e.g. .NET don't cope well with being returned a null VariationToTargetMap so we send back an empty struct here for now
// to match ff-server behaviour
if featureFlag.VariationToTargetMap == nil {
emptyVariationMap := []clientgen.VariationMap{}
featureFlag.VariationToTargetMap = &emptyVariationMap
}
return featureFlag, nil
}
// Add stores FlagConfig in the cache
func (f FeatureFlagRepo) Add(ctx context.Context, config ...domain.FlagConfig) error {
errs := []error{}
for _, cfg := range config {
k := domain.NewFeatureConfigsKey(cfg.EnvironmentID)
if err := f.cache.Set(ctx, string(k), cfg.FeatureConfigs); err != nil {
errs = append(errs, addError{
key: string(k),
identifier: "feature-configs",
err: err,
})
}
for _, flag := range cfg.FeatureConfigs {
key := domain.NewFeatureConfigKey(cfg.EnvironmentID, flag.Feature)
if err := f.cache.Set(ctx, string(key), flag); err != nil {
errs = append(errs, addError{
key: string(key),
identifier: flag.Feature,
err: err,
})
}
}
}
if len(errs) > 0 {
return fmt.Errorf("failed to add flagConfig(s) to cache: %v", errs)
}
return nil
}
// GetFeatureConfigForEnvironment gets the feature config for environment from cache.
func (f FeatureFlagRepo) GetFeatureConfigForEnvironment(ctx context.Context, envID string) ([]domain.FeatureFlag, bool) {
var features []domain.FeatureFlag
key := domain.NewFeatureConfigsKey(envID)
if err := f.cache.Get(ctx, string(key), &features); err != nil {
return features, false
}
return features, true
}
// Remove removes the feature entry from the cache
func (f FeatureFlagRepo) Remove(ctx context.Context, identifier string) error {
return f.cache.Delete(ctx, identifier)
}
// RemoveAllFeaturesForEnvironment removes all feature entries for given environment id
func (f FeatureFlagRepo) RemoveAllFeaturesForEnvironment(ctx context.Context, id string) error {
// get all the feature for given key
flags, err := f.Get(ctx, id)
if err != nil {
return err
}
// remove featureConfigs entry
fcKey := domain.NewFeatureConfigsKey(id)
if err := f.cache.Delete(ctx, string(fcKey)); err != nil {
return err
}
// remove all individual feature entries for environment
for _, flag := range flags {
key := domain.NewFeatureConfigKey(id, flag.Feature)
if err := f.cache.Delete(ctx, string(key)); err != nil {
return err
}
}
return nil
}