Skip to content

Commit 13cdbfd

Browse files
committed
Refactor to use generics
1 parent 567cc78 commit 13cdbfd

File tree

5 files changed

+85
-69
lines changed

5 files changed

+85
-69
lines changed

internal/data/data.go

Lines changed: 5 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,28 +9,14 @@ import (
99
"path/filepath"
1010
"strings"
1111

12-
"github.com/goccy/go-yaml"
1312
"github.com/spf13/viper"
1413

1514
"github.com/DanWlker/remind/internal/config"
15+
"github.com/DanWlker/remind/internal/shared"
1616
)
1717

1818
type EditTextFunc func(todo string, index int) (string, error)
1919

20-
func FGetTodoFromReader(r io.Reader) ([]TodoEntity, error) {
21-
var items []TodoEntity
22-
dec := yaml.NewDecoder(r)
23-
24-
err := dec.Decode(&items)
25-
if errors.Is(err, io.EOF) {
26-
return nil, nil
27-
} else if err != nil {
28-
return nil, fmt.Errorf("dec.Decode: %w", err)
29-
}
30-
31-
return items, nil
32-
}
33-
3420
// This does not create the file if it doesn't exist
3521
func GetTodoFromFile(fileFullPath string) (items []TodoEntity, err error) {
3622
f, err := os.Open(fileFullPath)
@@ -43,22 +29,13 @@ func GetTodoFromFile(fileFullPath string) (items []TodoEntity, err error) {
4329
}
4430
}()
4531

46-
items, err = FGetTodoFromReader(f)
32+
items, err = shared.FGetStructFromYaml[TodoEntity](f)
4733
if err != nil {
48-
return nil, fmt.Errorf("FGetTodoFromReader: %w", err)
34+
return nil, fmt.Errorf("FGetStructFromYaml: %w", err)
4935
}
5036
return items, nil
5137
}
5238

53-
func FWriteTodoToFile(w io.Writer, todoList []TodoEntity) error {
54-
enc := yaml.NewEncoder(w)
55-
if err := enc.Encode(todoList); err != nil {
56-
return fmt.Errorf("enc.Encode: %w", err)
57-
}
58-
59-
return nil
60-
}
61-
6239
func WriteTodoToFile(fileFullPath string, todoList []TodoEntity) (err error) {
6340
// Opens with
6441
// Write - WRONLY
@@ -77,8 +54,8 @@ func WriteTodoToFile(fileFullPath string, todoList []TodoEntity) (err error) {
7754
}
7855
}()
7956

80-
if err = FWriteTodoToFile(f, todoList); err != nil {
81-
return fmt.Errorf("FWriteTodoToFile: %w", err)
57+
if err = shared.FWriteStructToYaml(f, todoList); err != nil {
58+
return fmt.Errorf("FWriteStructToYaml: %w", err)
8259
}
8360

8461
return nil

internal/data/data_test.go

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,43 +4,9 @@ import (
44
"bytes"
55
"fmt"
66
"strconv"
7-
"strings"
87
"testing"
98
)
109

11-
func TestFGetTodoFromReader(t *testing.T) {
12-
t.Run("Test EOF returns without error", func(t *testing.T) {
13-
reader := strings.NewReader("")
14-
15-
res, err := FGetTodoFromReader(reader)
16-
if err != nil {
17-
t.Errorf("Expected no error, got %v", err)
18-
}
19-
20-
if len(res) != 0 {
21-
t.Errorf("Expected empty array, got %v", res)
22-
}
23-
})
24-
}
25-
26-
func TestFWriteTodoToFile(t *testing.T) {
27-
t.Run("Test nil array encodes without error", func(t *testing.T) {
28-
ex := "[]\n"
29-
30-
var b bytes.Buffer
31-
32-
err := FWriteTodoToFile(&b, nil)
33-
if err != nil {
34-
t.Errorf("Expected no error, got %v", err)
35-
}
36-
37-
got := b.String()
38-
if got != ex {
39-
t.Errorf("Expected %v, got %v", strconv.Quote(ex), strconv.Quote(got))
40-
}
41-
})
42-
}
43-
4410
func TestFPrettyPrintFile(t *testing.T) {
4511
t.Run("Functionality test", func(t *testing.T) {
4612
todoList := []TodoEntity{

internal/record/record.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package record
33
import (
44
"errors"
55
"fmt"
6-
"io"
76
"os"
87
"path/filepath"
98

@@ -64,12 +63,9 @@ func GetFileContents() (items []RecordEntity, err error) {
6463
}
6564
}()
6665

67-
dec := yaml.NewDecoder(f)
68-
err = dec.Decode(&items)
69-
if errors.Is(err, io.EOF) {
70-
return nil, nil
71-
} else if err != nil {
72-
return nil, fmt.Errorf("dec.Decode: %w", err)
66+
items, err = shared.FGetStructFromYaml[RecordEntity](f)
67+
if err != nil {
68+
return nil, fmt.Errorf("FGetStructFromYaml: %w", err)
7369
}
7470

7571
return items, nil

internal/shared/file_helpers.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package shared
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"io"
7+
8+
"github.com/goccy/go-yaml"
9+
)
10+
11+
func FGetStructFromYaml[T any](r io.Reader) ([]T, error) {
12+
var items []T
13+
dec := yaml.NewDecoder(r)
14+
15+
err := dec.Decode(&items)
16+
if errors.Is(err, io.EOF) {
17+
return nil, nil
18+
} else if err != nil {
19+
return nil, fmt.Errorf("dec.Decode: %w", err)
20+
}
21+
22+
return items, nil
23+
}
24+
25+
func FWriteStructToYaml[T any](w io.Writer, todoList []T) error {
26+
enc := yaml.NewEncoder(w)
27+
if err := enc.Encode(todoList); err != nil {
28+
return fmt.Errorf("enc.Encode: %w", err)
29+
}
30+
31+
return nil
32+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package shared
2+
3+
import (
4+
"bytes"
5+
"strconv"
6+
"strings"
7+
"testing"
8+
)
9+
10+
type SomeYamlStruct struct {
11+
Text string
12+
}
13+
14+
func TestFGetStructFromReader(t *testing.T) {
15+
t.Run("Test EOF returns without error", func(t *testing.T) {
16+
reader := strings.NewReader("")
17+
18+
res, err := FGetStructFromYaml[SomeYamlStruct](reader)
19+
if err != nil {
20+
t.Errorf("Expected no error, got %v", err)
21+
}
22+
23+
if len(res) != 0 {
24+
t.Errorf("Expected empty array, got %v", res)
25+
}
26+
})
27+
}
28+
29+
func TestFWriteStructToFile(t *testing.T) {
30+
t.Run("Test nil array encodes without error", func(t *testing.T) {
31+
ex := "[]\n"
32+
33+
var b bytes.Buffer
34+
35+
err := FWriteStructToYaml[SomeYamlStruct](&b, nil)
36+
if err != nil {
37+
t.Errorf("Expected no error, got %v", err)
38+
}
39+
40+
got := b.String()
41+
if got != ex {
42+
t.Errorf("Expected %v, got %v", strconv.Quote(ex), strconv.Quote(got))
43+
}
44+
})
45+
}

0 commit comments

Comments
 (0)