From 3e4636c4547f1f8c07e2d3789265cd456cc0ca66 Mon Sep 17 00:00:00 2001 From: Denis Tarasov Date: Sun, 6 Sep 2020 21:07:18 +0300 Subject: [PATCH] Delete Unpacker type --- structtags/README.md | 4 ++-- structtags/structtags.go | 15 ++++----------- structtags/structtags_test.go | 29 ++++++++++------------------- 3 files changed, 16 insertions(+), 32 deletions(-) diff --git a/structtags/README.md b/structtags/README.md index bb0cf7d..67c9d27 100644 --- a/structtags/README.md +++ b/structtags/README.md @@ -7,7 +7,7 @@ goos: linux goarch: amd64 pkg: gitlab.com/slon/shad-go/structtags -BenchmarkUnpacker/user-4 3273 329346 ns/op -BenchmarkUnpacker/user+good+order-4 648 1721068 ns/op +BenchmarkUnpacker/user-4 3064 362500 ns/op +BenchmarkUnpacker/user+good+order-4 663 1799294 ns/op PASS ``` diff --git a/structtags/structtags.go b/structtags/structtags.go index 7ef8fb0..d97dbfd 100644 --- a/structtags/structtags.go +++ b/structtags/structtags.go @@ -10,13 +10,6 @@ import ( "strings" ) -type Unpacker struct { -} - -func NewUnpacker() *Unpacker { - return &Unpacker{} -} - // Функция Unpack присваивает значения параметров из url query в поля переданной структуры. // Для этого сначала в первом создается map, @@ -25,7 +18,7 @@ func NewUnpacker() *Unpacker { // В следующем цикле поля структуры заполняются соответствующими // значениями из url query. -func (u *Unpacker) Unpack(req *http.Request, ptr interface{}) error { +func Unpack(req *http.Request, ptr interface{}) error { if err := req.ParseForm(); err != nil { return err } @@ -48,12 +41,12 @@ func (u *Unpacker) Unpack(req *http.Request, ptr interface{}) error { for _, value := range values { if f.Kind() == reflect.Slice { elem := reflect.New(f.Type().Elem()).Elem() - if err := u.populate(elem, value); err != nil { + if err := populate(elem, value); err != nil { return fmt.Errorf("%s: %v", name, err) } f.Set(reflect.Append(f, elem)) } else { - if err := u.populate(f, value); err != nil { + if err := populate(f, value); err != nil { return fmt.Errorf("%s: %v", name, err) } } @@ -62,7 +55,7 @@ func (u *Unpacker) Unpack(req *http.Request, ptr interface{}) error { return nil } -func (u *Unpacker) populate(v reflect.Value, value string) error { +func populate(v reflect.Value, value string) error { switch v.Kind() { case reflect.String: v.SetString(value) diff --git a/structtags/structtags_test.go b/structtags/structtags_test.go index 2d97931..6204622 100644 --- a/structtags/structtags_test.go +++ b/structtags/structtags_test.go @@ -75,8 +75,7 @@ type Order struct { func TestUnpack_User(t *testing.T) { r, _ := http.NewRequest("GET", userURL, nil) user := &User{} - u := NewUnpacker() - err := u.Unpack(r, user) + err := Unpack(r, user) require.NoError(t, err) require.Equal(t, expectedUser.ID, user.ID) require.Equal(t, expectedUser.Name, user.Name) @@ -88,8 +87,7 @@ func TestUnpack_User(t *testing.T) { func TestUnpack_Good(t *testing.T) { r, _ := http.NewRequest("GET", goodURL, nil) good := &Good{} - u := NewUnpacker() - err := u.Unpack(r, good) + err := Unpack(r, good) require.NoError(t, err) require.Equal(t, expectedGood.ID, good.ID) require.Equal(t, expectedGood.Name, good.Name) @@ -98,9 +96,7 @@ func TestUnpack_Good(t *testing.T) { func TestUnpack_Order(t *testing.T) { r, _ := http.NewRequest("GET", orderURL, nil) order := &Order{} - u := NewUnpacker() - err := u.Unpack(r, order) - fmt.Println(orderURL) + err := Unpack(r, order) require.NoError(t, err) require.Equal(t, expectedOrder.ID, order.ID) require.Equal(t, expectedOrder.UserID, order.UserID) @@ -111,8 +107,7 @@ func TestUnpack_Order(t *testing.T) { func TestUnpack_ParseFormError(t *testing.T) { r, _ := http.NewRequest("POST", "localhost", nil) user := &User{} - u := NewUnpacker() - err := u.Unpack(r, user) + err := Unpack(r, user) require.Error(t, err) } @@ -120,8 +115,7 @@ func TestUnpack_IncorrectBoolData(t *testing.T) { url := "localhost/user?id=1&has_subscription=7" r, _ := http.NewRequest("GET", url, nil) user := &User{} - u := NewUnpacker() - err := u.Unpack(r, user) + err := Unpack(r, user) require.Error(t, err) } @@ -129,8 +123,7 @@ func TestUnpack_IncorrectIntData(t *testing.T) { url := "localhost/user?id=abc" r, _ := http.NewRequest("GET", url, nil) user := &User{} - u := NewUnpacker() - err := u.Unpack(r, user) + err := Unpack(r, user) require.Error(t, err) } @@ -145,21 +138,19 @@ func BenchmarkUnpacker(b *testing.B) { order := &Order{} b.Run("user", func(b *testing.B) { - u := NewUnpacker() for i := 0; i < b.N; i++ { for j := 0; j < 1000; j++ { - _ = u.Unpack(userRequest, user) + _ = Unpack(userRequest, user) } } }) b.Run("user+good+order", func(b *testing.B) { - u := NewUnpacker() for i := 0; i < b.N; i++ { for j := 0; j < 1000; j++ { - _ = u.Unpack(userRequest, user) - _ = u.Unpack(goodRequest, good) - _ = u.Unpack(orderRequest, order) + _ = Unpack(userRequest, user) + _ = Unpack(goodRequest, good) + _ = Unpack(orderRequest, order) } } })