/
eventactivation_converter.go
131 lines (105 loc) · 3.01 KB
/
eventactivation_converter.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
package application
import (
"strings"
"github.com/kyma-project/kyma/components/application-broker/pkg/apis/applicationconnector/v1alpha1"
"github.com/kyma-project/kyma/components/console-backend-service/internal/domain/rafter/spec"
"github.com/kyma-project/kyma/components/console-backend-service/internal/gqlschema"
)
type eventActivationConverter struct{}
func (c *eventActivationConverter) ToGQL(in *v1alpha1.EventActivation) *gqlschema.EventActivation {
if in == nil {
return nil
}
return &gqlschema.EventActivation{
Name: in.Name,
DisplayName: in.Spec.DisplayName,
SourceID: in.Spec.SourceID,
}
}
func (c *eventActivationConverter) ToGQLs(in []*v1alpha1.EventActivation) []gqlschema.EventActivation {
var result []gqlschema.EventActivation
for _, item := range in {
converted := c.ToGQL(item)
if converted != nil {
result = append(result, *converted)
}
}
return result
}
func (c *eventActivationConverter) ToGQLEvents(in *spec.AsyncAPISpec) []gqlschema.EventActivationEvent {
if in == nil {
return []gqlschema.EventActivationEvent{}
}
var events []gqlschema.EventActivationEvent
for k, channel := range in.Data.Channels {
if !c.isSubscribeEvent(channel) {
continue
}
eventType, version := c.getEventVersionedType(k)
events = append(events, gqlschema.EventActivationEvent{
EventType: eventType,
Version: version,
Description: c.getSummary(channel),
Schema: c.getPayload(channel),
})
}
return events
}
func (c *eventActivationConverter) getEventVersionedType(in string) (string, string) {
versionedType := strings.Replace(in, "/", ".", -1)
lastDotIndex := strings.LastIndex(versionedType, ".")
if lastDotIndex < 0 {
return versionedType, ""
}
eventType := versionedType[:lastDotIndex]
version := versionedType[(lastDotIndex + 1):]
return eventType, version
}
func (c *eventActivationConverter) isSubscribeEvent(in interface{}) bool {
_, exists := c.convertToMap(in)["subscribe"]
return exists
}
func (c *eventActivationConverter) getSummary(in interface{}) string {
message := c.getMessage(in)
if message == nil {
return ""
}
summary, exists := message["summary"]
if !exists {
return ""
}
result, ok := summary.(string)
if !ok {
return ""
}
return result
}
func (c *eventActivationConverter) getPayload(in interface{}) map[string]interface{} {
message := c.getMessage(in)
if message == nil {
return nil
}
payload, exists := message["payload"]
if !exists {
return nil
}
return c.convertToMap(payload)
}
func (c *eventActivationConverter) getMessage(in interface{}) map[string]interface{} {
subscribe, exists := c.convertToMap(in)["subscribe"]
if !exists {
return map[string]interface{}{}
}
message, exists := c.convertToMap(subscribe)["message"]
if !exists {
return map[string]interface{}{}
}
return c.convertToMap(message)
}
func (c *eventActivationConverter) convertToMap(in interface{}) map[string]interface{} {
result, ok := in.(map[string]interface{})
if !ok {
return map[string]interface{}{}
}
return result
}