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
No related branches found
No related tags found
No related merge requests found
...@@ -3,11 +3,11 @@ ...@@ -3,11 +3,11 @@
"devenv": { "devenv": {
"locked": { "locked": {
"dir": "src/modules", "dir": "src/modules",
"lastModified": 1698243190, "lastModified": 1702549996,
"narHash": "sha256-n+SbyNQRhUcaZoU00d+7wi17HJpw/kAUrXOL4zRcqE8=", "narHash": "sha256-mEN+8gjWUXRxBCcixeth+jlDNuzxbpFwZNOEc4K22vw=",
"owner": "cachix", "owner": "cachix",
"repo": "devenv", "repo": "devenv",
"rev": "86f476f7edb86159fd20764489ab4e4df6edb4b6", "rev": "e681a99ffe2d2882f413a5d771129223c838ddce",
"type": "github" "type": "github"
}, },
"original": { "original": {
...@@ -74,11 +74,11 @@ ...@@ -74,11 +74,11 @@
}, },
"nixpkgs": { "nixpkgs": {
"locked": { "locked": {
"lastModified": 1701263465, "lastModified": 1702346276,
"narHash": "sha256-lNXUIlkfyDyp9Ox21hr+wsEf/IBklLvb6bYcyeXbdRc=", "narHash": "sha256-eAQgwIWApFQ40ipeOjVSoK4TEHVd6nbSd9fApiHIw5A=",
"owner": "nixos", "owner": "nixos",
"repo": "nixpkgs", "repo": "nixpkgs",
"rev": "50aa30a13c4ab5e7ba282da460a3e3d44e9d0eb3", "rev": "cf28ee258fd5f9a52de6b9865cdb93a1f96d09b7",
"type": "github" "type": "github"
}, },
"original": { "original": {
...@@ -106,11 +106,11 @@ ...@@ -106,11 +106,11 @@
}, },
"nixpkgs_2": { "nixpkgs_2": {
"locked": { "locked": {
"lastModified": 1698288402, "lastModified": 1702350026,
"narHash": "sha256-jIIjApPdm+4yt8PglX8pUOexAdEiAax/DXW3S/Mb21E=", "narHash": "sha256-A+GNZFZdfl4JdDphYKBJ5Ef1HOiFsP18vQe9mqjmUis=",
"owner": "NixOS", "owner": "NixOS",
"repo": "nixpkgs", "repo": "nixpkgs",
"rev": "60b9db998f71ea49e1a9c41824d09aa274be1344", "rev": "9463103069725474698139ab10f17a9d125da859",
"type": "github" "type": "github"
}, },
"original": { "original": {
...@@ -130,11 +130,11 @@ ...@@ -130,11 +130,11 @@
"nixpkgs-stable": "nixpkgs-stable" "nixpkgs-stable": "nixpkgs-stable"
}, },
"locked": { "locked": {
"lastModified": 1698227354, "lastModified": 1702456155,
"narHash": "sha256-Fi5H9jbaQLmLw9qBi/mkR33CoFjNbobo5xWdX4tKz1Q=", "narHash": "sha256-I2XhXGAecdGlqi6hPWYT83AQtMgL+aa3ulA85RAEgOk=",
"owner": "cachix", "owner": "cachix",
"repo": "pre-commit-hooks.nix", "repo": "pre-commit-hooks.nix",
"rev": "bd38df3d508dfcdff52cd243d297f218ed2257bf", "rev": "007a45d064c1c32d04e1b8a0de5ef00984c419bc",
"type": "github" "type": "github"
}, },
"original": { "original": {
...@@ -171,11 +171,11 @@ ...@@ -171,11 +171,11 @@
"nixpkgs": "nixpkgs_2" "nixpkgs": "nixpkgs_2"
}, },
"locked": { "locked": {
"lastModified": 1690668568, "lastModified": 1700695799,
"narHash": "sha256-jzixQKFFW4oxO0S4GYqbkFCXzhBd6com6Z9+MtVKakU=", "narHash": "sha256-nXRhRE69kULaNxijX7ZF14pGSu6Ar/FIvfKCIut7OXc=",
"ref": "refs/heads/master", "ref": "refs/heads/master",
"rev": "3838f03165b726e47d586c04a1821749375e1001", "rev": "fdcc60bfd3642207e50e8e6c89c0a9a7b27a40a9",
"revCount": 37, "revCount": 41,
"type": "git", "type": "git",
"url": "https://gitlab.schukai.com/oss/utilities/version.git" "url": "https://gitlab.schukai.com/oss/utilities/version.git"
}, },
......
...@@ -6,6 +6,7 @@ package jobqueue ...@@ -6,6 +6,7 @@ package jobqueue
import ( import (
"encoding/json" "encoding/json"
"github.com/fsnotify/fsnotify" "github.com/fsnotify/fsnotify"
"strings"
"time" "time"
) )
...@@ -36,23 +37,28 @@ type SchedulerPersistence struct { ...@@ -36,23 +37,28 @@ type SchedulerPersistence struct {
EventFlags fsnotify.Op `yaml:"eventFlags,omitempty" json:"eventFlags,omitempty" gorm:"column:eventFlags"` EventFlags fsnotify.Op `yaml:"eventFlags,omitempty" json:"eventFlags,omitempty" gorm:"column:eventFlags"`
} }
// UnmarshalJSON implements the json.Unmarshaler interface type scheduleImportStruct struct {
func (sp *SchedulerPersistence) UnmarshalJSON(data []byte) error { Time *string `yaml:"time,omitempty" json:"time,omitempty"`
// Anonymous structure to avoid endless recursion Interval *string `yaml:"interval,omitempty" json:"interval,omitempty"`
type Alias SchedulerPersistence Delay *string `yaml:"delay,omitempty" json:"delay,omitempty"`
aux := &struct { EventFlags *string `yaml:"eventFlags,omitempty" json:"eventFlags,omitempty"`
Time *string `json:"time,omitempty"`
Interval *string `json:"interval,omitempty"` // Ensure this matches the JSON field name
*Alias
}{
Alias: (*Alias)(sp),
}
if err := json.Unmarshal(data, &aux); err != nil { Type string `yaml:"type" json:"type"`
return err 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 t time.Time
var err error var err error
for _, format := range SupportedTimeFormats { for _, format := range SupportedTimeFormats {
...@@ -67,7 +73,7 @@ func (sp *SchedulerPersistence) UnmarshalJSON(data []byte) error { ...@@ -67,7 +73,7 @@ func (sp *SchedulerPersistence) UnmarshalJSON(data []byte) error {
sp.Time = &t sp.Time = &t
} }
if aux.Interval != nil { if aux.Interval != nil && *aux.Interval != "" {
d, err := time.ParseDuration(*aux.Interval) d, err := time.ParseDuration(*aux.Interval)
if err != nil { if err != nil {
return err return err
...@@ -75,46 +81,54 @@ func (sp *SchedulerPersistence) UnmarshalJSON(data []byte) error { ...@@ -75,46 +81,54 @@ func (sp *SchedulerPersistence) UnmarshalJSON(data []byte) error {
sp.Delay = d sp.Delay = d
} }
return nil if aux.Delay != nil && *aux.Delay != "" {
d, err := time.ParseDuration(*aux.Delay)
if err != nil {
return err
} }
sp.Delay = d
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 { if aux.EventFlags != nil && *aux.EventFlags != "" {
return err 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 return err
} }
sp.Time = &t
return sp.parseAndAssignFields(aux)
} }
if aux.Interval != nil { func (sp *SchedulerPersistence) UnmarshalYAML(unmarshal func(interface{}) error) error {
d, err := time.ParseDuration(*aux.Interval) var aux scheduleImportStruct
if err != nil { if err := unmarshal(&aux); err != nil {
return err return err
} }
sp.Delay = d
}
return nil return sp.parseAndAssignFields(aux)
} }
...@@ -8,6 +8,7 @@ import ( ...@@ -8,6 +8,7 @@ import (
"github.com/fsnotify/fsnotify" "github.com/fsnotify/fsnotify"
"github.com/robfig/cron/v3" "github.com/robfig/cron/v3"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"gopkg.in/yaml.v3"
"os" "os"
"sync/atomic" "sync/atomic"
"testing" "testing"
...@@ -411,3 +412,24 @@ func TestInotifyScheduler_BasicFunctionality(t *testing.T) { ...@@ -411,3 +412,24 @@ func TestInotifyScheduler_BasicFunctionality(t *testing.T) {
t.Errorf("Expected to run 2 times, ran %d times", count) 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