-
Notifications
You must be signed in to change notification settings - Fork 0
/
repository.go
99 lines (87 loc) · 2.26 KB
/
repository.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
package flags
import (
"context"
"fmt"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
)
var ctx = context.Background()
type FlagRepository interface {
Get(key string) (bool, error)
Exists(name string) (bool, error)
Set(key string, value any) error
List() ([]Flag, error)
Delete(name string) error
}
type FlagMongoRepository struct {
collection *mongo.Collection
}
func NewFlagMongoRepository(host string, port uint) (FlagRepository, error) {
hostStr := fmt.Sprintf("mongodb://%v:%v", host, port)
client, err := mongo.Connect(ctx, options.Client().ApplyURI(hostStr))
if err != nil {
return nil, err
}
collection := client.Database("toggles").Collection("flags")
return FlagMongoRepository{collection}, nil
}
func (repository FlagMongoRepository) List() ([]Flag, error) {
cur, err := repository.collection.Find(ctx, bson.D{})
if err != nil {
return nil, err
}
defer cur.Close(ctx)
result := make([]Flag, 0)
for cur.Next(ctx) {
var flag Flag
err := cur.Decode(&flag)
if err != nil {
return nil, err
}
result = append(result, flag)
}
if err := cur.Err(); err != nil {
return nil, err
}
return result, nil
}
func (repository FlagMongoRepository) Get(key string) (bool, error) {
x := repository.collection.FindOne(ctx, bson.D{{Key: "name", Value: key}})
var f Flag
err := x.Decode(&f)
if err == mongo.ErrNoDocuments {
return false, ErrFlagNotFound
} else if err != nil {
return false, err
}
return f.Value, nil
}
func (repository FlagMongoRepository) Set(key string, value any) error {
filter := bson.D{{"name", key}}
update := bson.D{{"$set", bson.D{{"value", value}}}}
opts := options.Update().SetUpsert(true)
_, err := repository.collection.UpdateOne(context.TODO(), filter, update, opts)
if err != nil {
return err
} else {
return nil
}
}
func (repository FlagMongoRepository) Exists(name string) (bool, error) {
_, err := repository.Get(name)
if err == nil {
return true, nil
} else if err == ErrFlagNotFound {
return false, nil
} else {
return false, err
}
}
func (repository FlagMongoRepository) Delete(name string) error {
_, err := repository.collection.DeleteOne(ctx, bson.D{{Key: "name", Value: name}})
if err != nil {
return err
}
return nil
}