Skip to content
Snippets Groups Projects
Verified Commit a3c67f13 authored by Volker Schukai's avatar Volker Schukai :alien:
Browse files

fix: parse duration in json #35

parent 6112106b
No related branches found
No related tags found
No related merge requests found
......@@ -4,6 +4,7 @@
package jobqueue
import (
"encoding/json"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"
"testing"
......@@ -29,3 +30,31 @@ interval: "1h30m"
expectedInterval, _ := time.ParseDuration("1h30m")
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")
}
......@@ -42,6 +42,7 @@ func (sp *SchedulerPersistence) UnmarshalJSON(data []byte) error {
type Alias SchedulerPersistence
aux := &struct {
Time *string `json:"time,omitempty"`
Interval *string `json:"interval,omitempty"` // Ensure this matches the JSON field name
*Alias
}{
Alias: (*Alias)(sp),
......@@ -66,6 +67,14 @@ func (sp *SchedulerPersistence) UnmarshalJSON(data []byte) error {
sp.Time = &t
}
if aux.Interval != nil {
d, err := time.ParseDuration(*aux.Interval)
if err != nil {
return err
}
sp.Delay = d
}
return nil
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment