-
-
Notifications
You must be signed in to change notification settings - Fork 703
/
mqtt_setter.go
48 lines (40 loc) · 927 Bytes
/
mqtt_setter.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
package server
import (
"strconv"
"github.com/spf13/cast"
)
type setter struct {
topic string
fun func(string) error
}
func setterFunc[T any](conv func(string) (T, error), set func(T) error) func(string) error {
return func(payload string) error {
val, err := conv(payload)
if err == nil {
err = set(val)
}
return err
}
}
func floatSetter(set func(float64) error) func(string) error {
return setterFunc(parseFloat, set)
}
func floatPtrSetter(set func(*float64) error) func(string) error {
return func(s string) error {
var val *float64
if f, err := parseFloat(s); err == nil {
val = &f
} else if s != "" {
return err
}
return set(val)
}
}
func intSetter(set func(int) error) func(string) error {
return setterFunc(strconv.Atoi, set)
}
func boolSetter(set func(bool) error) func(string) error {
return setterFunc(func(v string) (bool, error) {
return cast.ToBoolE(v)
}, set)
}