Skip to content

Commit 38147ec

Browse files
committed
Add implementation of interfaces project
1 parent 734b982 commit 38147ec

File tree

6 files changed

+198
-0
lines changed

6 files changed

+198
-0
lines changed

projects/interfaces/buffer.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package main
2+
3+
type OurByteBuffer struct {
4+
bytes []byte
5+
readPosition int
6+
}
7+
8+
func NewBufferString(s string) OurByteBuffer {
9+
return OurByteBuffer{
10+
bytes: []byte(s),
11+
readPosition: 0,
12+
}
13+
}
14+
15+
func (b *OurByteBuffer) Bytes() []byte {
16+
return b.bytes
17+
}
18+
19+
func (b *OurByteBuffer) Write(bytes []byte) (int, error) {
20+
b.bytes = append(b.bytes, bytes...)
21+
return len(bytes), nil
22+
}
23+
24+
func (b *OurByteBuffer) Read(to []byte) (int, error) {
25+
remainingBytes := len(b.bytes) - b.readPosition
26+
bytesToRead := len(to)
27+
if remainingBytes < bytesToRead {
28+
bytesToRead = remainingBytes
29+
}
30+
copy(to, b.bytes[b.readPosition:b.readPosition+bytesToRead])
31+
b.readPosition += bytesToRead
32+
return bytesToRead, nil
33+
}

projects/interfaces/buffer_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package main
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/require"
7+
)
8+
9+
func TestInitialBytesAreRead(t *testing.T) {
10+
want := []byte("hello")
11+
12+
b := NewBufferString("hello")
13+
14+
got := b.Bytes()
15+
16+
require.Equal(t, want, got)
17+
}
18+
19+
func TestSubsequentWritesAreAppended(t *testing.T) {
20+
want := []byte("hello world")
21+
22+
b := NewBufferString("hello")
23+
24+
_, err := b.Write([]byte(" world"))
25+
require.NoError(t, err)
26+
27+
got := b.Bytes()
28+
29+
require.Equal(t, want, got)
30+
}
31+
32+
func TestReadWithSliceBigEnoughForWholeBuffer(t *testing.T) {
33+
b := NewBufferString("hello world")
34+
35+
slice := make([]byte, 50)
36+
37+
n, err := b.Read(slice)
38+
require.NoError(t, err)
39+
require.Equal(t, 11, n)
40+
require.Equal(t, []byte("hello world"), slice[:n])
41+
}
42+
43+
func TestReadWithSliceSmallerThanWholeBuffer(t *testing.T) {
44+
b := NewBufferString("hello world")
45+
46+
slice := make([]byte, 6)
47+
48+
n, err := b.Read(slice)
49+
require.NoError(t, err)
50+
require.Equal(t, 6, n)
51+
require.Equal(t, []byte("hello "), slice)
52+
53+
n, err = b.Read(slice)
54+
require.NoError(t, err)
55+
require.Equal(t, 5, n)
56+
require.Equal(t, []byte("world"), slice[:n])
57+
}

projects/interfaces/filtering_pipe.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package main
2+
3+
import (
4+
"io"
5+
)
6+
7+
type FilteringPipe struct {
8+
writer io.Writer
9+
}
10+
11+
func NewFilteringPipe(writer io.Writer) FilteringPipe {
12+
return FilteringPipe{
13+
writer: writer,
14+
}
15+
}
16+
17+
func (fp *FilteringPipe) Write(bytes []byte) (int, error) {
18+
for i := range bytes {
19+
if bytes[i] < '0' || bytes[i] > '9' {
20+
if _, err := fp.writer.Write(bytes[i : i+1]); err != nil {
21+
return i, err
22+
}
23+
}
24+
}
25+
// We return len(bytes) because io.Writer is documented to return how many bytes were processed, not how many were actually used.
26+
return len(bytes), nil
27+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"testing"
6+
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
func TestWriteNoNumbers(t *testing.T) {
11+
buf := bytes.NewBufferString("")
12+
13+
fp := NewFilteringPipe(buf)
14+
15+
n, err := fp.Write([]byte("hello"))
16+
require.NoError(t, err)
17+
require.Equal(t, 5, n)
18+
require.Equal(t, "hello", buf.String())
19+
}
20+
21+
func TestWriteJustNumbers(t *testing.T) {
22+
buf := bytes.NewBufferString("")
23+
24+
fp := NewFilteringPipe(buf)
25+
26+
n, err := fp.Write([]byte("123"))
27+
require.NoError(t, err)
28+
require.Equal(t, 3, n)
29+
require.Equal(t, "", buf.String())
30+
}
31+
32+
func TestMultipleWrites(t *testing.T) {
33+
buf := bytes.NewBufferString("")
34+
35+
fp := NewFilteringPipe(buf)
36+
37+
n, err := fp.Write([]byte("start="))
38+
require.NoError(t, err)
39+
require.Equal(t, 6, n)
40+
n, err = fp.Write([]byte("1, end=10"))
41+
require.NoError(t, err)
42+
require.Equal(t, 9, n)
43+
require.Equal(t, "start=, end=", buf.String())
44+
}
45+
46+
func TestWriteMixedNumbersAndLetters(t *testing.T) {
47+
buf := bytes.NewBufferString("")
48+
49+
fp := NewFilteringPipe(buf)
50+
51+
n, err := fp.Write([]byte("start=1, end=10"))
52+
require.NoError(t, err)
53+
require.Equal(t, 15, n)
54+
require.Equal(t, "start=, end=", buf.String())
55+
}

projects/interfaces/go.mod

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module github.com/CodeYourFuture/immersive-go-course/projects/interfaces
2+
3+
go 1.19
4+
5+
require (
6+
github.com/davecgh/go-spew v1.1.1 // indirect
7+
github.com/pmezard/go-difflib v1.0.0 // indirect
8+
github.com/stretchr/testify v1.8.2 // indirect
9+
gopkg.in/yaml.v3 v3.0.1 // indirect
10+
)

projects/interfaces/go.sum

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
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/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
5+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
6+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
7+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
8+
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
9+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
10+
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
11+
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
12+
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
13+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
14+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
15+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
16+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 commit comments

Comments
 (0)