/
zMathDivide.go
58 lines (44 loc) · 1.04 KB
/
zMathDivide.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
package netroutine
import (
"context"
"encoding/json"
)
const idMathDivide = "MathDivide"
func init() {
blocks[idMathDivide] = &MathDivide{}
}
type MathDivide struct {
Source1Key string
Source2Key string
ToKey string
}
func (b *MathDivide) toBytes() ([]byte, error) {
return json.Marshal(b)
}
func (b *MathDivide) fromBytes(bytes []byte) error {
return json.Unmarshal(bytes, b)
}
func (b *MathDivide) kind() string {
return idMathDivide
}
func (b *MathDivide) Run(ctx context.Context, wce *Environment) (string, Status) {
s1, ok := wce.getData(b.Source1Key)
if !ok {
return log(b, missingWorkingData(b.Source1Key), Error)
}
s1f, err := toFloat64(s1)
if err != nil {
return log(b, reportWrongType(b.Source1Key), Error)
}
s2, ok := wce.getData(b.Source2Key)
if !ok {
return log(b, missingWorkingData(b.Source2Key), Error)
}
s2f, err := toFloat64(s2)
if err != nil {
return log(b, reportWrongType(b.Source2Key), Error)
}
v := s1f / s2f
wce.setData(b.ToKey, v)
return log(b, setWorkingData(b.ToKey, v), Success)
}