/
utils.go
85 lines (68 loc) · 4.2 KB
/
utils.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 operators
import (
"context"
"runtime/debug"
"unsafe"
"github.com/kyma-incubator/compass/components/director/internal/domain/statusreport"
"github.com/kyma-incubator/compass/components/director/pkg/graphql"
"github.com/kyma-incubator/compass/components/director/internal/model"
"github.com/kyma-incubator/compass/components/director/pkg/log"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
// reqBodyNameRegex is a regex defined by the destination creator API specifying what destination names are allowed
var reqBodyNameRegex = "[a-zA-Z0-9_-]{1,64}"
func isConfigEmpty(config string) bool {
return config == "" || config == "{}" || config == "\"\"" || config == "null"
}
func isFormationAssignmentConfigEmpty(assignment *model.FormationAssignment) bool {
return assignment != nil && isConfigEmpty(string(assignment.Value))
}
func isNotificationStatusReportConfigEmpty(notificationStatusReport *statusreport.NotificationStatusReport) bool {
return notificationStatusReport != nil && isConfigEmpty(string(notificationStatusReport.Configuration))
}
// RetrieveFormationAssignmentPointer converts the provided memory address in form of an integer back to the model.FormationAssignment pointer structure
// It's important the provided memory address to stores information about model.FormationAssignment entity, otherwise the result could be very abnormal
func RetrieveFormationAssignmentPointer(ctx context.Context, joinPointDetailsAssignmentMemoryAddress uintptr) (*model.FormationAssignment, error) {
if joinPointDetailsAssignmentMemoryAddress == 0 { // the default value of uintptr is 0
return nil, errors.New("The join point details' assignment memory address cannot be 0")
}
defer func() {
if err := recover(); err != nil {
log.C(ctx).WithField(logrus.ErrorKey, err).Panicf("A panic occurred while converting join point details' assignment address: %d to type: %T", joinPointDetailsAssignmentMemoryAddress, &model.FormationAssignment{})
debug.PrintStack()
}
}()
joinPointAssignmentPointer := (*model.FormationAssignment)(unsafe.Pointer(joinPointDetailsAssignmentMemoryAddress))
return joinPointAssignmentPointer, nil
}
// RetrieveWebhookPointerFromMemoryAddress converts the provided uninterpreted memory address in form of an integer back to the model.Webhook pointer structure
// It's important the provided memory address to stores information about model.Webhook entity, otherwise the result could be very abnormal
func RetrieveWebhookPointerFromMemoryAddress(ctx context.Context, webhookMemoryAddress uintptr) (*graphql.Webhook, error) {
if webhookMemoryAddress == 0 { // the default value of uintptr is 0
return nil, errors.New("The webhook memory address cannot be 0")
}
defer func() {
if err := recover(); err != nil {
log.C(ctx).WithField(logrus.ErrorKey, err).Panicf("A panic occurred while converting join point details' assignment address: %d to type: %T", webhookMemoryAddress, &model.Webhook{})
debug.PrintStack()
}
}()
joinPointWebhookPointer := (*graphql.Webhook)(unsafe.Pointer(webhookMemoryAddress))
return joinPointWebhookPointer, nil
}
// RetrieveNotificationStatusReportPointer converts the provided memory address in form of an integer back to the statusreport.NotificationStatusReport pointer structure
// It's important the provided memory address to stores information about model.FormationAssignment entity, otherwise the result could be very abnormal
func RetrieveNotificationStatusReportPointer(ctx context.Context, notificationStatusReportMemoryAddress uintptr) (*statusreport.NotificationStatusReport, error) {
if notificationStatusReportMemoryAddress == 0 { // the default value of uintptr is 0
return nil, errors.New("The join point details' notification status report memory address cannot be 0")
}
defer func() {
if err := recover(); err != nil {
log.C(ctx).WithField(logrus.ErrorKey, err).Panicf("A panic occurred while converting join point details' notification status report memory address: %d to type: %T", notificationStatusReportMemoryAddress, &model.FormationAssignment{})
debug.PrintStack()
}
}()
notificationStatusReport := (*statusreport.NotificationStatusReport)(unsafe.Pointer(notificationStatusReportMemoryAddress))
return notificationStatusReport, nil
}