Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions admin/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}

Expand All @@ -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
}
Expand Down
13 changes: 13 additions & 0 deletions admin/admin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ func TestUser_URLValues(t *testing.T) {
}

exAlias := "smith"
emptyFirstName := ""

tests := []struct {
name string
Expand Down Expand Up @@ -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) {
Expand Down