Skip to content

Commit

Permalink
go fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
mattn committed Feb 19, 2014
1 parent 260f756 commit 0654797
Show file tree
Hide file tree
Showing 9 changed files with 265 additions and 262 deletions.
46 changes: 23 additions & 23 deletions history.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ package main
import (
"errors"
"path/filepath"
"time"
"time"
)

type HistoryItem struct {
Source string
Dest string
Source string
Dest string
Timestamp int64
Id string
Id string
}

func normalizePath(p string) string {
Expand All @@ -28,58 +28,58 @@ func clearHistory() error {

func allHistoryItems() ([]HistoryItem, error) {
var output []HistoryItem

rows, err := profileDb_.Query("SELECT id, source, destination, timestamp FROM history ORDER BY id")
if err != nil {
return output, err
}

for rows.Next() {
var item HistoryItem
rows.Scan(&item.Id, &item.Source, &item.Dest, &item.Timestamp)
output = append(output, item)
}

return output, nil
}

func saveHistoryItems(sources []string, destinations []string) error {
if len(sources) != len(destinations) {
return errors.New("Number of sources and destinations do not match.")
}

if len(sources) == 0 {
return nil
}

tx, err := profileDb_.Begin()
if err != nil {
return err
}

for i, source := range sources {
dest := destinations[i]
tx.Exec("INSERT INTO history (source, destination, timestamp) VALUES (?, ?, ?)", normalizePath(source), normalizePath(dest), time.Now().Unix())
tx.Exec("INSERT INTO history (source, destination, timestamp) VALUES (?, ?, ?)", normalizePath(source), normalizePath(dest), time.Now().Unix())
}

return tx.Commit()
}

func deleteHistoryItems(items []HistoryItem) error {
if len(items) == 0 {
return nil
}

sqlOr := ""
for _, item := range items {
if sqlOr != "" {
sqlOr += " OR "
}
sqlOr += "id = " + item.Id
}

_, err := profileDb_.Exec("DELETE FROM history WHERE " + sqlOr)

return err
}

Expand All @@ -89,13 +89,13 @@ func deleteOldHistoryItems(minTimestamp int64) {
}
}

func latestHistoryItemsByDestinations(paths []string) ([]HistoryItem, error) {
func latestHistoryItemsByDestinations(paths []string) ([]HistoryItem, error) {
var output []HistoryItem
if len(paths) == 0 {
return output, nil
}
sqlOr := ""

sqlOr := ""
var sqlArgs []interface{}
for _, p := range paths {
sqlArgs = append(sqlArgs, p)
Expand All @@ -104,12 +104,12 @@ func latestHistoryItemsByDestinations(paths []string) ([]HistoryItem, error) {
}
sqlOr += "destination = ?"
}
rows, err := profileDb_.Query("SELECT id, source, destination, timestamp FROM history WHERE " + sqlOr + " ORDER BY timestamp DESC", sqlArgs...)

rows, err := profileDb_.Query("SELECT id, source, destination, timestamp FROM history WHERE "+sqlOr+" ORDER BY timestamp DESC", sqlArgs...)
if err != nil {
return output, err
}

doneDestinations := make(map[string]bool)
for rows.Next() {
var item HistoryItem
Expand All @@ -121,6 +121,6 @@ func latestHistoryItemsByDestinations(paths []string) ([]HistoryItem, error) {
output = append(output, item)
doneDestinations[item.Dest] = true
}

return output, nil
}
}
40 changes: 20 additions & 20 deletions history_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,43 +8,43 @@ import (
func Test_saveHistoryItems(t *testing.T) {
setup(t)
defer teardown(t)

err := saveHistoryItems([]string{"one", "two"}, []string{"one"})
if err == nil {
t.Error("Expected error, got nil")
}

err = saveHistoryItems([]string{}, []string{})
if err != nil {
t.Errorf("Expected no error, got %s", err)
}

items, _ := allHistoryItems()
if len(items) > 0 {
t.Errorf("Expected no items, got %d", len(items))
}
saveHistoryItems([]string{"one","two"}, []string{"1","2"})

saveHistoryItems([]string{"one", "two"}, []string{"1", "2"})

items, _ = allHistoryItems()
if len(items) != 2 {
t.Errorf("Expected 2 items, got %d", len(items))
}

for _, item := range items {
if (filepath.Base(item.Source) == "one" && filepath.Base(item.Dest) != "1") || (filepath.Base(item.Source) == "two" && filepath.Base(item.Dest) != "2") {
t.Error("Source and destination do not match.")
}
}

saveHistoryItems([]string{"three"}, []string{"3"})
items, _ = allHistoryItems()
if len(items) != 3 {
t.Errorf("Expected 3 items, got %d", len(items))
}

profileDb_.Close()

err = saveHistoryItems([]string{"un"}, []string{"dest"})
if err == nil {
t.Error("Expected error, got nil")
Expand All @@ -55,11 +55,11 @@ func Test_deleteHistoryItems(t *testing.T) {
setup(t)
defer teardown(t)

saveHistoryItems([]string{"one","two","three"}, []string{"1","2","3"})
saveHistoryItems([]string{"one", "two", "three"}, []string{"1", "2", "3"})

items, _ := allHistoryItems()
deleteHistoryItems([]HistoryItem{items[0], items[1]})

items, _ = allHistoryItems()
if len(items) != 1 {
t.Errorf("Expected 1 item, got %d", len(items))
Expand All @@ -76,10 +76,10 @@ func Test_deleteOldHistoryItems(t *testing.T) {

now := 1000
for i := 0; i < 5; i++ {
profileDb_.Exec("INSERT INTO history (source, destination, timestamp) VALUES (?, ?, ?)", "a", "b", now + i)
profileDb_.Exec("INSERT INTO history (source, destination, timestamp) VALUES (?, ?, ?)", "a", "b", now+i)
}
deleteOldHistoryItems(int64(now + 2))

items, _ := allHistoryItems()
if len(items) != 3 {
t.Errorf("Expected 3 items, got %d", len(items))
Expand All @@ -92,18 +92,18 @@ func Test_latestHistoryItemsByDestinations(t *testing.T) {

now := 1000
for i := 0; i < 5; i++ {
profileDb_.Exec("INSERT INTO history (source, destination, timestamp) VALUES (?, ?, ?)", "a", "b", now + i)
profileDb_.Exec("INSERT INTO history (source, destination, timestamp) VALUES (?, ?, ?)", "a", "b", now+i)
}

items, _ := allHistoryItems()
dest := items[0].Dest

items, _ = latestHistoryItemsByDestinations([]string{dest})
if len(items) != 1 {
t.Errorf("Expected 1 item, got %d", len(items))
} else {
if items[0].Timestamp != 1004 {
t.Error("Did not get the right item")
t.Error("Did not get the right item")
}
}
}
}
4 changes: 2 additions & 2 deletions log.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ func log(level int, s string, a ...interface{}) {
if level < minLogLevel_ {
return
}
fmt.Printf(APPNAME + ": " + s + "\n", a...)
fmt.Printf(APPNAME+": "+s+"\n", a...)
}

func logDebug(s string, a ...interface{}) {
Expand All @@ -23,4 +23,4 @@ func logInfo(s string, a ...interface{}) {

func logError(s string, a ...interface{}) {
log(3, s, a...)
}
}
Loading

0 comments on commit 0654797

Please sign in to comment.