FILE / ScuroNeko/mtg

config/type_float.go

Исходный файл и его история в репозитории.
FILE d6566e5dfbaf4ec7ccf5fdda22d8bb641bcc19db
Files
mtg/config/type_float.go
T

42 lines
677 B
Go

package config
import (
"fmt"
"strconv"
)
type TypeFloat struct {
value float64
}
func (c *TypeFloat) UnmarshalJSON(data []byte) error {
value, err := strconv.ParseFloat(string(data), 64)
if err != nil {
return fmt.Errorf("incorrect float value: %w", err)
}
if value < 0 {
return fmt.Errorf("%f should be positive", value)
}
c.value = value
return nil
}
func (c *TypeFloat) MarshalText() ([]byte, error) {
return []byte(c.String()), nil
}
func (c TypeFloat) String() string {
return strconv.FormatFloat(c.value, 'f', -1, 64)
}
func (c TypeFloat) Value(defaultValue float64) float64 {
if c.value < 0.00001 {
return defaultValue
}
return c.value
}