diff --git a/admin/admin.go b/admin/admin.go index 7815148..18632d2 100644 --- a/admin/admin.go +++ b/admin/admin.go @@ -62,7 +62,7 @@ type User struct { } // URLValues transforms a User into url.Values using the 'url' struct tag to -// define the key of the map. Fields are skiped if the value is empty. +// define the key of the map. Fields are skipped if the value is empty. func (u *User) URLValues() url.Values { params := url.Values{} @@ -75,17 +75,20 @@ func (u *User) URLValues() url.Values { if tag == "" { continue } - // Skip fields have a zero value. - if v.Field(i).Interface() == reflect.Zero(v.Field(i).Type()).Interface() { - continue + + f := v.Field(i) + + // Dereference Pointers + if f.Kind() == reflect.Ptr && !f.IsNil() { + f = f.Elem() } - var val string - if t.Field(i).Type.Kind() == reflect.Ptr { - val = fmt.Sprintf("%v", v.Field(i).Elem()) - } else { - val = fmt.Sprintf("%v", v.Field(i)) + + // Skip fields that have a zero value. + if f.Interface() == reflect.Zero(f.Type()).Interface() { + continue } - params[tag] = []string{val} + + params[tag] = []string{fmt.Sprintf("%v", f)} } return params } diff --git a/admin/admin_test.go b/admin/admin_test.go index b71003a..4317761 100644 --- a/admin/admin_test.go +++ b/admin/admin_test.go @@ -159,6 +159,7 @@ func TestUser_URLValues(t *testing.T) { } exAlias := "smith" + emptyFirstName := "" tests := []struct { name string @@ -204,6 +205,18 @@ func TestUser_URLValues(t *testing.T) { "username": {"jsmith"}}, ), }, + { + name: "Empty values skipped", + fields: fields{ + Username: "jsmith", + FirstName: &emptyFirstName, + LastName: nil, + Notes: "", + }, + want: url.Values(map[string][]string{ + "username": {"jsmith"}}, + ), + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) {