refactor: moved mirrorStatus back to manager

This commit is contained in:
bigeagle 2016-04-21 20:03:07 +08:00
parent 96f38363ea
commit ed69dde18e
No known key found for this signature in database
GPG Key ID: 9171A4571C27920A
4 changed files with 102 additions and 65 deletions

66
internal/status.go Normal file
View File

@ -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
}

23
internal/status_test.go Normal file
View File

@ -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)
})
}

View File

@ -1,35 +1,24 @@
package status package manager
import ( import (
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
"strconv" "strconv"
"strings"
"time" "time"
. "github.com/tuna/tunasync/internal"
) )
type syncStatus uint8 type mirrorStatus struct {
const (
None syncStatus = iota
Failed
Success
Syncing
PreSyncing
Paused
Disabled
)
type MirrorStatus struct {
Name string Name string
Status syncStatus Status SyncStatus
LastUpdate time.Time LastUpdate time.Time
Upstream string Upstream string
Size string // approximate size Size string // approximate size
} }
func (s MirrorStatus) MarshalJSON() ([]byte, error) { func (s mirrorStatus) MarshalJSON() ([]byte, error) {
m := map[string]interface{}{ m := map[string]interface{}{
"name": s.Name, "name": s.Name,
"status": s.Status, "status": s.Status,
@ -41,7 +30,7 @@ func (s MirrorStatus) MarshalJSON() ([]byte, error) {
return json.Marshal(m) return json.Marshal(m)
} }
func (s *MirrorStatus) UnmarshalJSON(v []byte) error { func (s *mirrorStatus) UnmarshalJSON(v []byte) error {
var m map[string]interface{} var m map[string]interface{}
err := json.Unmarshal(v, &m) err := json.Unmarshal(v, &m)
@ -99,46 +88,3 @@ func (s *MirrorStatus) UnmarshalJSON(v []byte) error {
} }
return nil 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
}

View File

@ -1,10 +1,12 @@
package status package manager
import ( import (
"encoding/json" "encoding/json"
"testing" "testing"
"time" "time"
tunasync "github.com/tuna/tunasync/internal"
. "github.com/smartystreets/goconvey/convey" . "github.com/smartystreets/goconvey/convey"
) )
@ -14,9 +16,9 @@ func TestStatus(t *testing.T) {
loc, err := time.LoadLocation(tz) loc, err := time.LoadLocation(tz)
So(err, ShouldBeNil) So(err, ShouldBeNil)
m := MirrorStatus{ m := mirrorStatus{
Name: "tunalinux", Name: "tunalinux",
Status: Success, Status: tunasync.Success,
LastUpdate: time.Date(2016, time.April, 16, 23, 8, 10, 0, loc), LastUpdate: time.Date(2016, time.April, 16, 23, 8, 10, 0, loc),
Size: "5GB", Size: "5GB",
Upstream: "rsync://mirrors.tuna.tsinghua.edu.cn/tunalinux/", Upstream: "rsync://mirrors.tuna.tsinghua.edu.cn/tunalinux/",
@ -25,7 +27,7 @@ func TestStatus(t *testing.T) {
b, err := json.Marshal(m) b, err := json.Marshal(m)
So(err, ShouldBeNil) So(err, ShouldBeNil)
// fmt.Println(string(b)) // fmt.Println(string(b))
var m2 MirrorStatus var m2 mirrorStatus
err = json.Unmarshal(b, &m2) err = json.Unmarshal(b, &m2)
So(err, ShouldBeNil) So(err, ShouldBeNil)
// fmt.Printf("%#v", m2) // fmt.Printf("%#v", m2)