1/* 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} 51 52// ToUnstructured implements the value.UnstructuredConverter interface. 53func (d Duration) ToUnstructured() interface{} { 54 return d.Duration.String() 55} 56 57// OpenAPISchemaType is used by the kube-openapi generator when constructing 58// the OpenAPI spec of this type. 59// 60// See: https://github.com/kubernetes/kube-openapi/tree/master/pkg/generators 61func (_ Duration) OpenAPISchemaType() []string { return []string{"string"} } 62 63// OpenAPISchemaFormat is used by the kube-openapi generator when constructing 64// the OpenAPI spec of this type. 65func (_ Duration) OpenAPISchemaFormat() string { return "" } 66