mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-08-31 16:24:03 +03:00
FILE / ScuroNeko/mtg
config/type_float.go
Исходный файл и его история в репозитории.
42 lines
677 B
Go
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
|
|
}
|