Skip to content

Commit 3ace9f7

Browse files
authored
Add write file (#4)
1 parent deb2af9 commit 3ace9f7

File tree

5 files changed

+77
-12
lines changed

5 files changed

+77
-12
lines changed

.github/workflows/test.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111
- name: Set up Go
1212
uses: actions/setup-go@v1
1313
with:
14-
go-version: 1.16
14+
go-version: 1.19
1515
- name: Check out code
1616
uses: actions/checkout@v2
1717
- name: Install dependencies

go.mod

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
module github.com/kelindar/tile
22

3-
go 1.17
3+
go 1.19
44

55
require (
6-
github.com/kelindar/iostream v1.2.0
7-
github.com/stretchr/testify v1.7.0
6+
github.com/kelindar/iostream v1.3.0
7+
github.com/stretchr/testify v1.8.1
88
)
99

1010
require (
11-
github.com/davecgh/go-spew v1.1.0 // indirect
11+
github.com/davecgh/go-spew v1.1.1 // indirect
1212
github.com/pmezard/go-difflib v1.0.0 // indirect
13-
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
13+
gopkg.in/yaml.v3 v3.0.1 // indirect
1414
)

go.sum

+12-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
1-
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
21
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3-
github.com/kelindar/iostream v1.2.0 h1:UaFLsj/0quGMZxI/rpHmI8u7vNKY9xSyw4mQ9YCTPWA=
4-
github.com/kelindar/iostream v1.2.0/go.mod h1:MkjMuVb6zGdPQVdwLnFRO0xOTOdDvBWTztFmjRDQkXk=
2+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
3+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4+
github.com/kelindar/iostream v1.3.0 h1:Bz2qQabipZlF1XCk64bnxsGLete+iHtayGPeWVpbwbo=
5+
github.com/kelindar/iostream v1.3.0/go.mod h1:MkjMuVb6zGdPQVdwLnFRO0xOTOdDvBWTztFmjRDQkXk=
56
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
67
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
78
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
8-
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
9-
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
9+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
10+
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
11+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
12+
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
13+
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
14+
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
1015
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
1116
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
12-
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
1317
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
18+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
19+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

store.go

+42
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,18 @@
44
package tile
55

66
import (
7+
"compress/flate"
78
"encoding/binary"
89
"io"
10+
"os"
911

1012
"github.com/kelindar/iostream"
1113
)
1214

1315
const tileDataSize = 54
1416

17+
// ---------------------------------- Stream ----------------------------------
18+
1519
// WriteTo writes the grid to a specific writer.
1620
func (m *Grid) WriteTo(dst io.Writer) (n int64, err error) {
1721
p1 := At(0, 0)
@@ -64,3 +68,41 @@ func ReadFrom(src io.Reader) (grid *Grid, err error) {
6468
})
6569
return
6670
}
71+
72+
// ---------------------------------- File ----------------------------------
73+
74+
// WriteFile writes the grid into a flate-compressed binary file.
75+
func (m *Grid) WriteFile(filename string) error {
76+
file, err := os.Create(filename)
77+
if err != nil {
78+
return err
79+
}
80+
81+
defer file.Close()
82+
writer, err := flate.NewWriter(file, flate.BestSpeed)
83+
if err != nil {
84+
return err
85+
}
86+
87+
// WriteTo the underlying writer
88+
defer writer.Close()
89+
_, err = m.WriteTo(writer)
90+
return err
91+
}
92+
93+
// Restore restores the grid from the specified file. The grid must
94+
// be written using the corresponding WriteFile() method.
95+
func ReadFile(filename string) (grid *Grid, err error) {
96+
if _, err := os.Stat(filename); os.IsNotExist(err) {
97+
return nil, os.ErrNotExist
98+
}
99+
100+
// Otherwise, attempt to open the file and restore
101+
file, err := os.Open(filename)
102+
if err != nil {
103+
return nil, err
104+
}
105+
106+
defer file.Close()
107+
return ReadFrom(flate.NewReader(file))
108+
}

store_test.go

+17
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package tile
66
import (
77
"bytes"
88
"compress/flate"
9+
"os"
910
"testing"
1011

1112
"github.com/stretchr/testify/assert"
@@ -78,3 +79,19 @@ func TestSaveLoadFlate(t *testing.T) {
7879
assert.NoError(t, err)
7980
assert.Equal(t, m.pages, out.pages)
8081
}
82+
83+
func TestSaveLoadFile(t *testing.T) {
84+
temp, err := os.CreateTemp("", "*")
85+
assert.NoError(t, err)
86+
defer os.Remove(temp.Name())
87+
88+
println(temp.Name())
89+
// Write a test map into temp file
90+
m := mapFrom("300x300.png")
91+
assert.NoError(t, m.WriteFile(temp.Name()))
92+
93+
// Read the map back
94+
out, err := ReadFile(temp.Name())
95+
assert.NoError(t, err)
96+
assert.Equal(t, m.pages, out.pages)
97+
}

0 commit comments

Comments
 (0)