mirror of
https://github.com/bluenviron/mediamtx
synced 2024-12-12 09:44:59 +00:00
37 lines
805 B
Go
37 lines
805 B
Go
package conf
|
|
|
|
import (
|
|
"encoding/json"
|
|
"time"
|
|
)
|
|
|
|
// StringDuration is a duration that is unmarshaled from a string.
|
|
// Durations are normally unmarshaled from numbers.
|
|
type StringDuration time.Duration
|
|
|
|
// MarshalJSON implements json.Marshaler.
|
|
func (d StringDuration) MarshalJSON() ([]byte, error) {
|
|
return json.Marshal(time.Duration(d).String())
|
|
}
|
|
|
|
// UnmarshalJSON implements json.Unmarshaler.
|
|
func (d *StringDuration) UnmarshalJSON(b []byte) error {
|
|
var in string
|
|
if err := json.Unmarshal(b, &in); err != nil {
|
|
return err
|
|
}
|
|
|
|
du, err := time.ParseDuration(in)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
*d = StringDuration(du)
|
|
|
|
return nil
|
|
}
|
|
|
|
// UnmarshalEnv implements env.Unmarshaler.
|
|
func (d *StringDuration) UnmarshalEnv(_ string, v string) error {
|
|
return d.UnmarshalJSON([]byte(`"` + v + `"`))
|
|
}
|