package provider import ( "fmt" "time" ) // Duration is a time.Duration that accepts the spellings a module.yaml uses: // a Go duration string ("25s", "1200ms") or a bare number meaning seconds. // yaml.v3 decodes time.Duration only as raw nanoseconds, which no one writes. type Duration time.Duration // D returns the value as a time.Duration, or def when unset. func (d Duration) D(def time.Duration) time.Duration { if d == 0 { return def } return time.Duration(d) } func (d *Duration) UnmarshalYAML(unmarshal func(any) error) error { var s string if err := unmarshal(&s); err == nil { if s == "" { *d = 0 return nil } v, err := time.ParseDuration(s) if err != nil { return fmt.Errorf("invalid duration %q: %w", s, err) } *d = Duration(v) return nil } var f float64 if err := unmarshal(&f); err != nil { return fmt.Errorf("duration must be a string like \"25s\" or a number of seconds") } *d = Duration(time.Duration(f * float64(time.Second))) return nil }