@@ -165,26 +165,59 @@ type Delay struct {
165165}
166166
167167func (d * Delay ) UnmarshalJSON (data []byte ) error {
168- var s time.Duration
169- if err := json .Unmarshal (data , & s ); err == nil {
170- d .Min = s
171- d .Max = s
168+ // Scalar form: a single duration applied to both bounds.
169+ if v , ok , err := parseJSONDuration (data ); err != nil {
170+ return err
171+ } else if ok {
172+ d .Min , d .Max = v , v
172173 return d .validate ()
173174 }
174175
176+ // Object form: {"min": ..., "max": ...}. Each bound is a duration string ("10ms") or a number
177+ // of nanoseconds — the same shapes the Lua and go_template_yaml engines already accept.
175178 var res struct {
176- Min time. Duration `json:"min"`
177- Max time. Duration `json:"max"`
179+ Min json. RawMessage `json:"min"`
180+ Max json. RawMessage `json:"max"`
178181 }
179-
180182 if err := json .Unmarshal (data , & res ); err != nil {
181183 return err
182184 }
183- d .Min = res .Min
184- d .Max = res .Max
185+ if len (res .Min ) > 0 {
186+ v , _ , err := parseJSONDuration (res .Min )
187+ if err != nil {
188+ return err
189+ }
190+ d .Min = v
191+ }
192+ if len (res .Max ) > 0 {
193+ v , _ , err := parseJSONDuration (res .Max )
194+ if err != nil {
195+ return err
196+ }
197+ d .Max = v
198+ }
185199 return d .validate ()
186200}
187201
202+ // parseJSONDuration reads a JSON scalar as a duration: a string like "10ms" (time.ParseDuration)
203+ // or a number of nanoseconds. ok is false when the value is not a scalar (e.g. an object), so the
204+ // caller can fall back to the {min, max} form.
205+ func parseJSONDuration (data []byte ) (time.Duration , bool , error ) {
206+ var v interface {}
207+ if err := json .Unmarshal (data , & v ); err != nil {
208+ return 0 , false , err
209+ }
210+ switch x := v .(type ) {
211+ case string :
212+ dur , err := time .ParseDuration (x )
213+ return dur , true , err
214+ case float64 :
215+ return time .Duration (int64 (x )), true , nil
216+ default :
217+ return 0 , false , nil
218+ }
219+ }
220+
188221func (d * Delay ) UnmarshalYAML (unmarshal func (interface {}) error ) error {
189222 var s time.Duration
190223 if err := unmarshal (& s ); err == nil {
0 commit comments