mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-08-31 20:24:03 +03:00
Move config into separate package
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
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) { // nolint: unparam
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user