-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessage.go
168 lines (134 loc) · 3.62 KB
/
message.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
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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package main
import (
"encoding/json"
"errors"
"log"
"strings"
graph "gopkg.in/r3labs/graph.v2"
)
const (
// COMPONENTYPE : component type
COMPONENTYPE = "component"
// SERVICETYPE : service type
SERVICETYPE = "service"
)
// Message : Struct representing a received message, with
// its endpoint as "subject" and the content as "data"
type Message struct {
subject string
data map[string]interface{}
}
// NewMessage : Message constructor
func NewMessage(subject string, data []byte) (*Message, error) {
var m map[string]interface{}
if subject == "" {
return nil, errors.New("Error : invalid message subject")
}
err := json.Unmarshal(data, &m)
if err != nil {
return nil, err
}
return &Message{subject: subject, data: m}, nil
}
// NewFakeComponent : returns an empty component that can be used as start or end point
func NewFakeComponent(id string) *graph.GenericComponent {
c := make(graph.GenericComponent)
c["_component_id"] = id
c["_state"] = STATUSCOMPLETED
c["_state"] = "none"
return &c
}
// getGraph : will return the graph attached to the current message
// or nil in case there is some problem
func (m *Message) getGraph() *graph.Graph {
if m.getType() == SERVICETYPE {
return m.getGraphFromGraph()
}
return m.getGraphFromComponent()
}
// getComponent : will get the graph current component
func (m *Message) getComponent() *graph.GenericComponent {
var component *graph.GenericComponent
switch m.getType() {
case SERVICETYPE:
component = NewFakeComponent("start")
case COMPONENTYPE:
component = graph.MapGenericComponent(m.data)
}
return component
}
func (m *Message) getGraphFromGraph() *graph.Graph {
g := graph.New()
err := g.Load(m.data)
if err != nil {
log.Println("Error: could not load mapping!" + err.Error())
return nil
}
g.Action = m.subject
err = setMapping(g.ID, g)
if err != nil {
log.Println("Error: could not store mapping!" + err.Error())
return nil
}
return g
}
func (m *Message) getGraphFromComponent() *graph.Graph {
g := graph.New()
key := m.getServiceKey()
id, ok := m.data[key].(string)
if ok != true {
log.Println("Error: could not get graph from message")
return nil
}
mapping, err := getMapping(id)
if err != nil {
log.Println("Error: could not get mapping: " + id)
log.Println(err.Error())
return nil
}
err = g.Load(mapping)
if err != nil {
log.Println("Error: could not load mapping!" + err.Error())
return nil
}
return g
}
// getServiceKey : get the field key to identify the service
func (m *Message) getServiceKey() string {
if m.getType() == SERVICETYPE {
return "id"
}
return "service"
}
// getType : a message cab have a type 'service' or 'component'. String
// 'unsupported' will be returned as default value
func (m *Message) getType() string {
switch m.subject {
case "build.create", "build.delete", "build.import", "build.patch", "build.sync":
return SERVICETYPE
}
if m.data["_component_id"] != nil && m.isCompleted() {
return COMPONENTYPE
}
return "unsupported"
}
// isSupported : check to see if the message is supported or not
func (m *Message) isSupported() bool {
if m.getType() == "unsupported" {
return false
}
return true
}
// isCompleted : check if the message is a final message *.done or *.error
func (m *Message) isCompleted() bool {
parts := strings.Split(m.subject, ".")
if len(parts) > 1 {
if parts[len(parts)-1] == "done" || parts[len(parts)-1] == "error" {
return true
}
}
return false
}