Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
ollama
Commits
af47413d
Unverified
Commit
af47413d
authored
May 06, 2024
by
Jackie Li
Committed by
GitHub
May 06, 2024
Browse files
Add MarshalJSON to Duration (#3284)
--------- Co-authored-by:
Patrick Devine
<
patrick@infrahq.com
>
parent
d091fe3c
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
67 additions
and
1 deletion
+67
-1
api/types.go
api/types.go
+10
-1
api/types_test.go
api/types_test.go
+57
-0
No files found.
api/types.go
View file @
af47413d
...
@@ -436,6 +436,13 @@ type Duration struct {
...
@@ -436,6 +436,13 @@ type Duration struct {
time
.
Duration
time
.
Duration
}
}
func
(
d
Duration
)
MarshalJSON
()
([]
byte
,
error
)
{
if
d
.
Duration
<
0
{
return
[]
byte
(
"-1"
),
nil
}
return
[]
byte
(
"
\"
"
+
d
.
Duration
.
String
()
+
"
\"
"
),
nil
}
func
(
d
*
Duration
)
UnmarshalJSON
(
b
[]
byte
)
(
err
error
)
{
func
(
d
*
Duration
)
UnmarshalJSON
(
b
[]
byte
)
(
err
error
)
{
var
v
any
var
v
any
if
err
:=
json
.
Unmarshal
(
b
,
&
v
);
err
!=
nil
{
if
err
:=
json
.
Unmarshal
(
b
,
&
v
);
err
!=
nil
{
...
@@ -449,7 +456,7 @@ func (d *Duration) UnmarshalJSON(b []byte) (err error) {
...
@@ -449,7 +456,7 @@ func (d *Duration) UnmarshalJSON(b []byte) (err error) {
if
t
<
0
{
if
t
<
0
{
d
.
Duration
=
time
.
Duration
(
math
.
MaxInt64
)
d
.
Duration
=
time
.
Duration
(
math
.
MaxInt64
)
}
else
{
}
else
{
d
.
Duration
=
time
.
Duration
(
t
*
float64
(
time
.
Second
))
d
.
Duration
=
time
.
Duration
(
int
(
t
)
*
int
(
time
.
Second
))
}
}
case
string
:
case
string
:
d
.
Duration
,
err
=
time
.
ParseDuration
(
t
)
d
.
Duration
,
err
=
time
.
ParseDuration
(
t
)
...
@@ -459,6 +466,8 @@ func (d *Duration) UnmarshalJSON(b []byte) (err error) {
...
@@ -459,6 +466,8 @@ func (d *Duration) UnmarshalJSON(b []byte) (err error) {
if
d
.
Duration
<
0
{
if
d
.
Duration
<
0
{
d
.
Duration
=
time
.
Duration
(
math
.
MaxInt64
)
d
.
Duration
=
time
.
Duration
(
math
.
MaxInt64
)
}
}
default
:
return
fmt
.
Errorf
(
"Unsupported type: '%s'"
,
reflect
.
TypeOf
(
v
))
}
}
return
nil
return
nil
...
...
api/types_test.go
View file @
af47413d
...
@@ -21,6 +21,11 @@ func TestKeepAliveParsingFromJSON(t *testing.T) {
...
@@ -21,6 +21,11 @@ func TestKeepAliveParsingFromJSON(t *testing.T) {
req
:
`{ "keep_alive": 42 }`
,
req
:
`{ "keep_alive": 42 }`
,
exp
:
&
Duration
{
42
*
time
.
Second
},
exp
:
&
Duration
{
42
*
time
.
Second
},
},
},
{
name
:
"Positive Float"
,
req
:
`{ "keep_alive": 42.5 }`
,
exp
:
&
Duration
{
42
*
time
.
Second
},
},
{
{
name
:
"Positive Integer String"
,
name
:
"Positive Integer String"
,
req
:
`{ "keep_alive": "42m" }`
,
req
:
`{ "keep_alive": "42m" }`
,
...
@@ -31,6 +36,11 @@ func TestKeepAliveParsingFromJSON(t *testing.T) {
...
@@ -31,6 +36,11 @@ func TestKeepAliveParsingFromJSON(t *testing.T) {
req
:
`{ "keep_alive": -1 }`
,
req
:
`{ "keep_alive": -1 }`
,
exp
:
&
Duration
{
math
.
MaxInt64
},
exp
:
&
Duration
{
math
.
MaxInt64
},
},
},
{
name
:
"Negative Float"
,
req
:
`{ "keep_alive": -3.14 }`
,
exp
:
&
Duration
{
math
.
MaxInt64
},
},
{
{
name
:
"Negative Integer String"
,
name
:
"Negative Integer String"
,
req
:
`{ "keep_alive": "-1m" }`
,
req
:
`{ "keep_alive": "-1m" }`
,
...
@@ -48,3 +58,50 @@ func TestKeepAliveParsingFromJSON(t *testing.T) {
...
@@ -48,3 +58,50 @@ func TestKeepAliveParsingFromJSON(t *testing.T) {
})
})
}
}
}
}
func
TestDurationMarshalUnmarshal
(
t
*
testing
.
T
)
{
tests
:=
[]
struct
{
name
string
input
time
.
Duration
expected
time
.
Duration
}{
{
"negative duration"
,
time
.
Duration
(
-
1
),
time
.
Duration
(
math
.
MaxInt64
),
},
{
"positive duration"
,
time
.
Duration
(
42
*
time
.
Second
),
time
.
Duration
(
42
*
time
.
Second
),
},
{
"another positive duration"
,
time
.
Duration
(
42
*
time
.
Minute
),
time
.
Duration
(
42
*
time
.
Minute
),
},
{
"zero duration"
,
time
.
Duration
(
0
),
time
.
Duration
(
0
),
},
{
"max duration"
,
time
.
Duration
(
math
.
MaxInt64
),
time
.
Duration
(
math
.
MaxInt64
),
},
}
for
_
,
test
:=
range
tests
{
t
.
Run
(
test
.
name
,
func
(
t
*
testing
.
T
)
{
b
,
err
:=
json
.
Marshal
(
Duration
{
test
.
input
})
require
.
NoError
(
t
,
err
)
var
d
Duration
err
=
json
.
Unmarshal
(
b
,
&
d
)
require
.
NoError
(
t
,
err
)
assert
.
Equal
(
t
,
test
.
expected
,
d
.
Duration
,
"input %v, marshalled %v, got %v"
,
test
.
input
,
string
(
b
),
d
.
Duration
)
})
}
}
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