forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalc.go
150 lines (126 loc) Β· 2.91 KB
/
calc.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package provider
import (
"errors"
"fmt"
"github.com/evcc-io/evcc/util"
)
type calcProvider struct {
add, mul, div []func() (float64, error)
sign func() (float64, error)
}
func init() {
registry.Add("calc", NewCalcFromConfig)
}
// NewCalcFromConfig creates calc provider
func NewCalcFromConfig(other map[string]interface{}) (Provider, error) {
var cc struct {
Add []Config
Mul []Config
Div []Config
Sign *Config
}
if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
}
o := &calcProvider{}
if i := min(len(cc.Add), 1) + min(len(cc.Mul), 1) + min(len(cc.Div), 1); i > 1 ||
(len(cc.Add) > 0 && cc.Sign != nil) ||
(len(cc.Mul) > 0 && cc.Sign != nil) ||
(len(cc.Div) > 0 && cc.Sign != nil) {
return nil, errors.New("can only have either add, mul, div or sign")
}
for idx, cc := range cc.Add {
f, err := NewFloatGetterFromConfig(cc)
if err != nil {
return nil, fmt.Errorf("add[%d]: %w", idx, err)
}
o.add = append(o.add, f)
}
for idx, cc := range cc.Mul {
f, err := NewFloatGetterFromConfig(cc)
if err != nil {
return nil, fmt.Errorf("mul[%d]: %w", idx, err)
}
o.mul = append(o.mul, f)
}
for idx, cc := range cc.Div {
f, err := NewFloatGetterFromConfig(cc)
if err != nil {
return nil, fmt.Errorf("div[%d]: %w", idx, err)
}
o.div = append(o.div, f)
}
if cc.Sign != nil {
f, err := NewFloatGetterFromConfig(*cc.Sign)
if err != nil {
return nil, fmt.Errorf("sign: %w", err)
}
o.sign = f
}
return o, nil
}
var _ IntProvider = (*calcProvider)(nil)
func (o *calcProvider) IntGetter() (func() (int64, error), error) {
return func() (int64, error) {
f, err := o.floatGetter()
return int64(f), err
}, nil
}
var _ StringProvider = (*calcProvider)(nil)
func (o *calcProvider) StringGetter() (func() (string, error), error) {
return func() (string, error) {
f, err := o.floatGetter()
return fmt.Sprintf("%c", int(f)), err
}, nil
}
var _ FloatProvider = (*calcProvider)(nil)
func (o *calcProvider) FloatGetter() (func() (float64, error), error) {
return o.floatGetter, nil
}
func (o *calcProvider) floatGetter() (float64, error) {
var res float64
switch {
case len(o.mul) > 0:
res = 1
for idx, p := range o.mul {
v, err := p()
if err != nil {
return 0, fmt.Errorf("mul[%d]: %w", idx, err)
}
res *= v
}
case len(o.add) > 0:
for idx, p := range o.add {
v, err := p()
if err != nil {
return 0, fmt.Errorf("add[%d]: %w", idx, err)
}
res += v
}
case len(o.div) > 0:
for idx, p := range o.div {
v, err := p()
if err != nil {
return 0, fmt.Errorf("div[%d]: %w", idx, err)
}
if idx == 0 {
if v == 0 {
break
}
res = v
} else if v != 0 {
res /= v
} else if v == 0 {
res = 0
break
}
}
default:
v, err := o.sign()
if err != nil {
return 0, fmt.Errorf("sign: %w", err)
}
res = map[bool]float64{false: -1, true: 1}[v >= 0]
}
return res, nil
}