Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
21c7dd42
Commit
21c7dd42
authored
Sep 16, 2015
by
Daniel Martí
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add pkg/api/unversioned.Duration type
Similar to pkg/util.Time.
parent
7e375b9c
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
200 additions
and
0 deletions
+200
-0
duration.go
pkg/api/unversioned/duration.go
+47
-0
duration_test.go
pkg/api/unversioned/duration_test.go
+153
-0
No files found.
pkg/api/unversioned/duration.go
0 → 100644
View file @
21c7dd42
/*
Copyright 2014 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package
unversioned
import
(
"encoding/json"
"time"
)
// Duration is a wrapper around time.Duration which supports correct
// marshaling to YAML and JSON. In particular, it marshals into strings, which
// can be used as map keys in json.
type
Duration
struct
{
time
.
Duration
}
// UnmarshalJSON implements the json.Unmarshaller interface.
func
(
d
*
Duration
)
UnmarshalJSON
(
b
[]
byte
)
error
{
var
str
string
json
.
Unmarshal
(
b
,
&
str
)
pd
,
err
:=
time
.
ParseDuration
(
str
)
if
err
!=
nil
{
return
err
}
d
.
Duration
=
pd
return
nil
}
// MarshalJSON implements the json.Marshaler interface.
func
(
d
Duration
)
MarshalJSON
()
([]
byte
,
error
)
{
return
json
.
Marshal
(
d
.
String
())
}
pkg/api/unversioned/duration_test.go
0 → 100644
View file @
21c7dd42
/*
Copyright 2014 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package
unversioned
import
(
"encoding/json"
"testing"
"time"
"github.com/ghodss/yaml"
)
type
DurationHolder
struct
{
D
Duration
`json:"d"`
}
func
TestDurationMarshalYAML
(
t
*
testing
.
T
)
{
cases
:=
[]
struct
{
input
Duration
result
string
}{
{
Duration
{
5
*
time
.
Second
},
"d: 5s
\n
"
},
{
Duration
{
2
*
time
.
Minute
},
"d: 2m0s
\n
"
},
{
Duration
{
time
.
Hour
+
3
*
time
.
Millisecond
},
"d: 1h0m0.003s
\n
"
},
}
for
_
,
c
:=
range
cases
{
input
:=
DurationHolder
{
c
.
input
}
result
,
err
:=
yaml
.
Marshal
(
&
input
)
if
err
!=
nil
{
t
.
Errorf
(
"Failed to marshal input: %q: %v"
,
input
,
err
)
}
if
string
(
result
)
!=
c
.
result
{
t
.
Errorf
(
"Failed to marshal input: %q: expected %q, got %q"
,
input
,
c
.
result
,
string
(
result
))
}
}
}
func
TestDurationUnmarshalYAML
(
t
*
testing
.
T
)
{
cases
:=
[]
struct
{
input
string
result
Duration
}{
{
"d: 0s
\n
"
,
Duration
{}},
{
"d: 5s
\n
"
,
Duration
{
5
*
time
.
Second
}},
{
"d: 2m0s
\n
"
,
Duration
{
2
*
time
.
Minute
}},
{
"d: 1h0m0.003s
\n
"
,
Duration
{
time
.
Hour
+
3
*
time
.
Millisecond
}},
// Units with zero values can optionally be dropped
{
"d: 2m
\n
"
,
Duration
{
2
*
time
.
Minute
}},
{
"d: 1h0.003s
\n
"
,
Duration
{
time
.
Hour
+
3
*
time
.
Millisecond
}},
}
for
_
,
c
:=
range
cases
{
var
result
DurationHolder
if
err
:=
yaml
.
Unmarshal
([]
byte
(
c
.
input
),
&
result
);
err
!=
nil
{
t
.
Errorf
(
"Failed to unmarshal input %q: %v"
,
c
.
input
,
err
)
}
if
result
.
D
!=
c
.
result
{
t
.
Errorf
(
"Failed to unmarshal input %q: expected %q, got %q"
,
c
.
input
,
c
.
result
,
result
)
}
}
}
func
TestDurationMarshalJSON
(
t
*
testing
.
T
)
{
cases
:=
[]
struct
{
input
Duration
result
string
}{
{
Duration
{
5
*
time
.
Second
},
`{"d":"5s"}`
},
{
Duration
{
2
*
time
.
Minute
},
`{"d":"2m0s"}`
},
{
Duration
{
time
.
Hour
+
3
*
time
.
Millisecond
},
`{"d":"1h0m0.003s"}`
},
}
for
_
,
c
:=
range
cases
{
input
:=
DurationHolder
{
c
.
input
}
result
,
err
:=
json
.
Marshal
(
&
input
)
if
err
!=
nil
{
t
.
Errorf
(
"Failed to marshal input: %q: %v"
,
input
,
err
)
}
if
string
(
result
)
!=
c
.
result
{
t
.
Errorf
(
"Failed to marshal input: %q: expected %q, got %q"
,
input
,
c
.
result
,
string
(
result
))
}
}
}
func
TestDurationUnmarshalJSON
(
t
*
testing
.
T
)
{
cases
:=
[]
struct
{
input
string
result
Duration
}{
{
`{"d":"0s"}`
,
Duration
{}},
{
`{"d":"5s"}`
,
Duration
{
5
*
time
.
Second
}},
{
`{"d":"2m0s"}`
,
Duration
{
2
*
time
.
Minute
}},
{
`{"d":"1h0m0.003s"}`
,
Duration
{
time
.
Hour
+
3
*
time
.
Millisecond
}},
// Units with zero values can optionally be dropped
{
`{"d":"2m"}`
,
Duration
{
2
*
time
.
Minute
}},
{
`{"d":"1h0.003s"}`
,
Duration
{
time
.
Hour
+
3
*
time
.
Millisecond
}},
}
for
_
,
c
:=
range
cases
{
var
result
DurationHolder
if
err
:=
json
.
Unmarshal
([]
byte
(
c
.
input
),
&
result
);
err
!=
nil
{
t
.
Errorf
(
"Failed to unmarshal input %q: %v"
,
c
.
input
,
err
)
}
if
result
.
D
!=
c
.
result
{
t
.
Errorf
(
"Failed to unmarshal input %q: expected %q, got %q"
,
c
.
input
,
c
.
result
,
result
)
}
}
}
func
TestDurationMarshalJSONUnmarshalYAML
(
t
*
testing
.
T
)
{
cases
:=
[]
struct
{
input
Duration
}{
{
Duration
{}},
{
Duration
{
5
*
time
.
Second
}},
{
Duration
{
2
*
time
.
Minute
}},
{
Duration
{
time
.
Hour
+
3
*
time
.
Millisecond
}},
}
for
i
,
c
:=
range
cases
{
input
:=
DurationHolder
{
c
.
input
}
jsonMarshalled
,
err
:=
json
.
Marshal
(
&
input
)
if
err
!=
nil
{
t
.
Errorf
(
"%d-1: Failed to marshal input: '%v': %v"
,
i
,
input
,
err
)
}
var
result
DurationHolder
if
err
:=
yaml
.
Unmarshal
(
jsonMarshalled
,
&
result
);
err
!=
nil
{
t
.
Errorf
(
"%d-2: Failed to unmarshal '%+v': %v"
,
i
,
string
(
jsonMarshalled
),
err
)
}
if
input
.
D
!=
result
.
D
{
t
.
Errorf
(
"%d-4: Failed to marshal input '%#v': got %#v"
,
i
,
input
,
result
)
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment