blob: 2eaabf0794f7d640a018f0c40aafe334f6ae18f4 [file] [log] [blame]
Zack Williamse940c7a2019-08-21 14:25:39 -07001/*
2Copyright 2014 The Kubernetes Authors.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17package v1
18
19import (
20 "encoding/json"
21 "time"
22)
23
24// Duration is a wrapper around time.Duration which supports correct
25// marshaling to YAML and JSON. In particular, it marshals into strings, which
26// can be used as map keys in json.
27type Duration struct {
28 time.Duration `protobuf:"varint,1,opt,name=duration,casttype=time.Duration"`
29}
30
31// UnmarshalJSON implements the json.Unmarshaller interface.
32func (d *Duration) UnmarshalJSON(b []byte) error {
33 var str string
34 err := json.Unmarshal(b, &str)
35 if err != nil {
36 return err
37 }
38
39 pd, err := time.ParseDuration(str)
40 if err != nil {
41 return err
42 }
43 d.Duration = pd
44 return nil
45}
46
47// MarshalJSON implements the json.Marshaler interface.
48func (d Duration) MarshalJSON() ([]byte, error) {
49 return json.Marshal(d.Duration.String())
50}