-
Notifications
You must be signed in to change notification settings - Fork 2
/
scenarios.go
143 lines (123 loc) · 4.55 KB
/
scenarios.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package scenarios
import (
"context"
"fmt"
"io"
"google.golang.org/protobuf/proto"
"github.com/curious-kitten/scratch-post/internal/decoder"
metadatav1 "github.com/curious-kitten/scratch-post/pkg/api/v1/metadata"
scenariov1 "github.com/curious-kitten/scratch-post/pkg/api/v1/scenario"
)
//go:generate mockgen -source ./scenarios.go -destination mocks/scenarios.go
type projectRetriever func(ctx context.Context, id string) (interface{}, error)
// MetaHandler handles metadata information
type MetaHandler interface {
NewMeta(author string, objType string) (*metadatav1.Identity, error)
UpdateMeta(author string, identity *metadatav1.Identity)
}
// Adder is used to add items to the store
type Adder interface {
AddOne(ctx context.Context, item interface{}) error
}
// Getter is used to retrieve items from the store
type Getter interface {
Get(ctx context.Context, id string, item interface{}) error
GetAll(ctx context.Context, items interface{}, filterMap map[string][]string, sortBy string, reverse bool, count int, previousLastValue string) error
}
// Deleter deletes an entry from the collection
type Deleter interface {
Delete(ctx context.Context, id string) error
}
// Updater is used to replace information into the Data Base
type Updater interface {
Update(ctx context.Context, id string, item interface{}) error
}
// ReaderUpdater is used to read and update objects in the Data Base
type ReaderUpdater interface {
Getter
Updater
}
// New returns a function used to create a scenario
func New(meta MetaHandler, collection Adder, getProject projectRetriever) func(ctx context.Context, author string, data io.Reader) (interface{}, error) {
return func(ctx context.Context, author string, data io.Reader) (interface{}, error) {
scenario := &scenariov1.Scenario{}
if err := decoder.Decode(scenario, data); err != nil {
return nil, err
}
if _, err := getProject(ctx, scenario.ProjectId); err != nil {
return nil, err
}
identity, err := meta.NewMeta(author, "scenario")
if err != nil {
return nil, err
}
scenario.Identity = identity
if err := collection.AddOne(ctx, scenario); err != nil {
return nil, err
}
return scenario, nil
}
}
// List returns a function used to return the scenarios
func List(collection Getter) func(ctx context.Context, filter map[string][]string, sortBy string, reverse bool, count int, previousLastValue string) ([]interface{}, error) {
return func(ctx context.Context, filter map[string][]string, sortBy string, reverse bool, count int, previousLastValue string) ([]interface{}, error) {
scenarioList := []scenariov1.Scenario{}
err := collection.GetAll(ctx, &scenarioList, filter, sortBy, reverse, count, previousLastValue)
if err != nil {
return nil, err
}
items := make([]interface{}, len(scenarioList))
for i := range scenarioList {
items[i] = proto.Clone(&scenarioList[i]).(*scenariov1.Scenario)
}
return items, nil
}
}
// Get returns a function to retrieve a scenario based on the passed ID
func Get(collectiom Getter) func(ctx context.Context, id string) (interface{}, error) {
return func(ctx context.Context, id string) (interface{}, error) {
scenario := &scenariov1.Scenario{}
if err := collectiom.Get(ctx, id, scenario); err != nil {
return nil, err
}
return scenario, nil
}
}
// Delete returns a function to delete a scenario based on the passed ID
func Delete(collection Deleter) func(ctx context.Context, id string) error {
return func(ctx context.Context, id string) error {
if err := collection.Delete(ctx, id); err != nil {
return err
}
return nil
}
}
// Update is used to replace a scenario with the provided scenario
func Update(meta MetaHandler, collection ReaderUpdater, getProject projectRetriever) func(ctx context.Context, user string, id string, data io.Reader) (interface{}, error) {
return func(ctx context.Context, user string, id string, data io.Reader) (interface{}, error) {
scenario := &scenariov1.Scenario{}
if err := decoder.Decode(scenario, data); err != nil {
return nil, err
}
if _, err := getProject(ctx, scenario.ProjectId); err != nil {
return nil, err
}
foundScenario, err := Get(collection)(ctx, id)
if err != nil {
return nil, err
}
var s *scenariov1.Scenario
var ok bool
if s, ok = foundScenario.(*scenariov1.Scenario); !ok {
return nil, fmt.Errorf("invalid data structure in DB")
}
scenario.Identity = s.Identity
meta.UpdateMeta(user, scenario.Identity)
if err := collection.Update(ctx, id, scenario); err != nil {
return nil, err
}
return scenario, nil
}
}
// func mangeFilters(filters map[string][]string) {
// }