evcc-io/server/helper.go
2024-02-24 17:52:15 +01:00

24 lines
502 B
Go

package server
import (
"fmt"
"math"
"strconv"
)
// pass converts a simple api without return value to api with nil error return value
func pass[T any](f func(T)) func(T) error {
return func(v T) error {
f(v)
return nil
}
}
// parseFloat rejects NaN and Inf values
func parseFloat(payload string) (float64, error) {
f, err := strconv.ParseFloat(payload, 64)
if err == nil && (math.IsNaN(f) || math.IsInf(f, 0)) {
err = fmt.Errorf("invalid float value: %s", payload)
}
return f, err
}