Skip to content

Commit

Permalink
refactor: Removing unused code
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Boutour <[email protected]>
  • Loading branch information
ViBiOh committed Mar 12, 2023
1 parent 4ecd63e commit dd2a610
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 175 deletions.
3 changes: 2 additions & 1 deletion pkg/crud/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"archive/zip"
"bytes"
"context"
"errors"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -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)
Expand Down
49 changes: 49 additions & 0 deletions pkg/provider/json.go
Original file line number Diff line number Diff line change
@@ -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)
}
74 changes: 0 additions & 74 deletions pkg/provider/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package provider
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
100 changes: 0 additions & 100 deletions pkg/provider/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package provider

import (
"io"
"reflect"
"testing"
)

Expand Down Expand Up @@ -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)
}
})
}
}

0 comments on commit dd2a610

Please sign in to comment.