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

fix: the import of yaml and json files only read 4 field #36

parent a3c67f13
Branches
Tags v1.15.2
No related merge requests found
......@@ -3,11 +3,11 @@
"devenv": {
"locked": {
"dir": "src/modules",
"lastModified": 1698243190,
"narHash": "sha256-n+SbyNQRhUcaZoU00d+7wi17HJpw/kAUrXOL4zRcqE8=",
"lastModified": 1702549996,
"narHash": "sha256-mEN+8gjWUXRxBCcixeth+jlDNuzxbpFwZNOEc4K22vw=",
"owner": "cachix",
"repo": "devenv",
"rev": "86f476f7edb86159fd20764489ab4e4df6edb4b6",
"rev": "e681a99ffe2d2882f413a5d771129223c838ddce",
"type": "github"
},
"original": {
......@@ -74,11 +74,11 @@
},
"nixpkgs": {
"locked": {
"lastModified": 1701263465,
"narHash": "sha256-lNXUIlkfyDyp9Ox21hr+wsEf/IBklLvb6bYcyeXbdRc=",
"lastModified": 1702346276,
"narHash": "sha256-eAQgwIWApFQ40ipeOjVSoK4TEHVd6nbSd9fApiHIw5A=",
"owner": "nixos",
"repo": "nixpkgs",
"rev": "50aa30a13c4ab5e7ba282da460a3e3d44e9d0eb3",
"rev": "cf28ee258fd5f9a52de6b9865cdb93a1f96d09b7",
"type": "github"
},
"original": {
......@@ -106,11 +106,11 @@
},
"nixpkgs_2": {
"locked": {
"lastModified": 1698288402,
"narHash": "sha256-jIIjApPdm+4yt8PglX8pUOexAdEiAax/DXW3S/Mb21E=",
"lastModified": 1702350026,
"narHash": "sha256-A+GNZFZdfl4JdDphYKBJ5Ef1HOiFsP18vQe9mqjmUis=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "60b9db998f71ea49e1a9c41824d09aa274be1344",
"rev": "9463103069725474698139ab10f17a9d125da859",
"type": "github"
},
"original": {
......@@ -130,11 +130,11 @@
"nixpkgs-stable": "nixpkgs-stable"
},
"locked": {
"lastModified": 1698227354,
"narHash": "sha256-Fi5H9jbaQLmLw9qBi/mkR33CoFjNbobo5xWdX4tKz1Q=",
"lastModified": 1702456155,
"narHash": "sha256-I2XhXGAecdGlqi6hPWYT83AQtMgL+aa3ulA85RAEgOk=",
"owner": "cachix",
"repo": "pre-commit-hooks.nix",
"rev": "bd38df3d508dfcdff52cd243d297f218ed2257bf",
"rev": "007a45d064c1c32d04e1b8a0de5ef00984c419bc",
"type": "github"
},
"original": {
......@@ -171,11 +171,11 @@
"nixpkgs": "nixpkgs_2"
},
"locked": {
"lastModified": 1690668568,
"narHash": "sha256-jzixQKFFW4oxO0S4GYqbkFCXzhBd6com6Z9+MtVKakU=",
"lastModified": 1700695799,
"narHash": "sha256-nXRhRE69kULaNxijX7ZF14pGSu6Ar/FIvfKCIut7OXc=",
"ref": "refs/heads/master",
"rev": "3838f03165b726e47d586c04a1821749375e1001",
"revCount": 37,
"rev": "fdcc60bfd3642207e50e8e6c89c0a9a7b27a40a9",
"revCount": 41,
"type": "git",
"url": "https://gitlab.schukai.com/oss/utilities/version.git"
},
......
......@@ -6,6 +6,7 @@ package jobqueue
import (
"encoding/json"
"github.com/fsnotify/fsnotify"
"strings"
"time"
)
......@@ -36,23 +37,28 @@ type SchedulerPersistence struct {
EventFlags fsnotify.Op `yaml:"eventFlags,omitempty" json:"eventFlags,omitempty" gorm:"column:eventFlags"`
}
// UnmarshalJSON implements the json.Unmarshaler interface
func (sp *SchedulerPersistence) UnmarshalJSON(data []byte) error {
// Anonymous structure to avoid endless recursion
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),
}
type scheduleImportStruct struct {
Time *string `yaml:"time,omitempty" json:"time,omitempty"`
Interval *string `yaml:"interval,omitempty" json:"interval,omitempty"`
Delay *string `yaml:"delay,omitempty" json:"delay,omitempty"`
EventFlags *string `yaml:"eventFlags,omitempty" json:"eventFlags,omitempty"`
if err := json.Unmarshal(data, &aux); err != nil {
return err
Type string `yaml:"type" json:"type"`
Spec string `yaml:"spec,omitempty" json:"spec,omitempty"`
Event string `yaml:"event,omitempty" json:"event,omitempty"`
Executed bool `yaml:"executed,omitempty" json:"executed,omitempty"`
Path string `yaml:"path,omitempty" json:"path,omitempty"`
}
if aux.Time != nil {
func (sp *SchedulerPersistence) parseAndAssignFields(aux scheduleImportStruct) error {
sp.Type = aux.Type
sp.Spec = aux.Spec
sp.Event = EventName(aux.Event)
sp.Executed = aux.Executed
sp.Path = aux.Path
if aux.Time != nil && *aux.Time != "" {
var t time.Time
var err error
for _, format := range SupportedTimeFormats {
......@@ -67,7 +73,7 @@ func (sp *SchedulerPersistence) UnmarshalJSON(data []byte) error {
sp.Time = &t
}
if aux.Interval != nil {
if aux.Interval != nil && *aux.Interval != "" {
d, err := time.ParseDuration(*aux.Interval)
if err != nil {
return err
......@@ -75,46 +81,54 @@ func (sp *SchedulerPersistence) UnmarshalJSON(data []byte) error {
sp.Delay = d
}
return nil
if aux.Delay != nil && *aux.Delay != "" {
d, err := time.ParseDuration(*aux.Delay)
if err != nil {
return err
}
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),
sp.Delay = d
}
if err := unmarshal(&aux); err != nil {
return err
if aux.EventFlags != nil && *aux.EventFlags != "" {
sp.EventFlags = fsnotify.Op(0)
for _, flag := range strings.Split(*aux.EventFlags, "|") {
switch flag {
case "Create":
sp.EventFlags |= fsnotify.Create
case "Write":
sp.EventFlags |= fsnotify.Write
case "Remove":
sp.EventFlags |= fsnotify.Remove
case "Rename":
sp.EventFlags |= fsnotify.Rename
case "Chmod":
sp.EventFlags |= fsnotify.Chmod
}
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 nil
}
// UnmarshalJSON implements the json.Unmarshaler interface
func (sp *SchedulerPersistence) UnmarshalJSON(data []byte) error {
var aux scheduleImportStruct
if err := json.Unmarshal(data, &aux); err != nil {
return err
}
sp.Time = &t
return sp.parseAndAssignFields(aux)
}
if aux.Interval != nil {
d, err := time.ParseDuration(*aux.Interval)
if err != nil {
func (sp *SchedulerPersistence) UnmarshalYAML(unmarshal func(interface{}) error) error {
var aux scheduleImportStruct
if err := unmarshal(&aux); err != nil {
return err
}
sp.Delay = d
}
return nil
return sp.parseAndAssignFields(aux)
}
......@@ -8,6 +8,7 @@ import (
"github.com/fsnotify/fsnotify"
"github.com/robfig/cron/v3"
"github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"
"os"
"sync/atomic"
"testing"
......@@ -411,3 +412,24 @@ func TestInotifyScheduler_BasicFunctionality(t *testing.T) {
t.Errorf("Expected to run 2 times, ran %d times", count)
}
}
func TestUnmarshalSchedulerPersistenceYAML(t *testing.T) {
// Beispiel-YAML-Daten
yamlData := `
type: interval
interval: 1m
time: "2023-12-15T12:00:00Z"
`
var sp SchedulerPersistence
err := yaml.Unmarshal([]byte(yamlData), &sp)
assert.Nil(t, err, "Unmarshalling should not produce an error")
expectedInterval, _ := time.ParseDuration("1m")
expectedTime, _ := time.Parse(time.RFC3339, "2023-12-15T12:00:00Z")
// Prüfen, ob die Werte korrekt unmarshalled wurden
assert.Equal(t, "interval", sp.Type, "Type should be unmarshalled correctly")
assert.Equal(t, expectedInterval, sp.Delay, "Interval should be unmarshalled correctly")
assert.Equal(t, &expectedTime, sp.Time, "Time should be unmarshalled correctly")
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment