FILE / ScuroNeko/mtg

config/type_bytes.go

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

48 lines
768 B
Go

package config
import (
"fmt"
"strings"
"github.com/alecthomas/units"
)
type TypeBytes struct {
value uint
}
func (c *TypeBytes) UnmarshalText(data []byte) error {
if len(data) == 0 {
return nil
}
value, err := units.ParseStrictBytes(strings.ToUpper(string(data)))
if err != nil {
return fmt.Errorf("incorrect bytes value: %w", err)
}
if value < 0 {
return fmt.Errorf("%d should be positive number", value)
}
c.value = uint(value)
return nil
}
func (c TypeBytes) MarshalText() ([]byte, error) {
return []byte(c.String()), nil
}
func (c TypeBytes) String() string {
return units.ToString(int64(c.value), 1024, "ib", "b")
}
func (c TypeBytes) Value(defaultValue uint) uint {
if c.value == 0 {
return defaultValue
}
return c.value
}