Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Job Queues
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Jira
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Container registry
Monitor
Service Desk
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
OSS
Libraries
Go
Services
Job Queues
Commits
a3c67f13
Verified
Commit
a3c67f13
authored
1 year ago
by
Volker Schukai
Browse files
Options
Downloads
Patches
Plain Diff
fix: parse duration in json
#35
parent
6112106b
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
schedule-interval_test.go
+29
-0
29 additions, 0 deletions
schedule-interval_test.go
scheduler.go
+10
-1
10 additions, 1 deletion
scheduler.go
with
39 additions
and
1 deletion
schedule-interval_test.go
+
29
−
0
View file @
a3c67f13
...
@@ -4,6 +4,7 @@
...
@@ -4,6 +4,7 @@
package
jobqueue
package
jobqueue
import
(
import
(
"encoding/json"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"
"gopkg.in/yaml.v3"
"testing"
"testing"
...
@@ -29,3 +30,31 @@ interval: "1h30m"
...
@@ -29,3 +30,31 @@ interval: "1h30m"
expectedInterval
,
_
:=
time
.
ParseDuration
(
"1h30m"
)
expectedInterval
,
_
:=
time
.
ParseDuration
(
"1h30m"
)
assert
.
Equal
(
t
,
expectedInterval
,
sp
.
Delay
,
"the interval should be parsed correctly"
)
assert
.
Equal
(
t
,
expectedInterval
,
sp
.
Delay
,
"the interval should be parsed correctly"
)
}
}
// TestUnmarshalJSON tests the UnmarshalJSON method
func
TestUnmarshalJSON
(
t
*
testing
.
T
)
{
// Example JSON string representing SchedulerPersistence
jsonStr
:=
`{
"time": "2023-12-06T15:04:05Z",
"interval": "1h30m"
}`
// Create an instance of SchedulerPersistence
sp
:=
SchedulerPersistence
{}
// Unmarshal the JSON string into the sp structure
err
:=
json
.
Unmarshal
([]
byte
(
jsonStr
),
&
sp
)
assert
.
NoError
(
t
,
err
,
"Unmarshalling should not produce an error"
)
// Verify that the Time field is correctly parsed
expectedTime
,
_
:=
time
.
Parse
(
time
.
RFC3339
,
"2023-12-06T15:04:05Z"
)
if
sp
.
Time
!=
nil
{
assert
.
Equal
(
t
,
expectedTime
,
*
sp
.
Time
,
"Time should be correctly parsed"
)
}
else
{
t
.
Error
(
"Time field is nil, but it should not be"
)
}
// Verify that the Delay field is correctly parsed
expectedInterval
,
_
:=
time
.
ParseDuration
(
"1h30m"
)
assert
.
Equal
(
t
,
expectedInterval
,
sp
.
Delay
,
"Interval should be correctly parsed"
)
}
This diff is collapsed.
Click to expand it.
scheduler.go
+
10
−
1
View file @
a3c67f13
...
@@ -41,7 +41,8 @@ func (sp *SchedulerPersistence) UnmarshalJSON(data []byte) error {
...
@@ -41,7 +41,8 @@ func (sp *SchedulerPersistence) UnmarshalJSON(data []byte) error {
// Anonymous structure to avoid endless recursion
// Anonymous structure to avoid endless recursion
type
Alias
SchedulerPersistence
type
Alias
SchedulerPersistence
aux
:=
&
struct
{
aux
:=
&
struct
{
Time
*
string
`json:"time,omitempty"`
Time
*
string
`json:"time,omitempty"`
Interval
*
string
`json:"interval,omitempty"`
// Ensure this matches the JSON field name
*
Alias
*
Alias
}{
}{
Alias
:
(
*
Alias
)(
sp
),
Alias
:
(
*
Alias
)(
sp
),
...
@@ -66,6 +67,14 @@ func (sp *SchedulerPersistence) UnmarshalJSON(data []byte) error {
...
@@ -66,6 +67,14 @@ func (sp *SchedulerPersistence) UnmarshalJSON(data []byte) error {
sp
.
Time
=
&
t
sp
.
Time
=
&
t
}
}
if
aux
.
Interval
!=
nil
{
d
,
err
:=
time
.
ParseDuration
(
*
aux
.
Interval
)
if
err
!=
nil
{
return
err
}
sp
.
Delay
=
d
}
return
nil
return
nil
}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment