Move config into separate package

This commit is contained in:
9seconds
2021-03-11 05:37:18 +03:00
parent 757ea5b63c
commit 1a02511afe
15 changed files with 615 additions and 552 deletions
+41
View File
@@ -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
}