From ed69dde18e155ad44edc9bb0910186fe4fee3898 Mon Sep 17 00:00:00 2001 From: bigeagle Date: Thu, 21 Apr 2016 20:03:07 +0800 Subject: [PATCH] refactor: moved mirrorStatus back to manager --- internal/status.go | 66 ++++++++++++++++++++ internal/status_test.go | 23 +++++++ {internal/status => manager}/status.go | 68 +++------------------ {internal/status => manager}/status_test.go | 10 +-- 4 files changed, 102 insertions(+), 65 deletions(-) create mode 100644 internal/status.go create mode 100644 internal/status_test.go rename {internal/status => manager}/status.go (64%) rename {internal/status => manager}/status_test.go (86%) diff --git a/internal/status.go b/internal/status.go new file mode 100644 index 0000000..8d20a73 --- /dev/null +++ b/internal/status.go @@ -0,0 +1,66 @@ +package internal + +import ( + "encoding/json" + "errors" + "fmt" +) + +type SyncStatus uint8 + +const ( + None SyncStatus = iota + Failed + Success + Syncing + PreSyncing + Paused + Disabled +) + +func (s SyncStatus) MarshalJSON() ([]byte, error) { + var strStatus string + switch s { + case None: + strStatus = "none" + case Failed: + strStatus = "failed" + case Success: + strStatus = "success" + case Syncing: + strStatus = "syncing" + case PreSyncing: + strStatus = "pre-syncing" + case Paused: + strStatus = "paused" + case Disabled: + strStatus = "disabled" + default: + return []byte{}, errors.New("Invalid status value") + } + + return json.Marshal(strStatus) +} + +func (s *SyncStatus) UnmarshalJSON(v []byte) error { + sv := string(v) + switch sv { + case `"none"`: + *s = None + case `"failed"`: + *s = Failed + case `"success"`: + *s = Success + case `"syncing"`: + *s = Syncing + case `"pre-syncing"`: + *s = PreSyncing + case `"paused"`: + *s = Paused + case `"disabled"`: + *s = Disabled + default: + return fmt.Errorf("Invalid status value: %s", string(v)) + } + return nil +} diff --git a/internal/status_test.go b/internal/status_test.go new file mode 100644 index 0000000..fcfafe8 --- /dev/null +++ b/internal/status_test.go @@ -0,0 +1,23 @@ +package internal + +import ( + "encoding/json" + "testing" + + . "github.com/smartystreets/goconvey/convey" +) + +func TestSyncStatus(t *testing.T) { + Convey("SyncStatus json ser-de should work", t, func() { + + b, err := json.Marshal(PreSyncing) + So(err, ShouldBeNil) + So(b, ShouldResemble, []byte(`"pre-syncing"`)) // deep equal should be used + + var s SyncStatus + + err = json.Unmarshal([]byte(`"failed"`), &s) + So(err, ShouldBeNil) + So(s, ShouldEqual, Failed) + }) +} diff --git a/internal/status/status.go b/manager/status.go similarity index 64% rename from internal/status/status.go rename to manager/status.go index dfab49c..c9e2c90 100644 --- a/internal/status/status.go +++ b/manager/status.go @@ -1,35 +1,24 @@ -package status +package manager import ( "encoding/json" "errors" "fmt" "strconv" - "strings" "time" + + . "github.com/tuna/tunasync/internal" ) -type syncStatus uint8 - -const ( - None syncStatus = iota - Failed - Success - Syncing - PreSyncing - Paused - Disabled -) - -type MirrorStatus struct { +type mirrorStatus struct { Name string - Status syncStatus + Status SyncStatus LastUpdate time.Time Upstream string Size string // approximate size } -func (s MirrorStatus) MarshalJSON() ([]byte, error) { +func (s mirrorStatus) MarshalJSON() ([]byte, error) { m := map[string]interface{}{ "name": s.Name, "status": s.Status, @@ -41,7 +30,7 @@ func (s MirrorStatus) MarshalJSON() ([]byte, error) { return json.Marshal(m) } -func (s *MirrorStatus) UnmarshalJSON(v []byte) error { +func (s *mirrorStatus) UnmarshalJSON(v []byte) error { var m map[string]interface{} err := json.Unmarshal(v, &m) @@ -99,46 +88,3 @@ func (s *MirrorStatus) UnmarshalJSON(v []byte) error { } return nil } - -func (s syncStatus) MarshalJSON() ([]byte, error) { - var strStatus string - switch s { - case None: - strStatus = "none" - case Success: - strStatus = "success" - case Syncing: - strStatus = "syncing" - case PreSyncing: - strStatus = "pre-syncing" - case Paused: - strStatus = "paused" - case Disabled: - strStatus = "disabled" - default: - return []byte{}, errors.New("Invalid status value") - } - - return json.Marshal(strStatus) -} - -func (s *syncStatus) UnmarshalJSON(v []byte) error { - sv := strings.Trim(string(v), `"`) - switch sv { - case "none": - *s = None - case "success": - *s = Success - case "syncing": - *s = Syncing - case "pre-syncing": - *s = PreSyncing - case "paused": - *s = Paused - case "disabled": - *s = Disabled - default: - return fmt.Errorf("Invalid status value: %s", string(v)) - } - return nil -} diff --git a/internal/status/status_test.go b/manager/status_test.go similarity index 86% rename from internal/status/status_test.go rename to manager/status_test.go index 578a115..06260d9 100644 --- a/internal/status/status_test.go +++ b/manager/status_test.go @@ -1,10 +1,12 @@ -package status +package manager import ( "encoding/json" "testing" "time" + tunasync "github.com/tuna/tunasync/internal" + . "github.com/smartystreets/goconvey/convey" ) @@ -14,9 +16,9 @@ func TestStatus(t *testing.T) { loc, err := time.LoadLocation(tz) So(err, ShouldBeNil) - m := MirrorStatus{ + m := mirrorStatus{ Name: "tunalinux", - Status: Success, + Status: tunasync.Success, LastUpdate: time.Date(2016, time.April, 16, 23, 8, 10, 0, loc), Size: "5GB", Upstream: "rsync://mirrors.tuna.tsinghua.edu.cn/tunalinux/", @@ -25,7 +27,7 @@ func TestStatus(t *testing.T) { b, err := json.Marshal(m) So(err, ShouldBeNil) // fmt.Println(string(b)) - var m2 MirrorStatus + var m2 mirrorStatus err = json.Unmarshal(b, &m2) So(err, ShouldBeNil) // fmt.Printf("%#v", m2)