flamenco/internal/manager/config/time_duration.go
Sybren A. Stüvel 4492d824cb Manager: Add custom Duration type for the configuration file
Add a custom `time.Duration` wrapper that can be marshalled to JSON as
well. Go's built-in marshaller just treats it as an `int64` and
therefore the values are nanoseconds. This new wrapper keeps the JSON
representation the same as the YAML marshaller (which uses the
`time.Duration.String()` function).

In preparation for !104406.
2025-07-01 19:43:09 +02:00

58 lines
1.2 KiB
Go

package config
// SPDX-License-Identifier: GPL-3.0-or-later
import (
"encoding/json"
"errors"
"time"
yaml "gopkg.in/yaml.v2"
)
// Duration is a time.Duration with custom JSON/YAML marshallers.
type Duration time.Duration
var _ json.Unmarshaler = (*Duration)(nil)
var _ json.Marshaler = Duration(0)
var _ yaml.Unmarshaler = (*Duration)(nil)
var _ yaml.Marshaler = Duration(0)
func (d Duration) MarshalJSON() ([]byte, error) {
return json.Marshal(time.Duration(d).String())
}
func (d *Duration) UnmarshalJSON(b []byte) error {
var v any
if err := json.Unmarshal(b, &v); err != nil {
return err
}
return d.unmarshal(v)
}
func (d Duration) MarshalYAML() (any, error) {
return time.Duration(d).String(), nil
}
func (d *Duration) UnmarshalYAML(unmarshal func(any) error) error {
stringValue := ""
if err := unmarshal(&stringValue); err != nil {
return err
}
return d.unmarshal(stringValue)
}
func (d *Duration) unmarshal(v any) error {
switch value := v.(type) {
case string:
timeDuration, err := time.ParseDuration(value)
if err != nil {
return err
}
*d = Duration(timeDuration)
return nil
default:
return errors.New("invalid duration")
}
}