X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/11ba2964a6cc44bd5c02fbb511bf413e52d03774..ef5ce10fcd3d3930bd3bbac310c5f39838c8325f:/sdk/go/arvados/duration.go diff --git a/sdk/go/arvados/duration.go b/sdk/go/arvados/duration.go index d7dff6ddc7..25eed010f2 100644 --- a/sdk/go/arvados/duration.go +++ b/sdk/go/arvados/duration.go @@ -7,7 +7,6 @@ package arvados import ( "encoding/json" "fmt" - "reflect" "time" ) @@ -15,7 +14,7 @@ import ( // a number of nanoseconds. type Duration time.Duration -// UnmarshalJSON implements json.Unmarshaler +// UnmarshalJSON implements json.Unmarshaler. func (d *Duration) UnmarshalJSON(data []byte) error { if data[0] == '"' { return d.Set(string(data[1 : len(data)-1])) @@ -23,37 +22,24 @@ func (d *Duration) UnmarshalJSON(data []byte) error { return fmt.Errorf("duration must be given as a string like \"600s\" or \"1h30m\"") } -// MarshalJSON implements json.Marshaler +// MarshalJSON implements json.Marshaler. func (d *Duration) MarshalJSON() ([]byte, error) { return json.Marshal(d.String()) } -// String implements fmt.Stringer +// String implements fmt.Stringer. func (d Duration) String() string { return time.Duration(d).String() } -// Duration returns a time.Duration +// Duration returns a time.Duration. func (d Duration) Duration() time.Duration { return time.Duration(d) } -// Set sets the current duration by parsing the string using time.ParseDuration +// Set implements the flag.Value interface and sets the duration value by using time.ParseDuration to parse the string. func (d *Duration) Set(s string) error { dur, err := time.ParseDuration(s) *d = Duration(dur) return err } - -// DurationMapStructureDecodeHook can be used to create a decoder for arvados.duration when using mapstructure -func DurationMapStructureDecodeHook() interface{} { - return func(f reflect.Type, t reflect.Type, data interface{}) (interface{}, error) { - var duration Duration - if f.Kind() != reflect.String || t != reflect.TypeOf(duration) { - return data, nil - } - - duration.Set(data.(string)) - return duration, nil - } -}