forked from brutella/hc
/
container.go
123 lines (100 loc) · 2.6 KB
/
container.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
package accessory
import (
"crypto/md5"
"encoding/json"
"github.com/brutella/hc/log"
)
// Container manages a list of accessories.
type Container struct {
Accessories []*Accessory `json:"accessories"`
idCount int64
}
// NewContainer returns a container.
func NewContainer() *Container {
return &Container{
Accessories: make([]*Accessory, 0),
idCount: 1,
}
}
// AddAccessory adds an accessory to the container.
// This method ensures that the accessory ids are valid and unique withing the container.
func (m *Container) AddAccessory(a *Accessory) {
a.SetID(m.idCount)
m.idCount++
m.Accessories = append(m.Accessories, a)
}
// RemoveAccessory removes an accessory from the container.
func (m *Container) RemoveAccessory(a *Accessory) {
for i, accessory := range m.Accessories {
if accessory == a {
m.Accessories = append(m.Accessories[:i], m.Accessories[i+1:]...)
}
}
}
// Equal returns true when receiver has the same accessories as the argument.
func (m *Container) Equal(other interface{}) bool {
if container, ok := other.(*Container); ok == true {
if len(m.Accessories) != len(container.Accessories) {
return false
}
for i, a := range m.Accessories {
if a.Equal(container.Accessories[i]) == false {
return false
}
}
return true
}
return false
}
// AccessoryType returns the accessory type identifier for the accessories inside the container.
func (m *Container) AccessoryType() AccessoryType {
if as := m.Accessories; len(as) > 0 {
if len(as) > 1 {
return TypeBridge
}
return as[0].Type
}
return TypeOther
}
// ContentHash returns a hash of the content (ignoring the value field).
func (m *Container) ContentHash() []byte {
var b []byte
var err error
if b, err = json.Marshal(m); err != nil {
log.Info.Panic(err)
}
val := map[string]interface{}{}
if err := json.Unmarshal(b, &val); err != nil {
log.Info.Panic(err)
}
deleteFieldFromDict(&val, "value")
if b, err = json.Marshal(val); err != nil {
log.Info.Panic(err)
}
h := md5.New()
h.Write(b)
return h.Sum(nil)
}
func deleteFieldFromDict(val *map[string]interface{}, field string) {
for k, v := range *val {
if k == field {
delete(*val, k)
} else {
deleteFieldFromInterface(&v, field)
}
}
}
func deleteFieldFromArray(val *[]interface{}, field string) {
for _, v := range *val {
deleteFieldFromInterface(&v, field)
}
}
func deleteFieldFromInterface(val *interface{}, field string) {
v := *val
if dict, ok := v.(map[string]interface{}); ok == true {
deleteFieldFromDict(&dict, field)
}
if array, ok := v.([]interface{}); ok == true {
deleteFieldFromArray(&array, field)
}
}