Skip to content

Commit

Permalink
+code for SliceOfJSONRelayState
Browse files Browse the repository at this point in the history
  • Loading branch information
kneerunjun committed Apr 7, 2021
1 parent b029596 commit b909fe1
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 16 deletions.
27 changes: 27 additions & 0 deletions sch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
)

func init() {
Expand Down Expand Up @@ -143,3 +144,29 @@ func TestOverlappingSchedules(t *testing.T) {
}
}
}

// TestToSchedules : will test conversion of slice of json relay state to schedules
func TestToSchedules(t *testing.T) {
jrs := SliceOfJSONRelayState{
{ON: "06:30 PM", OFF: "06:30 AM", IDs: []string{"IN1", "IN2"}, Primary: true},
{ON: "04:30 PM", OFF: "06:29 PM", IDs: []string{"IN1"}, Primary: false},
}
scheds := []Schedule{}
err := jrs.ToSchedules(&scheds)
assert.Nil(t, err, "Error converting to a slice of Schedules")
if err != nil {
return
}
for _, s := range scheds {
t.Log(s)
t.Log(s.Conflicts())
}
scheds, err = ReadScheduleFile("./test_sched2.json")
assert.Nil(t, err, "Unexpected error reading schedules from file")
if err != nil {
return
}
for _, s := range scheds {
t.Logf("%v:%d", s, s.Conflicts())
}
}
64 changes: 48 additions & 16 deletions sched.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,33 @@ func (jrs *JSONRelayState) ToSchedule() (Schedule, error) {

}

// SliceOfJSONRelayState : we are just encapsulating the slices to extend functions over it
type SliceOfJSONRelayState []JSONRelayState

// From a SliceOfJSONRelayState to a slice of schedules, this not only converts but also marks the schedules with conflicts
// Used when reading schedules from files or API payloads
func (sofjrs SliceOfJSONRelayState) ToSchedules(scheds *[]Schedule) error {
result := []Schedule{}
// converting from json schedules to schedule object slice
for _, s := range sofjrs {
sched, err := s.ToSchedule()
if err != nil {
return err
}
result = append(result, sched)
}
// flagging conflicts
for i, s := range result {
for _, ss := range result[i+1:] {
if s.ConflictsWith(ss) {
ss.AddConflict()
}
}
}
*scheds = result
return nil
}

// ReadScheduleFile : just so that we can read json schedule file, and get slice of schedules
// we have also added some conflict detection in here
// Call this from the client function to get schedules with their conflict numbers
Expand All @@ -205,26 +232,31 @@ func ReadScheduleFile(file string) ([]Schedule, error) {
}
jsonFile.Close() // since this returns a closure, the call to this cannot be deferred
type conf struct {
Schedules []JSONRelayState `json:"schedules"`
Schedules SliceOfJSONRelayState `json:"schedules"`
}
c := conf{}
json.Unmarshal(bytes, &c)
scheds := []Schedule{}
// converting from json schedules to schedule object slice
for _, s := range c.Schedules {
sched, err := s.ToSchedule()
if err != nil {
return nil, err
}
scheds = append(scheds, sched)
}
// flagging conflicts
for i, s := range scheds {
for _, ss := range scheds[i+1:] {
if s.ConflictsWith(ss) {
ss.AddConflict()
}
}
if err := c.Schedules.ToSchedules(&scheds); err != nil {
return nil, err
}
// +++++++++++++++ drop this when you are done testing
// converting from json schedules to schedule object slice
// for _, s := range c.Schedules {
// sched, err := s.ToSchedule()
// if err != nil {
// return nil, err
// }
// scheds = append(scheds, sched)
// }
// // flagging conflicts
// for i, s := range scheds {
// for _, ss := range scheds[i+1:] {
// if s.ConflictsWith(ss) {
// ss.AddConflict()
// }
// }
// }
// ++++++++++++++++++++++
return scheds, nil
}
6 changes: 6 additions & 0 deletions test_config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"schedules": [
{"on":"06:00 PM", "off":"07:15 AM","primary":true, "ids":["IN1","IN2","IN3","IN4"]},
{"on":"04:30 AM", "off":"10:00 PM","primary":false, "ids":["IN4"]}
]
}

0 comments on commit b909fe1

Please sign in to comment.