Files
945bf7731a Add collection-interval to metrics-generator user-configurable overrides (#2899)
* Add collection-interval to metrics-generator user-configurable overrides

Signed-off-by: Robbie Lankford <[email protected]>

* update CHANGELOG.md

* Update modules/overrides/userconfigurable/client/limits.go

Co-authored-by: Koenraad Verheyden <[email protected]>

* add collection interval validation

* implement json.Unmarshaler for duration type

* implement json.Marshaler for duration type

* fix tests/lint

* move duration code

---------

Signed-off-by: Robbie Lankford <[email protected]>
Co-authored-by: Koenraad Verheyden <[email protected]>
2023-09-13 14:47:41 +02:00

46 lines
837 B
Go

package client
import (
"encoding/json"
"fmt"
"time"
)
type Duration struct {
time.Duration
}
// MarshalJSON implements json.Marshaler.
func (d *Duration) MarshalJSON() ([]byte, error) {
return []byte(fmt.Sprintf(`"%s"`, d.String())), nil
}
// UnmarshalJSON implements json.Unmarshaler.
func (d *Duration) UnmarshalJSON(input []byte) error {
var unmarshalledJSON interface{}
err := json.Unmarshal(input, &unmarshalledJSON)
if err != nil {
return err
}
switch value := unmarshalledJSON.(type) {
case float64:
d.Duration = time.Duration(value)
case string:
d.Duration, err = time.ParseDuration(value)
if err != nil {
return err
}
default:
return fmt.Errorf("invalid duration: %#v", unmarshalledJSON)
}
return nil
}
var (
_ json.Marshaler = (*Duration)(nil)
_ json.Unmarshaler = (*Duration)(nil)
)