Skip to content

Commit

Permalink
found, fixed, and tested a nil panic
Browse files Browse the repository at this point in the history
  • Loading branch information
tigh-latte committed May 13, 2024
1 parent 98b9046 commit b045670
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
5 changes: 4 additions & 1 deletion encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,10 @@ func (e *encoder) marshal(namespace []byte, v reflect.Value, idx int) (bool, err
if t.Kind() == reflect.Ptr && v.IsNil() {
return false, nil
}
um := v.Interface().(Marshaler)
um, ok := v.Interface().(Marshaler)
if !ok {
return false, nil
}
vals, err := um.MarshalForm()
if err != nil {
return false, err
Expand Down
47 changes: 47 additions & 0 deletions encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1718,3 +1718,50 @@ func Test_MarshalForm_Err(t *testing.T) {
Equal(t, err.Error(), "Field Namespace:Struct ERROR:always err")
Equal(t, v3, url.Values{})
}

type errmarshalerptrrec struct {
called bool
Fname string
Sname string
}

func (m *errmarshalerptrrec) MarshalForm() ([]string, error) {
m.called = true
return nil, errors.New("always err")
}

func Test_MarshalForm_ErrPtrRec(t *testing.T) {
encoder := NewEncoder()

t1 := struct {
Ptr *errmarshalerptrrec
}{
Ptr: &errmarshalerptrrec{
Fname: "John",
Sname: "Smith",
},
}
v1, err := encoder.Encode(t1)
NotEqual(t, err, nil)
Equal(t, err.Error(), "Field Namespace:Ptr ERROR:always err")
Equal(t, v1, url.Values{})

t2 := struct {
NilPtr *errmarshalerptrrec
}{}
v2, err := encoder.Encode(t2)
Equal(t, err, nil)
Equal(t, v2, url.Values{})

t3 := struct {
Struct errmarshalerptrrec
}{
Struct: errmarshalerptrrec{
Fname: "John",
Sname: "Smith",
},
}
_, err = encoder.Encode(t3)
Equal(t, err, nil)
Equal(t, t3.Struct.called, false)
}

0 comments on commit b045670

Please sign in to comment.