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

fix: parse duration #34

parent 1eef22ed
No related branches found
No related tags found
No related merge requests found
......@@ -49,4 +49,5 @@ var (
ErrCannotLoadStatsFromDatabase = fmt.Errorf("errors while loading stats from database")
ErrInvalidTime = fmt.Errorf("invalid time")
ErrSchedulerMisconfiguration = fmt.Errorf("scheduler misconfiguration")
ErrInvalidDuration = fmt.Errorf("invalid duration")
)
// Copyright 2023 schukai GmbH
// SPDX-License-Identifier: AGPL-3.0
package jobqueue
import (
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"
"testing"
"time"
)
// TestUnmarshalYAML tests the unmarshalling of the SchedulerPersistence struct
func TestUnmarshalYAML(t *testing.T) {
yamlStr := `
time: "2023-12-06T15:04:05Z"
interval: "1h30m"
`
sp := SchedulerPersistence{}
err := yaml.Unmarshal([]byte(yamlStr), &sp)
assert.NoError(t, err)
expectedTime, _ := time.Parse(time.RFC3339, "2023-12-06T15:04:05Z")
assert.Equal(t, expectedTime, *sp.Time, "the time should be parsed correctly")
expectedInterval, _ := time.ParseDuration("1h30m")
assert.Equal(t, expectedInterval, sp.Delay, "the interval should be parsed correctly")
}
......@@ -68,3 +68,44 @@ func (sp *SchedulerPersistence) UnmarshalJSON(data []byte) error {
return nil
}
func (sp *SchedulerPersistence) UnmarshalYAML(unmarshal func(interface{}) error) error {
// Anonymous structure to avoid endless recursion
type Alias SchedulerPersistence
aux := &struct {
Time *string `yaml:"time,omitempty"`
Interval *string `yaml:"interval,omitempty"`
*Alias
}{
Alias: (*Alias)(sp),
}
if err := unmarshal(&aux); err != nil {
return err
}
if aux.Time != nil {
var t time.Time
var err error
for _, format := range SupportedTimeFormats {
t, err = time.Parse(format, *aux.Time)
if err == nil {
break
}
}
if err != nil {
return err
}
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