/
incident.go
68 lines (57 loc) · 2.04 KB
/
incident.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
package main
import "strconv"
import "strings"
// Incident defines the basic item for managing and tracking issues.
type Incident struct {
Type string `json:"type"` // The type of incident.
Id int64 `json:"id"` // The unique identifier for the incident.
Description string `json:"description"` // The description of the incident.
Reporter string `json:"reporter"` // The reporter of the incident.
State string `json:"state"` // The current state of the incident.
Attributes map[string]string `json:"attributes"` // The attributes associated with the incident.
}
// IncidentUpdate defines a set of updates to apply to an underyling incident.
type IncidentUpdate struct {
State string `json:"state"` // The new state for the incident.
Description string `json:"description"` // The new description of the incident.
Reporter string `json:"reporter"` // The new reporter of the incident.
Attributes map[string]string `json:"attributes"` // The new attributes to associate with the incident.
}
func updateIncident(original *Incident, updated IncidentUpdate) bool {
changed := false
if len(updated.State) > 0 {
original.State = updated.State
changed = true
}
if len(updated.Reporter) > 0 {
original.Reporter = updated.Reporter
changed = true
}
if len(updated.Description) > 0 {
original.Description = updated.Description
changed = true
}
if updated.Attributes != nil {
original.Attributes = updated.Attributes
changed = true
}
return changed
}
func getIncidentPropertyValue(key string, incident Incident) string {
if strings.EqualFold(key, "id") {
return strconv.FormatInt(incident.Id, 10)
}
if strings.EqualFold(key, "reporter") {
return incident.Reporter
}
if strings.EqualFold(key, "description") {
return incident.Description
}
if strings.EqualFold(key, "state") {
return incident.State
}
if val, ok := incident.Attributes[key]; ok {
return val
}
return ""
}