-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathzMathTotal.go
More file actions
62 lines (48 loc) · 1.03 KB
/
Copy pathzMathTotal.go
File metadata and controls
62 lines (48 loc) · 1.03 KB
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 netroutine
import (
"context"
"encoding/json"
)
const idMathTotal = "MathTotal"
func init() {
blocks[idMathTotal] = &MathTotal{}
}
type MathTotal struct {
FromKey string
ToKey string
}
func (b *MathTotal) toBytes() ([]byte, error) {
return json.Marshal(b)
}
func (b *MathTotal) fromBytes(bytes []byte) error {
return json.Unmarshal(bytes, b)
}
func (b *MathTotal) kind() string {
return idMathTotal
}
func (b *MathTotal) Run(ctx context.Context, wce *Environment) (string, Status) {
var total float64
v, ok := wce.getData(b.FromKey)
if !ok {
return log(b, missingWorkingData(b.FromKey), Error)
}
s, ok := v.([]interface{})
if !ok {
f, err := toFloat64(v)
if err != nil {
return log(b, reportWrongType(b.FromKey), Error)
}
total = f
wce.setData(b.ToKey, total)
return log(b, setWorkingData(b.ToKey, total), Success)
}
for _, v := range s {
f, err := toFloat64(v)
if err != nil {
continue
}
total += f
}
wce.setData(b.ToKey, total)
return log(b, setWorkingData(b.ToKey, total), Success)
}