Skip to content

Commit 495ba14

Browse files
gkristicPreetam Jinka
authored and
Preetam Jinka
committed
Replace array with map at /procs/ response
Closes #24.
1 parent 57f64df commit 495ba14

File tree

3 files changed

+11
-31
lines changed

3 files changed

+11
-31
lines changed

http.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,9 @@ func (pl *Proclist) getProcs() []ProcDetail {
2222

2323
for id, p := range pl.procs {
2424
p.mu.RLock()
25-
attrs := make([]AttrDetail, 0, len(p.attrs))
25+
attrs := make(map[string]interface{})
2626
for name, value := range p.attrs {
27-
attrs = append(attrs, AttrDetail{
28-
Name: name,
29-
Value: value,
30-
})
27+
attrs[name] = value
3128
}
3229
firstHEntry := p.history.Front().Value.(*historyEntry)
3330
lastHEntry := p.history.Back().Value.(*historyEntry)

pm_test.go

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,6 @@ func attrMapEquals(m1, m2 map[string]interface{}) bool {
2424
return true
2525
}
2626

27-
func attrMap(t *testing.T, p *ProcDetail) map[string]interface{} {
28-
attrs := make(map[string]interface{})
29-
for _, attr := range p.Attrs {
30-
if _, present := attrs[attr.Name]; present {
31-
t.Error("attribute doubly defined:", attr.Name)
32-
}
33-
attrs[attr.Name] = attr.Value
34-
}
35-
return attrs
36-
}
37-
3827
func procMapEquals(t *testing.T, m1, m2 map[string]ProcDetail) bool {
3928
if len(m1) != len(m2) {
4029
return false
@@ -44,7 +33,7 @@ func procMapEquals(t *testing.T, m1, m2 map[string]ProcDetail) bool {
4433
return false
4534
} else if v1.Id != v2.Id || v1.Status != v2.Status || v1.Cancelling != v2.Cancelling {
4635
return false
47-
} else if !attrMapEquals(attrMap(t, &v1), attrMap(t, &v2)) {
36+
} else if !attrMapEquals(v1.Attrs, v2.Attrs) {
4837
return false
4938
}
5039
}
@@ -138,10 +127,10 @@ func TestProclist(t *testing.T) {
138127
t.Error("bad status for req2; expecting 'init', got ", p2.Status)
139128
}
140129

141-
if !attrMapEquals(attrMap(t, &p1), attrs1) {
130+
if !attrMapEquals(p1.Attrs, attrs1) {
142131
t.Error("bad attribute set for req1")
143132
}
144-
if !attrMapEquals(attrMap(t, &p2), attrs2) {
133+
if !attrMapEquals(p2.Attrs, attrs2) {
145134
t.Error("bad attribute set for req2")
146135
}
147136

types.go

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,15 @@ import (
77
"time"
88
)
99

10-
// AttrDetail encodes a single, user-defined name/value pair.
11-
type AttrDetail struct {
12-
Name string `json:"name"`
13-
Value interface{} `json:"value"`
14-
}
15-
1610
// Type ProcDetail encodes a full process list from the server, including an
1711
// attributes array with application-defined names/values.
1812
type ProcDetail struct {
19-
Id string `json:"id"`
20-
Attrs []AttrDetail `json:"attrs,omitempty"`
21-
ProcTime time.Time `json:"procTime"`
22-
StatusTime time.Time `json:"statusTime"`
23-
Status string `json:"status"`
24-
Cancelling bool `json:"cancelling,omitempty"`
13+
Id string `json:"id"`
14+
Attrs map[string]interface{} `json:"attrs,omitempty"`
15+
ProcTime time.Time `json:"procTime"`
16+
StatusTime time.Time `json:"statusTime"`
17+
Status string `json:"status"`
18+
Cancelling bool `json:"cancelling,omitempty"`
2519
}
2620

2721
// ProcResponse is the response for a GET to /proc.

0 commit comments

Comments
 (0)