From 2d8ecced476d53cb7ea8e19174078d6a802ef172 Mon Sep 17 00:00:00 2001
From: Volker Schukai <volker.schukai@schukai.com>
Date: Fri, 15 Dec 2023 12:36:40 +0100
Subject: [PATCH] fix: the import of yaml and json files only read 4 field  #36

---
 devenv.lock       |  32 +++++++-------
 go.mod            |   2 +-
 go.sum            |   2 +
 scheduler.go      | 110 ++++++++++++++++++++++++++--------------------
 scheduler_test.go |  22 ++++++++++
 5 files changed, 103 insertions(+), 65 deletions(-)

diff --git a/devenv.lock b/devenv.lock
index 12c3e67..5a1545e 100644
--- a/devenv.lock
+++ b/devenv.lock
@@ -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"
       },
diff --git a/go.mod b/go.mod
index fbe98cd..7eab331 100644
--- a/go.mod
+++ b/go.mod
@@ -8,7 +8,7 @@ require (
 	github.com/docker/go-connections v0.4.0
 	github.com/fsnotify/fsnotify v1.7.0
 	github.com/go-chi/chi/v5 v5.0.10
-	github.com/google/uuid v1.4.0
+	github.com/google/uuid v1.5.0
 	github.com/pkg/sftp v1.13.6
 	github.com/robfig/cron/v3 v3.0.1
 	github.com/shirou/gopsutil/v3 v3.23.11
diff --git a/go.sum b/go.sum
index 16a60ca..0ea7815 100644
--- a/go.sum
+++ b/go.sum
@@ -34,6 +34,8 @@ github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
 github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
 github.com/google/uuid v1.4.0 h1:MtMxsa51/r9yyhkyLsVeVt0B+BGQZzpQiTQ4eHZ8bc4=
 github.com/google/uuid v1.4.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
+github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU=
+github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
 github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
 github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
 github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
diff --git a/scheduler.go b/scheduler.go
index 942218c..c573543 100644
--- a/scheduler.go
+++ b/scheduler.go
@@ -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"`
+
+	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 err := json.Unmarshal(data, &aux); err != nil {
-		return err
-	}
+func (sp *SchedulerPersistence) parseAndAssignFields(aux scheduleImportStruct) error {
 
-	if aux.Time != nil {
+	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
 	}
 
+	if aux.Delay != nil && *aux.Delay != "" {
+		d, err := time.ParseDuration(*aux.Delay)
+		if err != nil {
+			return err
+		}
+		sp.Delay = d
+	}
+
+	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
+			}
+		}
+	}
+
 	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),
-	}
+// UnmarshalJSON implements the json.Unmarshaler interface
+func (sp *SchedulerPersistence) UnmarshalJSON(data []byte) error {
 
-	if err := unmarshal(&aux); err != nil {
+	var aux scheduleImportStruct
+
+	if err := json.Unmarshal(data, &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
-	}
+	return sp.parseAndAssignFields(aux)
 
-	if aux.Interval != nil {
-		d, err := time.ParseDuration(*aux.Interval)
-		if err != nil {
-			return err
-		}
-		sp.Delay = d
+}
+
+func (sp *SchedulerPersistence) UnmarshalYAML(unmarshal func(interface{}) error) error {
+	var aux scheduleImportStruct
+	if err := unmarshal(&aux); err != nil {
+		return err
 	}
 
-	return nil
+	return sp.parseAndAssignFields(aux)
+
 }
diff --git a/scheduler_test.go b/scheduler_test.go
index a37dc3d..7fc0d4a 100644
--- a/scheduler_test.go
+++ b/scheduler_test.go
@@ -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")
+}
-- 
GitLab