Skip to content

Commit

Permalink
Merge pull request #1 from brenol/master
Browse files Browse the repository at this point in the history
Add benchmark and improve performance
  • Loading branch information
TomWright committed Oct 7, 2018
2 parents 72513e3 + b8953b2 commit 1416dbb
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
11 changes: 6 additions & 5 deletions unmarshal.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package queryparam

import (
"reflect"
"errors"
"net/url"
"fmt"
"net/url"
"reflect"
"strings"
)

Expand All @@ -28,7 +28,7 @@ func delimiterFromField(field reflect.StructField) string {
return defaultDelimiter
}

func unmarshalField(v reflect.Value, t reflect.Type, i int, u *url.URL) error {
func unmarshalField(v reflect.Value, t reflect.Type, i int, qs url.Values) error {
var (
field reflect.StructField
paramVal string
Expand All @@ -42,7 +42,7 @@ func unmarshalField(v reflect.Value, t reflect.Type, i int, u *url.URL) error {
return nil
}

paramVal = u.Query().Get(tagVal)
paramVal = qs.Get(tagVal)
if len(paramVal) == 0 {
return nil
}
Expand Down Expand Up @@ -75,8 +75,9 @@ func Unmarshal(u *url.URL, i interface{}) error {
v := iVal.Elem()
t := v.Type()

qs := u.Query()
for i := 0; i < t.NumField(); i++ {
if err := unmarshalField(v, t, i, u); err != nil {
if err := unmarshalField(v, t, i, qs); err != nil {
return err
}
}
Expand Down
32 changes: 26 additions & 6 deletions unmarshal_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package queryparam_test

import (
"testing"
"github.com/tomwright/queryparam"
"net/url"
"net/http"
"fmt"
"net/http"
"net/url"
"reflect"
"testing"

"github.com/tomwright/queryparam"
)

// ExampleUnmarshal creates a dummy http request and unmarshals the data into a struct
Expand Down Expand Up @@ -96,7 +97,7 @@ func TestUnmarshal(t *testing.T) {
st.Errorf("unable to unmarshal url: %s", err)
}

if ! reflect.DeepEqual(tc.Data, tc.OutputData) {
if !reflect.DeepEqual(tc.Data, tc.OutputData) {
st.Errorf("expected `%v`, got `%v`", tc.OutputData, tc.Data)
}
})
Expand All @@ -120,7 +121,7 @@ func TestUnmarshal_BlankDelimiterUsesDefaultDelimiter(t *testing.T) {
t.Errorf("unexpected error: %s", err)
}

if exp, got := []string{"Tom", "Jim"}, req.Name; ! reflect.DeepEqual(exp, got) {
if exp, got := []string{"Tom", "Jim"}, req.Name; !reflect.DeepEqual(exp, got) {
t.Errorf("unexpected result. expected `%v`, got `%v`", exp, got)
}
}
Expand Down Expand Up @@ -209,3 +210,22 @@ func TestUnmarshal_InvalidFieldType(t *testing.T) {
t.Errorf("unexpected error string. expected `%s`, got `%s`", exp, got)
}
}

func BenchmarkUnmarshal(b *testing.B) {

u, err := url.Parse("http://localhost:123?name=abcd&namelist=a,b,c&namelistdash=abc&age=12")
if err != nil {
b.FailNow()
}

b.ResetTimer()
for i := 0; i < b.N; i++ {
data := testData{}
err = queryparam.Unmarshal(u, &data)
if err != nil {
b.FailNow()
}
}
b.StopTimer()
b.ReportAllocs()
}

0 comments on commit 1416dbb

Please sign in to comment.