From 4534dffb865eb1a50bfbc291a5c3798183081caf Mon Sep 17 00:00:00 2001 From: "Gabriel A. Giovanini" Date: Sun, 26 May 2024 20:33:37 +0200 Subject: feat: Add actual git listing implementation --- pkg/u/file.go | 21 ++++++++++++ pkg/u/list.go | 17 ++++++++++ pkg/u/list_test.go | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ pkg/u/util.go | 17 ---------- pkg/u/util_test.go | 96 ------------------------------------------------------ 5 files changed, 134 insertions(+), 113 deletions(-) create mode 100644 pkg/u/file.go create mode 100644 pkg/u/list.go create mode 100644 pkg/u/list_test.go delete mode 100644 pkg/u/util.go delete mode 100644 pkg/u/util_test.go (limited to 'pkg/u') diff --git a/pkg/u/file.go b/pkg/u/file.go new file mode 100644 index 0000000..cf86c75 --- /dev/null +++ b/pkg/u/file.go @@ -0,0 +1,21 @@ +package u + +import ( + "errors" + "log/slog" + "os" +) + +func FileExist(filename string) bool { + if _, err := os.Stat(filename); err == nil { + return true + + } else if errors.Is(err, os.ErrNotExist) { + return false + } else { + slog.Warn("Schrödinger's file: it may or may not exist", "file", filename) + // Schrodinger: file may or may not exist. To be extra safe it will + // report the file doest not exist + return false + } +} diff --git a/pkg/u/list.go b/pkg/u/list.go new file mode 100644 index 0000000..34eafd1 --- /dev/null +++ b/pkg/u/list.go @@ -0,0 +1,17 @@ +package u + +func First[T any](v []T) (T, bool) { + if len(v) == 0 { + var zero T + return zero, false + } + return v[0], true +} + +func ChunkBy[T any](items []T, chunkSize int) [][]T { + var chunks = make([][]T, 0, (len(items)/chunkSize)+1) + for chunkSize < len(items) { + items, chunks = items[chunkSize:], append(chunks, items[0:chunkSize:chunkSize]) + } + return append(chunks, items) +} diff --git a/pkg/u/list_test.go b/pkg/u/list_test.go new file mode 100644 index 0000000..a6d84c7 --- /dev/null +++ b/pkg/u/list_test.go @@ -0,0 +1,96 @@ +// go:build unit + +package u + +import ( + "testing" + + "github.com/google/go-cmp/cmp" +) + +func TestFirst(t *testing.T) { + testCases := []struct { + name string + slice []int + first int + exist bool + }{ + { + name: "multiple items slice", + slice: []int{1, 2, 3}, + first: 1, + exist: true, + }, + { + name: "single item slice", + slice: []int{1}, + first: 1, + exist: true, + }, + { + name: "empty slice", + slice: []int{}, + first: 0, + exist: false, + }, + } + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + + first, empty := First(tc.slice) + + if first != tc.first { + t.Errorf("Error first, want %d got %d", tc.first, first) + } + + if empty != tc.exist { + t.Errorf("Error empty, want %t got %t", tc.exist, empty) + } + + }) + } +} + +func TestSubList(t *testing.T) { + testCases := []struct { + name string + slice []int + size int + want [][]int + }{ + { + name: "sigle size sub list", + slice: []int{1, 2, 3}, + size: 1, + want: [][]int{{1}, {2}, {3}}, + }, + { + name: "multiple size sub list", + slice: []int{1, 2, 3, 4}, + size: 2, + want: [][]int{{1, 2}, {3, 4}}, + }, + { + name: "uneven multiple size sub list", + slice: []int{1, 2, 3, 4, 5}, + size: 2, + want: [][]int{{1, 2}, {3, 4}, {5}}, + }, + { + name: "empty sub list", + slice: []int{}, + size: 2, + want: [][]int{{}}, + }, + } + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + + subList := ChunkBy(tc.slice, tc.size) + + if diff := cmp.Diff(tc.want, subList); diff != "" { + t.Errorf("Wrong result given - wanted + got\n %s", diff) + } + }) + } +} diff --git a/pkg/u/util.go b/pkg/u/util.go deleted file mode 100644 index 34eafd1..0000000 --- a/pkg/u/util.go +++ /dev/null @@ -1,17 +0,0 @@ -package u - -func First[T any](v []T) (T, bool) { - if len(v) == 0 { - var zero T - return zero, false - } - return v[0], true -} - -func ChunkBy[T any](items []T, chunkSize int) [][]T { - var chunks = make([][]T, 0, (len(items)/chunkSize)+1) - for chunkSize < len(items) { - items, chunks = items[chunkSize:], append(chunks, items[0:chunkSize:chunkSize]) - } - return append(chunks, items) -} diff --git a/pkg/u/util_test.go b/pkg/u/util_test.go deleted file mode 100644 index a6d84c7..0000000 --- a/pkg/u/util_test.go +++ /dev/null @@ -1,96 +0,0 @@ -// go:build unit - -package u - -import ( - "testing" - - "github.com/google/go-cmp/cmp" -) - -func TestFirst(t *testing.T) { - testCases := []struct { - name string - slice []int - first int - exist bool - }{ - { - name: "multiple items slice", - slice: []int{1, 2, 3}, - first: 1, - exist: true, - }, - { - name: "single item slice", - slice: []int{1}, - first: 1, - exist: true, - }, - { - name: "empty slice", - slice: []int{}, - first: 0, - exist: false, - }, - } - for _, tc := range testCases { - t.Run(tc.name, func(t *testing.T) { - - first, empty := First(tc.slice) - - if first != tc.first { - t.Errorf("Error first, want %d got %d", tc.first, first) - } - - if empty != tc.exist { - t.Errorf("Error empty, want %t got %t", tc.exist, empty) - } - - }) - } -} - -func TestSubList(t *testing.T) { - testCases := []struct { - name string - slice []int - size int - want [][]int - }{ - { - name: "sigle size sub list", - slice: []int{1, 2, 3}, - size: 1, - want: [][]int{{1}, {2}, {3}}, - }, - { - name: "multiple size sub list", - slice: []int{1, 2, 3, 4}, - size: 2, - want: [][]int{{1, 2}, {3, 4}}, - }, - { - name: "uneven multiple size sub list", - slice: []int{1, 2, 3, 4, 5}, - size: 2, - want: [][]int{{1, 2}, {3, 4}, {5}}, - }, - { - name: "empty sub list", - slice: []int{}, - size: 2, - want: [][]int{{}}, - }, - } - for _, tc := range testCases { - t.Run(tc.name, func(t *testing.T) { - - subList := ChunkBy(tc.slice, tc.size) - - if diff := cmp.Diff(tc.want, subList); diff != "" { - t.Errorf("Wrong result given - wanted + got\n %s", diff) - } - }) - } -} -- cgit v1.2.3