From 16d4c15cd22997e01c1af6fbee1cfe4f654f83a7 Mon Sep 17 00:00:00 2001 From: Nicholas Wiersma Date: Mon, 2 Oct 2023 18:34:25 +0200 Subject: [PATCH] chore: cleanup deprecated functions --- http/json.go | 30 -------------------- http/json_test.go | 71 ----------------------------------------------- ptr/ptr.go | 42 ---------------------------- ptr/ptr_test.go | 48 -------------------------------- 4 files changed, 191 deletions(-) delete mode 100755 http/json.go delete mode 100755 http/json_test.go diff --git a/http/json.go b/http/json.go deleted file mode 100755 index 09bbefc..0000000 --- a/http/json.go +++ /dev/null @@ -1,30 +0,0 @@ -package http - -import ( - "net/http" - - jsoniter "github.com/json-iterator/go" -) - -// JSONContentType represents MIME type for JSON content. -const JSONContentType = "application/json" - -// JSON encodes json content to the ResponseWriter. -// -// Deprecated: Use the `http/render` package instead. -func JSON(w http.ResponseWriter, code int, v interface{}) error { - raw, err := jsoniter.Marshal(v) - if err != nil { - w.WriteHeader(http.StatusInternalServerError) - return err - } - - w.Header().Set("Content-Type", JSONContentType) - w.WriteHeader(code) - - if _, err = w.Write(raw); err != nil { - return err - } - - return nil -} diff --git a/http/json_test.go b/http/json_test.go deleted file mode 100755 index 1c4e5e2..0000000 --- a/http/json_test.go +++ /dev/null @@ -1,71 +0,0 @@ -package http_test - -import ( - "errors" - "net/http" - "net/http/httptest" - "testing" - - httpx "github.com/hamba/pkg/v2/http" - "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/require" -) - -func TestJSON(t *testing.T) { - tests := []struct { - name string - code int - data interface{} - wantJSON string - wantErr require.ErrorAssertionFunc - }{ - { - name: "encodes JSON", - code: http.StatusOK, - data: struct { - Foo string - Bar string - }{"foo", "bar"}, - wantJSON: `{"Foo":"foo","Bar":"bar"}`, - wantErr: require.NoError, - }, - { - name: "handles bad data", - code: http.StatusInternalServerError, - data: make(chan int), - wantErr: require.Error, - }, - } - - for _, test := range tests { - test := test - t.Run(test.name, func(t *testing.T) { - t.Parallel() - - w := httptest.NewRecorder() - err := httpx.JSON(w, test.code, test.data) - - test.wantErr(t, err) - assert.Equal(t, test.code, w.Code) - assert.Equal(t, test.wantJSON, string(w.Body.Bytes())) - - if test.code/100 == 2 { - assert.Equal(t, httpx.JSONContentType, w.Header().Get("Content-Type")) - } - }) - } -} - -func TestJSON_WriteError(t *testing.T) { - w := testResponseWriter{} - - err := httpx.JSON(w, 200, "test") - - assert.Error(t, err) -} - -type testResponseWriter struct{} - -func (rw testResponseWriter) Header() http.Header { return http.Header{} } -func (rw testResponseWriter) Write([]byte) (int, error) { return 0, errors.New("test error") } -func (rw testResponseWriter) WriteHeader(int) {} diff --git a/ptr/ptr.go b/ptr/ptr.go index d48940f..cb32cb2 100644 --- a/ptr/ptr.go +++ b/ptr/ptr.go @@ -5,45 +5,3 @@ package ptr func Of[T any](v T) *T { return &v } - -// Bool converts a bool into a bool pointer. -// -// Deprecated: Use Of instead. -func Bool(b bool) *bool { - return &b -} - -// Float32 converts a float32 into a float32 pointer. -// -// Deprecated: Use Of instead. -func Float32(f float32) *float32 { - return &f -} - -// Float64 converts a float64 into a float64 pointer. -// -// Deprecated: Use Of instead. -func Float64(f float64) *float64 { - return &f -} - -// Int converts an int into an int pointer. -// -// Deprecated: Use Of instead. -func Int(i int) *int { - return &i -} - -// Int64 converts an int64 into an int64 pointer. -// -// Deprecated: Use Of instead. -func Int64(i int64) *int64 { - return &i -} - -// String converts a string into a string pointer. -// -// Deprecated: Use Of instead. -func String(s string) *string { - return &s -} diff --git a/ptr/ptr_test.go b/ptr/ptr_test.go index a1a7838..f863488 100644 --- a/ptr/ptr_test.go +++ b/ptr/ptr_test.go @@ -14,51 +14,3 @@ func TestOf(t *testing.T) { assert.Exactly(t, &want, got) } - -func TestBool(t *testing.T) { - want := true - - got := ptr.Bool(want) - - assert.Exactly(t, &want, got) -} - -func TestFloat32(t *testing.T) { - want := float32(1.0) - - got := ptr.Float32(want) - - assert.Exactly(t, &want, got) -} - -func TestFloat64(t *testing.T) { - want := float64(1.0) - - got := ptr.Float64(want) - - assert.Exactly(t, &want, got) -} - -func TestInt(t *testing.T) { - want := 1 - - got := ptr.Int(want) - - assert.Exactly(t, &want, got) -} - -func TestInt64(t *testing.T) { - want := int64(1) - - got := ptr.Int64(want) - - assert.Exactly(t, &want, got) -} - -func TestString(t *testing.T) { - want := "foo" - - got := ptr.String(want) - - assert.Exactly(t, &want, got) -}