Skip to content

Commit 505e66e

Browse files
authored
Revert "Fix panic when the decoding pointer type with custom decoding functio…"
This reverts commit 212b1c4.
1 parent 1f24742 commit 505e66e

File tree

2 files changed

+7
-57
lines changed

2 files changed

+7
-57
lines changed

decoder.go

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -192,15 +192,8 @@ func (d *decoder) setFieldByType(current reflect.Value, namespace []byte, idx in
192192
d.setError(namespace, err)
193193
return
194194
}
195-
if kind == reflect.Ptr {
196-
newVal := reflect.New(v.Type().Elem())
197-
if set = d.setFieldByType(newVal.Elem(), namespace, idx); set {
198-
v.Set(newVal)
199-
}
200-
} else {
201-
v.Set(reflect.ValueOf(val))
202-
}
203195

196+
v.Set(reflect.ValueOf(val))
204197
set = true
205198
return
206199
}

decoder_test.go

Lines changed: 6 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -830,71 +830,28 @@ func TestDecoderStruct(t *testing.T) {
830830

831831
func TestDecoderNativeTime(t *testing.T) {
832832

833-
type TType time.Time
834-
type TTypePtr *time.Time
835-
type TTypePtrWithCustomDecoder *time.Time
836-
837833
type TestError struct {
838-
Time time.Time
839-
TimeNoValue time.Time
840-
TimePtr *time.Time
841-
TimeType TType
842-
TimeTypeNoValue TType
843-
TimeTypePtr TTypePtr
844-
TimeTypePtrNoValue TTypePtr
845-
TimeTypePtrWithCustomDecoder TTypePtrWithCustomDecoder
846-
TimeTypePtrWithCustomDecoderNoValue TTypePtrWithCustomDecoder
834+
Time time.Time
835+
TimeNoValue time.Time
836+
TimePtr *time.Time
847837
}
848838

849839
values := url.Values{
850-
"Time": []string{"2006-01-02T15:04:05Z"},
851-
"TimeNoValue": []string{""},
852-
"TimePtr": []string{"2006-01-02T15:04:05Z"},
853-
"TimeType": []string{"2006-01-02T15:04:05Z"},
854-
"TimeTypeNoValue": []string{""},
855-
"TimeTypePtr": []string{"2006-01-02T15:04:05Z"},
856-
"TimeTypePtrNoValue": []string{""},
857-
"TimeTypePtrWithCustomDecoder": []string{"2006-01-02T15:04:05Z"},
858-
"TimeTypePtrWithCustomDecoderNoValue": []string{""},
840+
"Time": []string{"2006-01-02T15:04:05Z"},
841+
"TimeNoValue": []string{""},
842+
"TimePtr": []string{"2006-01-02T15:04:05Z"},
859843
}
860844

861845
var test TestError
862846

863847
decoder := NewDecoder()
864-
decoder.RegisterCustomTypeFunc(func(s []string) (interface{}, error) {
865-
if s[0] == "" {
866-
return TType{}, nil
867-
}
868-
parsed, err := time.Parse(time.RFC3339, s[0])
869-
if err != nil {
870-
return nil, err
871-
}
872-
return TType(parsed), nil
873-
}, TType{})
874-
decoder.RegisterCustomTypeFunc(func(s []string) (interface{}, error) {
875-
if s[0] == "" {
876-
return nil, nil
877-
}
878-
parsed, err := time.Parse(time.RFC3339, s[0])
879-
if err != nil {
880-
return nil, err
881-
}
882-
return TTypePtrWithCustomDecoder(&parsed), nil
883-
}, TTypePtrWithCustomDecoder(nil))
884848

885849
errs := decoder.Decode(&test, values)
886850
Equal(t, errs, nil)
887851

888852
tm, _ := time.Parse(time.RFC3339, "2006-01-02T15:04:05Z")
889853
Equal(t, test.Time.Equal(tm), true)
890854
Equal(t, test.TimeNoValue.Equal(tm), false)
891-
Equal(t, tm.Equal(time.Time(test.TimeType)), true)
892-
Equal(t, time.Time(test.TimeTypeNoValue).Equal(tm), false)
893-
Equal(t, time.Time(test.TimeTypeNoValue).IsZero(), true)
894-
Equal(t, (*time.Time)(test.TimeTypePtr).Equal(tm), true)
895-
Equal(t, test.TimeTypePtrNoValue, nil)
896-
Equal(t, (*time.Time)(test.TimeTypePtrWithCustomDecoder).Equal(tm), true)
897-
Equal(t, test.TimeTypePtrWithCustomDecoderNoValue, nil)
898855

899856
NotEqual(t, test.TimePtr, nil)
900857
Equal(t, (*test.TimePtr).Equal(tm), true)

0 commit comments

Comments
 (0)