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
+46
View File
@@ -0,0 +1,46 @@
package config
import (
"fmt"
"strings"
"time"
)
type TypeDuration struct {
value time.Duration
}
func (c *TypeDuration) UnmarshalText(data []byte) error {
if len(data) == 0 {
return nil
}
dur, err := time.ParseDuration(strings.ToLower(string(data)))
if err != nil {
return fmt.Errorf("incorrect duration: %w", err)
}
if dur < 0 {
return fmt.Errorf("%s should be positive duration", dur)
}
c.value = dur
return nil
}
func (c TypeDuration) MarshalText() ([]byte, error) { // nolint: unparam
return []byte(c.value.String()), nil
}
func (c TypeDuration) String() string {
return c.value.String()
}
func (c TypeDuration) Value(defaultValue time.Duration) time.Duration {
if c.value == 0 {
return defaultValue
}
return c.value
}