Skip to content

Commit

Permalink
use whence constant for Seek() (#746)
Browse files Browse the repository at this point in the history
  • Loading branch information
UponTheSky authored Mar 19, 2024
1 parent 2746e89 commit 22780a6
Show file tree
Hide file tree
Showing 40 changed files with 91 additions and 69 deletions.
5 changes: 3 additions & 2 deletions command-line/v1/file_system_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package poker
import (
"encoding/json"
"fmt"
"io"
"os"
"sort"
)
Expand Down Expand Up @@ -35,7 +36,7 @@ func NewFileSystemPlayerStore(file *os.File) (*FileSystemPlayerStore, error) {
}

func initialisePlayerDBFile(file *os.File) error {
file.Seek(0, 0)
file.Seek(0, io.SeekStart)

info, err := file.Stat()

Expand All @@ -45,7 +46,7 @@ func initialisePlayerDBFile(file *os.File) error {

if info.Size() == 0 {
file.Write([]byte("[]"))
file.Seek(0, 0)
file.Seek(0, io.SeekStart)
}

return nil
Expand Down
3 changes: 2 additions & 1 deletion command-line/v1/tape.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package poker

import (
"io"
"os"
)

Expand All @@ -10,6 +11,6 @@ type tape struct {

func (t *tape) Write(p []byte) (n int, err error) {
t.file.Truncate(0)
t.file.Seek(0, 0)
t.file.Seek(0, io.SeekStart)
return t.file.Write(p)
}
2 changes: 1 addition & 1 deletion command-line/v1/tape_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestTape_Write(t *testing.T) {

tape.Write([]byte("abc"))

file.Seek(0, 0)
file.Seek(0, io.SeekStart)
newFileContents, _ := io.ReadAll(file)

got := string(newFileContents)
Expand Down
5 changes: 3 additions & 2 deletions command-line/v2/file_system_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package poker
import (
"encoding/json"
"fmt"
"io"
"os"
"sort"
)
Expand Down Expand Up @@ -35,7 +36,7 @@ func NewFileSystemPlayerStore(file *os.File) (*FileSystemPlayerStore, error) {
}

func initialisePlayerDBFile(file *os.File) error {
file.Seek(0, 0)
file.Seek(0, io.SeekStart)

info, err := file.Stat()

Expand All @@ -45,7 +46,7 @@ func initialisePlayerDBFile(file *os.File) error {

if info.Size() == 0 {
file.Write([]byte("[]"))
file.Seek(0, 0)
file.Seek(0, io.SeekStart)
}

return nil
Expand Down
3 changes: 2 additions & 1 deletion command-line/v2/tape.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package poker

import (
"io"
"os"
)

Expand All @@ -10,6 +11,6 @@ type tape struct {

func (t *tape) Write(p []byte) (n int, err error) {
t.file.Truncate(0)
t.file.Seek(0, 0)
t.file.Seek(0, io.SeekStart)
return t.file.Write(p)
}
2 changes: 1 addition & 1 deletion command-line/v2/tape_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestTape_Write(t *testing.T) {

tape.Write([]byte("abc"))

file.Seek(0, 0)
file.Seek(0, io.SeekStart)
newFileContents, _ := io.ReadAll(file)

got := string(newFileContents)
Expand Down
5 changes: 3 additions & 2 deletions command-line/v3/file_system_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package poker
import (
"encoding/json"
"fmt"
"io"
"os"
"sort"
)
Expand Down Expand Up @@ -57,7 +58,7 @@ func FileSystemPlayerStoreFromFile(path string) (*FileSystemPlayerStore, func(),
}

func initialisePlayerDBFile(file *os.File) error {
file.Seek(0, 0)
file.Seek(0, io.SeekStart)

info, err := file.Stat()

Expand All @@ -67,7 +68,7 @@ func initialisePlayerDBFile(file *os.File) error {

if info.Size() == 0 {
file.Write([]byte("[]"))
file.Seek(0, 0)
file.Seek(0, io.SeekStart)
}

return nil
Expand Down
3 changes: 2 additions & 1 deletion command-line/v3/tape.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package poker

import (
"io"
"os"
)

Expand All @@ -10,6 +11,6 @@ type tape struct {

func (t *tape) Write(p []byte) (n int, err error) {
t.file.Truncate(0)
t.file.Seek(0, 0)
t.file.Seek(0, io.SeekStart)
return t.file.Write(p)
}
2 changes: 1 addition & 1 deletion command-line/v3/tape_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestTape_Write(t *testing.T) {

tape.Write([]byte("abc"))

file.Seek(0, 0)
file.Seek(0, io.SeekStart)
newFileContents, _ := io.ReadAll(file)

got := string(newFileContents)
Expand Down
32 changes: 16 additions & 16 deletions io.md
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ type FileSystemPlayerStore struct {
}

func (f *FileSystemPlayerStore) GetLeague() []Player {
f.database.Seek(0, 0)
f.database.Seek(0, io.SeekStart)
league, _ := NewLeague(f.database)
return league
}
Expand Down Expand Up @@ -586,7 +586,7 @@ func (f *FileSystemPlayerStore) RecordWin(name string) {
}
}

f.database.Seek(0, 0)
f.database.Seek(0, io.SeekStart)
json.NewEncoder(f.database).Encode(league)
}
```
Expand Down Expand Up @@ -646,7 +646,7 @@ func (f *FileSystemPlayerStore) RecordWin(name string) {
player.Wins++
}

f.database.Seek(0, 0)
f.database.Seek(0, io.SeekStart)
json.NewEncoder(f.database).Encode(league)
}
```
Expand Down Expand Up @@ -699,7 +699,7 @@ func (f *FileSystemPlayerStore) RecordWin(name string) {
league = append(league, Player{name, 1})
}

f.database.Seek(0, 0)
f.database.Seek(0, io.SeekStart)
json.NewEncoder(f.database).Encode(league)
}
```
Expand Down Expand Up @@ -765,7 +765,7 @@ type FileSystemPlayerStore struct {
}

func NewFileSystemPlayerStore(database io.ReadWriteSeeker) *FileSystemPlayerStore {
database.Seek(0, 0)
database.Seek(0, io.SeekStart)
league, _ := NewLeague(database)
return &FileSystemPlayerStore{
database: database,
Expand Down Expand Up @@ -802,7 +802,7 @@ func (f *FileSystemPlayerStore) RecordWin(name string) {
f.league = append(f.league, Player{name, 1})
}

f.database.Seek(0, 0)
f.database.Seek(0, io.SeekStart)
json.NewEncoder(f.database).Encode(f.league)
}
```
Expand Down Expand Up @@ -832,7 +832,7 @@ type tape struct {
}

func (t *tape) Write(p []byte) (n int, err error) {
t.file.Seek(0, 0)
t.file.Seek(0, io.SeekStart)
return t.file.Write(p)
}
```
Expand All @@ -852,7 +852,7 @@ Update the constructor to use `Tape`
```go
//file_system_store.go
func NewFileSystemPlayerStore(database io.ReadWriteSeeker) *FileSystemPlayerStore {
database.Seek(0, 0)
database.Seek(0, io.SeekStart)
league, _ := NewLeague(database)

return &FileSystemPlayerStore{
Expand Down Expand Up @@ -880,7 +880,7 @@ func TestTape_Write(t *testing.T) {

tape.Write([]byte("abc"))

file.Seek(0, 0)
file.Seek(0, io.SeekStart)
newFileContents, _ := io.ReadAll(file)

got := string(newFileContents)
Expand Down Expand Up @@ -916,7 +916,7 @@ type tape struct {

func (t *tape) Write(p []byte) (n int, err error) {
t.file.Truncate(0)
t.file.Seek(0, 0)
t.file.Seek(0, io.SeekStart)
return t.file.Write(p)
}
```
Expand All @@ -941,7 +941,7 @@ type FileSystemPlayerStore struct {
}

func NewFileSystemPlayerStore(file *os.File) *FileSystemPlayerStore {
file.Seek(0, 0)
file.Seek(0, io.SeekStart)
league, _ := NewLeague(file)

return &FileSystemPlayerStore{
Expand Down Expand Up @@ -1011,7 +1011,7 @@ Let's make it so our constructor is capable of returning an error.
```go
//file_system_store.go
func NewFileSystemPlayerStore(file *os.File) (*FileSystemPlayerStore, error) {
file.Seek(0, 0)
file.Seek(0, io.SeekStart)
league, err := NewLeague(file)

if err != nil {
Expand Down Expand Up @@ -1121,7 +1121,7 @@ Change our constructor to the following
//file_system_store.go
func NewFileSystemPlayerStore(file *os.File) (*FileSystemPlayerStore, error) {

file.Seek(0, 0)
file.Seek(0, io.SeekStart)

info, err := file.Stat()

Expand All @@ -1131,7 +1131,7 @@ func NewFileSystemPlayerStore(file *os.File) (*FileSystemPlayerStore, error) {

if info.Size() == 0 {
file.Write([]byte("[]"))
file.Seek(0, 0)
file.Seek(0, io.SeekStart)
}

league, err := NewLeague(file)
Expand All @@ -1156,7 +1156,7 @@ Our constructor is a bit messy now, so let's extract the initialise code into a
```go
//file_system_store.go
func initialisePlayerDBFile(file *os.File) error {
file.Seek(0, 0)
file.Seek(0, io.SeekStart)

info, err := file.Stat()

Expand All @@ -1166,7 +1166,7 @@ func initialisePlayerDBFile(file *os.File) error {

if info.Size() == 0 {
file.Write([]byte("[]"))
file.Seek(0, 0)
file.Seek(0, io.SeekStart)
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion io/v1/file_system_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type FileSystemPlayerStore struct {

// GetLeague returns the scores of all the players.
func (f *FileSystemPlayerStore) GetLeague() []Player {
f.database.Seek(0, 0)
f.database.Seek(0, io.SeekStart)
league, _ := NewLeague(f.database)
return league
}
2 changes: 1 addition & 1 deletion io/v2/file_system_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type FileSystemPlayerStore struct {

// GetLeague returns the scores of all the players.
func (f *FileSystemPlayerStore) GetLeague() []Player {
f.database.Seek(0, 0)
f.database.Seek(0, io.SeekStart)
league, _ := NewLeague(f.database)
return league
}
Expand Down
4 changes: 2 additions & 2 deletions io/v3/file_system_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type FileSystemPlayerStore struct {

// GetLeague returns the scores of all the players.
func (f *FileSystemPlayerStore) GetLeague() League {
f.database.Seek(0, 0)
f.database.Seek(0, io.SeekStart)
league, _ := NewLeague(f.database)
return league
}
Expand All @@ -38,6 +38,6 @@ func (f *FileSystemPlayerStore) RecordWin(name string) {
player.Wins++
}

f.database.Seek(0, 0)
f.database.Seek(0, io.SeekStart)
json.NewEncoder(f.database).Encode(league)
}
4 changes: 2 additions & 2 deletions io/v4/file_system_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type FileSystemPlayerStore struct {

// GetLeague returns the scores of all the players.
func (f *FileSystemPlayerStore) GetLeague() League {
f.database.Seek(0, 0)
f.database.Seek(0, io.SeekStart)
league, _ := NewLeague(f.database)
return league
}
Expand Down Expand Up @@ -40,6 +40,6 @@ func (f *FileSystemPlayerStore) RecordWin(name string) {
league = append(league, Player{name, 1})
}

f.database.Seek(0, 0)
f.database.Seek(0, io.SeekStart)
json.NewEncoder(f.database).Encode(league)
}
4 changes: 2 additions & 2 deletions io/v5/file_system_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type FileSystemPlayerStore struct {

// GetLeague returns the scores of all the players.
func (f *FileSystemPlayerStore) GetLeague() League {
f.database.Seek(0, 0)
f.database.Seek(0, io.SeekStart)
league, _ := NewLeague(f.database)
return league
}
Expand Down Expand Up @@ -40,6 +40,6 @@ func (f *FileSystemPlayerStore) RecordWin(name string) {
league = append(league, Player{name, 1})
}

f.database.Seek(0, 0)
f.database.Seek(0, io.SeekStart)
json.NewEncoder(f.database).Encode(league)
}
4 changes: 2 additions & 2 deletions io/v6/file_system_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type FileSystemPlayerStore struct {

// NewFileSystemPlayerStore creates a FileSystemPlayerStore.
func NewFileSystemPlayerStore(database io.ReadWriteSeeker) *FileSystemPlayerStore {
database.Seek(0, 0)
database.Seek(0, io.SeekStart)
league, _ := NewLeague(database)

return &FileSystemPlayerStore{
Expand Down Expand Up @@ -49,6 +49,6 @@ func (f *FileSystemPlayerStore) RecordWin(name string) {
f.league = append(f.league, Player{name, 1})
}

f.database.Seek(0, 0)
f.database.Seek(0, io.SeekStart)
json.NewEncoder(f.database).Encode(f.league)
}
3 changes: 2 additions & 1 deletion io/v7/file_system_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"encoding/json"
"io"
"os"
)

Expand All @@ -13,7 +14,7 @@ type FileSystemPlayerStore struct {

// NewFileSystemPlayerStore creates a FileSystemPlayerStore.
func NewFileSystemPlayerStore(file *os.File) *FileSystemPlayerStore {
file.Seek(0, 0)
file.Seek(0, io.SeekStart)
league, _ := NewLeague(file)

return &FileSystemPlayerStore{
Expand Down
Loading

0 comments on commit 22780a6

Please sign in to comment.