Add yaml file reader to config

This commit is contained in:
2025-03-20 03:52:25 +04:00
parent 3cd72855e5
commit 459f7ee0dc
12 changed files with 293 additions and 95 deletions

View File

@@ -34,3 +34,24 @@ func (timeout *Timeout) UnmarshalJSON(b []byte) error {
func (timeout Timeout) MarshalJSON() ([]byte, error) {
return json.Marshal(timeout.Duration.String())
}
func (timeout *Timeout) UnmarshalYAML(unmarshal func(interface{}) error) error {
var v any
if err := unmarshal(&v); err != nil {
return err
}
switch value := v.(type) {
case float64:
timeout.Duration = time.Duration(value)
return nil
case string:
var err error
timeout.Duration, err = time.ParseDuration(value)
if err != nil {
return errors.New("Timeout is invalid (e.g. 400ms, 1s, 5m, 1h)")
}
return nil
default:
return errors.New("Timeout is invalid (e.g. 400ms, 1s, 5m, 1h)")
}
}