forked from evcc-io/evcc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: simplify how mqtt setters are defined (evcc-io#12428)
- Loading branch information
Showing
2 changed files
with
82 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package server | ||
|
||
import ( | ||
"strconv" | ||
) | ||
|
||
type setter struct { | ||
topic string | ||
fun func(string) error | ||
} | ||
|
||
func setterFuncErr[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 setterFunc[T any](conv func(string) (T, error), set func(T)) func(string) error { | ||
return setterFuncErr(conv, func(val T) error { | ||
set(val) | ||
return nil | ||
}) | ||
} | ||
|
||
func floatSetterErr(set func(float64) error) func(string) error { | ||
return setterFuncErr(parseFloat, set) | ||
} | ||
|
||
func floatSetter(set func(float64)) func(string) error { | ||
return setterFunc(parseFloat, set) | ||
} | ||
|
||
func intSetterErr(set func(int) error) func(string) error { | ||
return setterFuncErr(strconv.Atoi, set) | ||
} | ||
|
||
func intSetter(set func(int)) func(string) error { | ||
return setterFunc(strconv.Atoi, set) | ||
} |