-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransact.go
More file actions
387 lines (332 loc) · 10.3 KB
/
transact.go
File metadata and controls
387 lines (332 loc) · 10.3 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/*
* Copyright © 2022 Atomist, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package skill
import (
"bytes"
"context"
"fmt"
"net/http"
"os"
"reflect"
"strings"
"github.com/atomist-skills/go-skill/internal"
"github.com/google/uuid"
"olympos.io/encoding/edn"
)
// Entity models the required fields to transact an entity
type Entity struct {
EntityType edn.Keyword `edn:"schema/entity-type"`
Entity string `edn:"schema/entity,omitempty"`
}
// ManyRef models an entity reference of cardinality many
type ManyRef struct {
Add []string `edn:"add,omitempty"`
Set []string `edn:"set,omitempty"`
Retract []string `edn:"retract,omitempty"`
}
// Transaction collects entities
type Transaction interface {
Ordered() Transaction
AddEntities(entities ...interface{}) Transaction
EntityRefs(entityType string) []string
EntityRef(entityType string) string
Transact() error
}
type transaction struct {
entities []interface{}
ctx context.Context
ordered bool
transactor Transactor
}
type Transactor func(entities []interface{}, ordered bool) error
func NewTransaction(ctx context.Context, transactor Transactor) Transaction {
return newTransaction(ctx, transactor)
}
func NewStringTransactor(stringTransactionFunc func(string)) Transactor {
return func(entities []interface{}, ordered bool) error {
transactions, err := makeTransaction(entities, "")
if err != nil {
return err
}
flattenedEntities := transactions.Data
bs, _ := edn.MarshalPPrint(flattenedEntities, nil)
stringTransactionFunc(string(bs))
return nil
}
}
func NewHttpTransactor(teamId string, token string, orderingKey string, correlationId string, logger Logger) Transactor {
sender := createHttpMessageSender(teamId, token, correlationId, logger)
return func(entities []interface{}, ordered bool) error {
if ordered {
return sender.TransactOrdered(entities, orderingKey)
}
return sender.Transact(entities)
}
}
// Ordered makes this ordered
func (t *transaction) Ordered() Transaction {
t.ordered = true
return t
}
// AddEntities adds a new entity to this transaction
func (t *transaction) AddEntities(entities ...interface{}) Transaction {
for _, e := range entities {
t.entities = append(t.entities, makeEntity(e))
}
return t
}
// Transact triggers a transaction of the entities to the backend.
// The recorded entities are not discarded in this transaction for further reference
func (t *transaction) Transact() error {
return t.transactor(t.entities, t.ordered)
}
// MakeEntity creates a new Entity struct populated with entity-type and a unique entity identifier
func MakeEntity[E interface{}](value E, entityId ...string) E {
reflectValue := reflect.ValueOf(value)
var field reflect.StructField
if reflectValue.Kind() == reflect.Ptr {
field, _ = reflect.TypeOf(value).Elem().FieldByName("Entity")
} else {
field, _ = reflect.TypeOf(value).FieldByName("Entity")
}
entityType := field.Tag.Get("entity-type")
entity := Entity{
EntityType: edn.Keyword(entityType),
}
if len(entityId) == 0 {
parts := strings.Split(entityType, "/")
entity.Entity = fmt.Sprintf("$%s-%s", parts[len(parts)-1], uuid.New().String())
} else {
entity.Entity = entityId[0]
}
if reflectValue.Kind() == reflect.Ptr {
reflectValue.Elem().FieldByName("Entity").Set(reflect.ValueOf(entity))
} else {
reflect.ValueOf(&value).Elem().FieldByName("Entity").Set(reflect.ValueOf(entity))
}
return value
}
func (t *transaction) EntityRefs(entityType string) []string {
return EntityRefs(t.entities, entityType)
}
func (t *transaction) EntityRef(entityType string) string {
return EntityRef(t.entities, entityType)
}
// newTransaction creates a new Transaction to record entities
func newTransaction(ctx context.Context, transactor Transactor) Transaction {
return &transaction{
entities: make([]interface{}, 0),
ctx: ctx,
ordered: false,
transactor: transactor,
}
}
// EntityRefs find all entities by given entityType and returns their identity
func EntityRefs(entities []interface{}, entityType string) []string {
entityRefs := make([]string, 0)
for i := range entities {
entity := entities[i]
if entity != nil && reflect.ValueOf(entity).FieldByName("EntityType").String() == entityType {
value := reflect.ValueOf(entity).FieldByName("Entity").Interface().(Entity)
entityRefs = append([]string{value.Entity}, entityRefs...)
}
}
return entityRefs
}
// EntityRef finds one entity by given entityType and returns its identity
func EntityRef(entities []interface{}, entityType string) string {
if entityRefs := EntityRefs(entities, entityType); len(entityRefs) > 0 {
return entityRefs[0]
}
return ""
}
type Transact func(entities interface{}) error
type TransactOrdered func(entities interface{}, orderingKey string) error
type messageSender struct {
Transact Transact
TransactOrdered TransactOrdered
}
func createMessageSender(ctx context.Context, event EventIncoming, logger Logger) messageSender {
messageSender := messageSender{}
messageSender.TransactOrdered = func(entities interface{}, orderingKey string) error {
// Don't transact when evaluating policies locally
if os.Getenv("SCOUT_LOCAL_POLICY_EVALUATION") == "true" {
return nil
}
var entityArray []interface{}
rt := reflect.TypeOf(entities)
switch rt.Kind() {
case reflect.Array:
case reflect.Slice:
entityArray = entities.([]interface{})
default:
entityArray = []any{entities}
}
transactions, err := makeTransaction(entityArray, orderingKey)
bs, err := edn.MarshalPPrint(internal.TransactionEntityBody{
Transactions: []internal.TransactionEntity{*transactions}}, nil)
if err != nil {
return err
}
client := http.DefaultClient
logger.Debugf("Transacting entities: %s", string(bs))
httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, event.Urls.Transactions, bytes.NewBuffer(bs))
if err != nil {
return err
}
httpReq.Header.Set("Authorization", "Bearer "+event.Token)
httpReq.Header.Set("Content-Type", "application/edn")
resp, err := client.Do(httpReq)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != 202 {
Log.Warnf("Error transacting entities: %s", resp.Status)
}
return nil
}
messageSender.Transact = func(entities interface{}) error {
return messageSender.TransactOrdered(entities, "")
}
return messageSender
}
func makeTransaction(entities []interface{}, orderingKey string) (*internal.TransactionEntity, error) {
body, err := edn.MarshalPPrint(entities, nil)
if err != nil {
return nil, err
}
var e []map[edn.Keyword]edn.RawMessage
err = edn.NewDecoder(bytes.NewReader(body)).Decode(&e)
if err != nil {
return nil, err
}
transactions := internal.TransactionEntity{Data: flattenEntities(e)}
if orderingKey != "" {
transactions.OrderingKey = orderingKey
}
return &transactions, nil
}
func flattenEntities(entities []map[edn.Keyword]edn.RawMessage) []map[edn.Keyword]edn.RawMessage {
fEntities := make([]map[edn.Keyword]edn.RawMessage, 0)
for _, e := range entities {
fEntities = append(fEntities, flattenEntity(e)...)
}
// make entity list unique by schema/entity
uEntities := make(map[string]map[edn.Keyword]edn.RawMessage, 0)
for _, e := range fEntities {
entity := string(e["schema/entity"])
if _, ok := uEntities[entity]; !ok {
uEntities[entity] = e
}
}
// collect the values
fEntities = make([]map[edn.Keyword]edn.RawMessage, 0)
for _, v := range uEntities {
fEntities = append(fEntities, v)
}
return fEntities
}
func flattenEntity(entity map[edn.Keyword]edn.RawMessage) []map[edn.Keyword]edn.RawMessage {
entities := make([]map[edn.Keyword]edn.RawMessage, 0)
entities = append(entities, entity)
for k, v := range entity {
if !strings.HasPrefix(k.String(), ":schema/") {
// test single entity first
var n map[edn.Keyword]edn.RawMessage
err := edn.NewDecoder(bytes.NewReader(v)).Decode(&n)
if err == nil {
if e, ok := n["schema/entity"]; ok {
entity[k] = e
entities = append(entities, flattenEntity(n)...)
}
continue
}
// test array second
var a []map[edn.Keyword]edn.RawMessage
err = edn.NewDecoder(bytes.NewReader(v)).Decode(&a)
if err == nil {
refs := make([]string, len(a))
for i := range a {
refs[i] = string(a[i]["schema/entity"])
entities = append(entities, flattenEntity(a[i])...)
}
entity[k] = []byte(fmt.Sprintf("{:set [%s]}", strings.Join(refs, "")))
}
}
}
return entities
}
func makeEntity(x interface{}) interface{} {
// Starting value must be a pointer.
v := reflect.ValueOf(x)
if v.Kind() != reflect.Ptr {
v = reflect.ValueOf(&x)
}
setEntityValues(v, "")
return x
}
func setEntityValues(v reflect.Value, entityType string) {
switch v.Kind() {
case reflect.Ptr:
if v.IsZero() {
return
}
setEntityValues(v.Elem(), entityType)
case reflect.Interface:
if v.IsZero() {
return
}
iv := v.Elem()
switch iv.Kind() {
case reflect.Slice, reflect.Ptr:
setEntityValues(iv, entityType)
case reflect.Struct, reflect.Array:
// Copy required for modification.
copy := reflect.New(iv.Type()).Elem()
copy.Set(iv)
setEntityValues(copy, entityType)
v.Set(copy)
}
case reflect.Struct:
t := v.Type()
for i := 0; i < t.NumField(); i++ {
sf := t.Field(i)
fv := v.Field(i)
if sf.Name == "Entity" {
if entityType != "" {
if fv.String() == "" {
parts := strings.Split(entityType, "/")
fv.Set(reflect.ValueOf(fmt.Sprintf("$%s-%s", parts[len(parts)-1], uuid.New().String())))
}
} else {
entityType := sf.Tag.Get("entity-type")
setEntityValues(fv, entityType)
}
} else if sf.Name == "EntityType" {
if fv.Interface().(edn.Keyword) == "" {
fv.Set(reflect.ValueOf(edn.Keyword(entityType)))
}
} else {
setEntityValues(fv, entityType)
}
}
case reflect.Slice, reflect.Array:
for i := 0; i < v.Len(); i++ {
setEntityValues(v.Index(i), entityType)
}
}
}