Skip to content

Commit

Permalink
fix: change worker lastCheckedIn to type int64 (#109)
Browse files Browse the repository at this point in the history
  • Loading branch information
Emmanuel Meinen committed Sep 22, 2020
1 parent 0e83c29 commit 24f0a72
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 21 deletions.
11 changes: 8 additions & 3 deletions database/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type Worker struct {
Address sql.NullString `sql:"address"`
Routes pq.StringArray `sql:"routes"`
Active sql.NullBool `sql:"active"`
LastCheckedIn sql.NullTime `sql:"last_checked_in"`
LastCheckedIn sql.NullInt64 `sql:"last_checked_in"`
}

// Nullify ensures the valid flag for
Expand Down Expand Up @@ -58,6 +58,11 @@ func (w *Worker) Nullify() *Worker {
w.Hostname.Valid = false
}

// check if the LastCheckedIn field should be false
if w.LastCheckedIn.Int64 == 0 {
w.LastCheckedIn.Valid = false
}

return w
}

Expand All @@ -71,7 +76,7 @@ func (w *Worker) ToLibrary() *library.Worker {
worker.SetAddress(w.Address.String)
worker.SetRoutes(w.Routes)
worker.SetActive(w.Active.Bool)
worker.SetLastCheckedIn(w.LastCheckedIn.Time)
worker.SetLastCheckedIn(w.LastCheckedIn.Int64)
return worker
}

Expand Down Expand Up @@ -100,7 +105,7 @@ func WorkerFromLibrary(w *library.Worker) *Worker {
Address: sql.NullString{String: w.GetAddress(), Valid: true},
Routes: w.GetRoutes(),
Active: sql.NullBool{Bool: w.GetActive(), Valid: true},
LastCheckedIn: sql.NullTime{Time: w.GetLastCheckedIn(), Valid: true},
LastCheckedIn: sql.NullInt64{Int64: w.GetLastCheckedIn(), Valid: true},
}

return worker.Nullify()
Expand Down
13 changes: 6 additions & 7 deletions database/worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"database/sql"
"reflect"
"testing"
"time"

"github.com/go-vela/types/library"
)
Expand All @@ -22,7 +21,7 @@ func TestDatabase_Worker_Nullify(t *testing.T) {
Hostname: sql.NullString{String: "", Valid: false},
Address: sql.NullString{String: "", Valid: false},
Active: sql.NullBool{Bool: false, Valid: false},
LastCheckedIn: sql.NullTime{Time: time.Time{}, Valid: false},
LastCheckedIn: sql.NullInt64{Int64: 0, Valid: false},
}

// setup tests
Expand Down Expand Up @@ -63,7 +62,7 @@ func TestDatabase_Worker_ToLibrary(t *testing.T) {
want.SetAddress("http://localhost:8080")
want.SetRoutes([]string{"vela"})
want.SetActive(true)
want.SetLastCheckedIn(time.Time{})
want.SetLastCheckedIn(1563474077)

// run test
got := testWorker().ToLibrary()
Expand All @@ -89,7 +88,7 @@ func TestDatabase_Worker_Validate(t *testing.T) {
ID: sql.NullInt64{Int64: 1, Valid: true},
Address: sql.NullString{String: "http://localhost:8080", Valid: true},
Active: sql.NullBool{Bool: true, Valid: true},
LastCheckedIn: sql.NullTime{Time: time.Time{}, Valid: true},
LastCheckedIn: sql.NullInt64{Int64: 1563474077, Valid: true},
},
},
{ // no Address set for worker
Expand All @@ -98,7 +97,7 @@ func TestDatabase_Worker_Validate(t *testing.T) {
ID: sql.NullInt64{Int64: 1, Valid: true},
Hostname: sql.NullString{String: "worker_0", Valid: true},
Active: sql.NullBool{Bool: true, Valid: true},
LastCheckedIn: sql.NullTime{Time: time.Time{}, Valid: true},
LastCheckedIn: sql.NullInt64{Int64: 1563474077, Valid: true},
},
},
}
Expand Down Expand Up @@ -130,7 +129,7 @@ func TestDatabase_WorkerFromLibrary(t *testing.T) {
w.SetAddress("http://localhost:8080")
w.SetRoutes([]string{"vela"})
w.SetActive(true)
w.SetLastCheckedIn(time.Time{})
w.SetLastCheckedIn(1563474077)

want := testWorker()

Expand All @@ -151,6 +150,6 @@ func testWorker() *Worker {
Address: sql.NullString{String: "http://localhost:8080", Valid: true},
Routes: []string{"vela"},
Active: sql.NullBool{Bool: true, Valid: true},
LastCheckedIn: sql.NullTime{Time: time.Time{}, Valid: true},
LastCheckedIn: sql.NullInt64{Int64: 1563474077, Valid: true},
}
}
19 changes: 9 additions & 10 deletions library/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,18 @@ package library

import (
"fmt"
"time"
)

// Worker is the library representation of a worker.
//
// swagger:model Worker
type Worker struct {
ID *int64 `json:"id,omitempty"`
Hostname *string `json:"hostname,omitempty"`
Address *string `json:"address,omitempty"`
Routes *[]string `json:"routes,omitempty"`
Active *bool `json:"active,omitempty"`
LastCheckedIn *time.Time `json:"last_checked_in,omitempty"`
ID *int64 `json:"id,omitempty"`
Hostname *string `json:"hostname,omitempty"`
Address *string `json:"address,omitempty"`
Routes *[]string `json:"routes,omitempty"`
Active *bool `json:"active,omitempty"`
LastCheckedIn *int64 `json:"last_checked_in,omitempty"`
}

// GetID returns the ID field.
Expand Down Expand Up @@ -90,10 +89,10 @@ func (w *Worker) GetActive() bool {
//
// When the provided Worker type is nil, or the field within
// the type is nil, it returns the zero value for the field.
func (w *Worker) GetLastCheckedIn() time.Time {
func (w *Worker) GetLastCheckedIn() int64 {
// return zero value if Worker type or LastCheckedIn field is nil
if w == nil || w.LastCheckedIn == nil {
return time.Time{} // 0001-01-01 00:00:00 +0000 UTC
return 0
}

return *w.LastCheckedIn
Expand Down Expand Up @@ -168,7 +167,7 @@ func (w *Worker) SetActive(v bool) {
//
// When the provided Worker type is nil, it
// will set nothing and immediately return.
func (w *Worker) SetLastCheckedIn(v time.Time) {
func (w *Worker) SetLastCheckedIn(v int64) {
// return if Worker type is nil
if w == nil {
return
Expand Down
2 changes: 1 addition & 1 deletion library/worker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func testWorker() *Worker {
w.SetAddress("http://localhost:8080")
w.SetRoutes([]string{"vela"})
w.SetActive(true)
w.SetLastCheckedIn(time.Time{})
w.SetLastCheckedIn(time.Time{}.UTC().Unix())

return w
}

0 comments on commit 24f0a72

Please sign in to comment.