From dd2a61018dcddffb85d6fd20cabd5dc3e3e823db Mon Sep 17 00:00:00 2001 From: Vincent Boutour Date: Sun, 12 Mar 2023 12:43:10 +0100 Subject: [PATCH] refactor: Removing unused code Signed-off-by: Vincent Boutour --- pkg/crud/list.go | 3 +- pkg/provider/json.go | 49 +++++++++++++++++++ pkg/provider/util.go | 74 ---------------------------- pkg/provider/util_test.go | 100 -------------------------------------- 4 files changed, 51 insertions(+), 175 deletions(-) create mode 100644 pkg/provider/json.go diff --git a/pkg/crud/list.go b/pkg/crud/list.go index 61f9f755..aa43482c 100644 --- a/pkg/crud/list.go +++ b/pkg/crud/list.go @@ -4,6 +4,7 @@ import ( "archive/zip" "bytes" "context" + "errors" "fmt" "io" "net/http" @@ -234,7 +235,7 @@ func (a App) addFileToZip(ctx context.Context, zipWriter *zip.Writer, item absto } defer func() { - err = provider.HandleClose(reader, err) + err = errors.Join(err, reader.Close()) }() buffer := provider.BufferPool.Get().(*bytes.Buffer) diff --git a/pkg/provider/json.go b/pkg/provider/json.go new file mode 100644 index 00000000..ec327a67 --- /dev/null +++ b/pkg/provider/json.go @@ -0,0 +1,49 @@ +package provider + +import ( + "context" + "encoding/json" + "errors" + "fmt" + "io" + + absto "github.com/ViBiOh/absto/pkg/model" +) + +func LoadJSON[T any](ctx context.Context, storageApp absto.Storage, filename string) (output T, err error) { + var reader io.ReadCloser + + reader, err = storageApp.ReadFrom(ctx, filename) + if err != nil { + err = fmt.Errorf("read: %w", err) + return + } + + defer func() { + err = errors.Join(err, reader.Close()) + }() + + if err = json.NewDecoder(reader).Decode(&output); err != nil { + err = fmt.Errorf("decode: %w", storageApp.ConvertError(err)) + } + + return +} + +func SaveJSON[T any](ctx context.Context, storageApp absto.Storage, filename string, content T) error { + reader, writer := io.Pipe() + + done := make(chan error) + go func() { + defer close(done) + var err error + + if jsonErr := json.NewEncoder(writer).Encode(content); jsonErr != nil { + err = fmt.Errorf("encode: %w", jsonErr) + } + + done <- errors.Join(err, writer.Close()) + }() + + return errors.Join(storageApp.WriteTo(ctx, filename, reader, absto.WriteOpts{}), <-done) +} diff --git a/pkg/provider/util.go b/pkg/provider/util.go index acd47a01..97b919b3 100644 --- a/pkg/provider/util.go +++ b/pkg/provider/util.go @@ -3,7 +3,6 @@ package provider import ( "bytes" "context" - "encoding/json" "errors" "fmt" "io" @@ -127,71 +126,6 @@ func DoneWriter(isDone func() bool, w io.Writer, content string) { SafeWrite(w, content) } -func FindPath(arr []string, value string) int { - for index, item := range arr { - if strings.HasPrefix(item, value) { - return index - } - } - - return -1 -} - -func RemoveIndex(arr []string, index int) []string { - if len(arr) == 0 || index < 0 || index >= len(arr) { - return arr - } - - return append(arr[:index], arr[index+1:]...) -} - -func LoadJSON[T any](ctx context.Context, storageApp absto.Storage, filename string) (output T, err error) { - var reader io.ReadCloser - reader, err = storageApp.ReadFrom(ctx, filename) - if err != nil { - err = fmt.Errorf("read: %w", err) - return - } - - defer func() { - err = HandleClose(reader, err) - }() - - if err = json.NewDecoder(reader).Decode(&output); err != nil { - err = fmt.Errorf("decode: %w", storageApp.ConvertError(err)) - } - - return -} - -func SaveJSON(ctx context.Context, storageApp absto.Storage, filename string, content any) error { - reader, writer := io.Pipe() - - done := make(chan error) - go func() { - defer close(done) - var err error - - if jsonErr := json.NewEncoder(writer).Encode(content); jsonErr != nil { - err = fmt.Errorf("encode: %w", jsonErr) - } - - if closeErr := writer.Close(); closeErr != nil { - err = errors.Join(err, fmt.Errorf("close encoder: %w", closeErr)) - } - - done <- err - }() - - err := storageApp.WriteTo(ctx, filename, reader, absto.WriteOpts{}) - - if jsonErr := <-done; jsonErr != nil { - err = errors.Join(err, jsonErr) - } - - return err -} - func SendLargeFile(ctx context.Context, storageApp absto.Storage, item absto.Item, req request.Request) (*http.Response, error) { file, err := storageApp.ReadFrom(ctx, item.Pathname) // will be closed by `PipeWriter` if err != nil { @@ -223,14 +157,6 @@ func SendLargeFile(ctx context.Context, storageApp absto.Storage, item absto.Ite return request.DoWithClient(SlowClient, r) } -func HandleClose(closer io.Closer, err error) error { - if closeErr := closer.Close(); closeErr != nil { - return errors.Join(err, fmt.Errorf("close: %w", closeErr)) - } - - return err -} - func LogClose(closer io.Closer, fn, item string) { if err := closer.Close(); err != nil { logger.WithField("fn", fn).WithField("item", item).Error("close: %s", err) diff --git a/pkg/provider/util_test.go b/pkg/provider/util_test.go index 31a80927..bf5de9d5 100644 --- a/pkg/provider/util_test.go +++ b/pkg/provider/util_test.go @@ -2,7 +2,6 @@ package provider import ( "io" - "reflect" "testing" ) @@ -91,102 +90,3 @@ func TestSafeWrite(t *testing.T) { }) } } - -func TestFindIndex(t *testing.T) { - type args struct { - arr []string - value string - } - - cases := map[string]struct { - args args - want int - }{ - "empty": { - args{}, - -1, - }, - "single element": { - args{ - arr: []string{"localhost"}, - value: "localhost", - }, - 0, - }, - "multiple element": { - args{ - arr: []string{"localhost", "::1", "world.com"}, - value: "::1", - }, - 1, - }, - } - - for intention, tc := range cases { - t.Run(intention, func(t *testing.T) { - if got := FindPath(tc.args.arr, tc.args.value); got != tc.want { - t.Errorf("FindIndex() = %d, want %d", got, tc.want) - } - }) - } -} - -func TestRemoveIndex(t *testing.T) { - type args struct { - arr []string - index int - } - - cases := map[string]struct { - args args - want []string - }{ - "empty": { - args{}, - nil, - }, - "negative": { - args{ - arr: []string{"localhost"}, - index: -1, - }, - []string{"localhost"}, - }, - "index out of range": { - args{ - arr: []string{"localhost"}, - index: 1, - }, - []string{"localhost"}, - }, - "valid": { - args{ - arr: []string{"localhost"}, - index: 0, - }, - []string{}, - }, - "multiple": { - args{ - arr: []string{"localhost", "::1", "world.com"}, - index: 1, - }, - []string{"localhost", "world.com"}, - }, - "upper bounds": { - args{ - arr: []string{"localhost", "::1", "world.com"}, - index: 2, - }, - []string{"localhost", "::1"}, - }, - } - - for intention, tc := range cases { - t.Run(intention, func(t *testing.T) { - if got := RemoveIndex(tc.args.arr, tc.args.index); !reflect.DeepEqual(got, tc.want) { - t.Errorf("RemoveIndex() = %+v, want %+v", got, tc.want) - } - }) - } -}