mirror of
https://github.com/diamondburned/cchat-discord.git
synced 2024-12-27 14:27:43 +00:00
40 lines
571 B
Go
40 lines
571 B
Go
|
package config
|
||
|
|
||
|
import (
|
||
|
"strconv"
|
||
|
"time"
|
||
|
|
||
|
"github.com/pkg/errors"
|
||
|
)
|
||
|
|
||
|
type boolStamp struct {
|
||
|
stamp time.Duration
|
||
|
value bool
|
||
|
}
|
||
|
|
||
|
var _ customType = (*boolStamp)(nil)
|
||
|
|
||
|
func (bs boolStamp) Marshal() string {
|
||
|
if bs.stamp > 0 {
|
||
|
return bs.stamp.String()
|
||
|
}
|
||
|
|
||
|
return strconv.FormatBool(bs.value)
|
||
|
}
|
||
|
|
||
|
func (bs *boolStamp) Unmarshal(v string) error {
|
||
|
t, err := time.ParseDuration(v)
|
||
|
if err == nil && t > 0 {
|
||
|
bs.stamp = t
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
b, err := strconv.ParseBool(v)
|
||
|
if err == nil {
|
||
|
bs.value = b
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
return errors.New("invalid bool or timestamp")
|
||
|
}
|