/
state.go
68 lines (57 loc) · 1.71 KB
/
state.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 building
import (
"github.com/golang/protobuf/proto"
"github.com/rmrobinson/nerves/services/domotics/bridge"
)
// State contains the currently active view of the state of the linked service.
// It (or parts of it) may be persisted to ensure recovery of the service across reboots.
// It may be replicated to other services, however it will always represent the currently active state of a single service.
type State struct {
buildings map[string]*Building
floors map[string]*Floor
rooms map[string]*Room
bridges map[string]*bridge.Bridge
}
// Dup performs a deep copy on our state
func (s *State) Dup() *State {
ns := &State{
buildings: map[string]*Building{},
floors: map[string]*Floor{},
rooms: map[string]*Room{},
bridges: map[string]*bridge.Bridge{},
}
// Duplicate the rooms
for _, r := range s.rooms {
nr := proto.Clone(r).(*Room)
ns.rooms[r.Id] = nr
}
// Duplicate the bridges
for _, b := range s.bridges {
nb := proto.Clone(b).(*bridge.Bridge)
ns.bridges[b.Id] = nb
}
// Duplicate the floors.
for _, f := range s.floors {
nf := proto.Clone(f).(*Floor)
// We ensure the Room array for the floor points to the new fields.
nf.Rooms = []*Room{}
for _, r := range f.Rooms {
nf.Rooms = append(nf.Rooms, ns.rooms[r.Id])
}
ns.floors[f.Id] = nf
}
for _, b := range s.buildings {
nb := proto.Clone(b).(*Building)
// We ensure the Floor array for the building points to the new fields.
nb.Floors = []*Floor{}
for _, f := range b.Floors {
nb.Floors = append(nb.Floors, ns.floors[f.Id])
}
nb.Bridges = []*bridge.Bridge{}
for _, br := range b.Bridges {
nb.Bridges = append(nb.Bridges, ns.bridges[br.Id])
}
ns.buildings[b.Id] = nb
}
return ns
}