-
Notifications
You must be signed in to change notification settings - Fork 2
/
session.go
62 lines (51 loc) · 1.11 KB
/
session.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
package traindown
import (
"encoding/json"
"time"
)
// Session is a collection of Movements that occurred.
type Session struct {
Date time.Time `json:"date"`
DefaultUnit string `json:"defaultUnit,omitempty"`
Errors []error `json:"errors"`
Movements []*Movement `json:"movements"`
Metadata Metadata `json:"metadata"`
Notes []string `json:"notes"`
}
/* Public */
// NewSession spits out a new Session
func NewSession() *Session {
return &Session{
Metadata: make(Metadata),
Movements: make([]*Movement, 0),
Notes: make([]string, 0),
}
}
func (s Session) String() string {
ss, _ := json.Marshal(s)
return string(ss)
}
// Volumes computes the volume performed by unit.
func (s Session) Volumes() map[string]float32 {
v := make(map[string]float32)
for _, m := range s.Movements {
mvs := m.Volumes()
for mu, mv := range mvs {
val, ok := v[mu]
if ok {
v[mu] = val + mv
} else {
v[mu] = mv
}
}
}
return v
}
/* Private */
func (s *Session) assignSpecial(k string, v string) bool {
if isUnit(k) {
s.DefaultUnit = v
return true
}
return false
}