| // Copyright 2016 Canonical Ltd. |
| // Licensed under the LGPLv3, see LICENCE file for details. |
| // TimeDuration returns a Checker that accepts a string value, and returns |
| // the parsed time.Duration value. Emtpy strings are considered empty time.Duration |
| func TimeDuration() Checker { |
| type timeDurationC struct{} |
| // Coerce implements Checker Coerce method. |
| func (c timeDurationC) Coerce(v interface{}, path []string) (interface{}, error) { |
| return nil, error_{"string or time.Duration", v, path} |
| switch reflect.TypeOf(v).Kind() { |
| case reflect.TypeOf(empty).Kind(): |
| vstr := reflect.ValueOf(v).String() |
| v, err := time.ParseDuration(vstr) |
| return nil, error_{"string or time.Duration", v, path} |