@@ -39,18 +39,18 @@ func Encoding[T any](t *testing.T, opt EncodingOptions[T]) {
3939func Decode [T any ](t * testing.T , opt EncodingOptions [T ]) {
4040 t .Helper ()
4141
42- var got T
42+ var diff string
4343
4444 api := don .New (nil )
45- api .Post ("/" , don .H (func (ctx context.Context , req T ) (don. Empty , error ) {
46- got = req
47- return don. Empty {} , nil
45+ api .Post ("/" , don .H (func (ctx context.Context , req T ) (any , error ) {
46+ diff = cmp . Diff ( opt . Parsed , req , ignoreUnexported [ T ]())
47+ return nil , nil
4848 }))
4949
5050 ctx := httptest .NewRequest (http .MethodPost , "/" , opt .Raw , map [string ]string {"Content-Type" : opt .Mime })
5151 api .RequestHandler ()(ctx )
5252
53- if diff := cmp . Diff ( opt . Parsed , got , ignoreUnexported [ T ]()); diff != "" {
53+ if diff != "" {
5454 t .Error (diff )
5555 }
5656
@@ -63,7 +63,7 @@ func Encode[T any](t *testing.T, opt EncodingOptions[T]) {
6363 t .Helper ()
6464
6565 api := don .New (nil )
66- api .Post ("/" , don .H (func (ctx context.Context , req don. Empty ) (T , error ) {
66+ api .Post ("/" , don .H (func (ctx context.Context , req any ) (T , error ) {
6767 return opt .Parsed , nil
6868 }))
6969
@@ -113,9 +113,14 @@ func BenchmarkDecode[T any](b *testing.B, opt EncodingOptions[T]) {
113113 ctx := httptest .NewRequest ("POST" , "/" , "" , nil )
114114 ctx .Request .SetBodyStream (rd , len (opt .Raw ))
115115
116+ v := new (T )
117+ if val := reflect .ValueOf (v ).Elem (); val .Kind () == reflect .Pointer {
118+ val .Set (reflect .New (val .Type ().Elem ()))
119+ }
120+
116121 for i := 0 ; i < b .N ; i ++ {
117122 rd .Seek (0 , io .SeekStart ) //nolint:errcheck
118- dec (ctx , new ( T )) //nolint:errcheck
123+ dec (ctx , v ) //nolint:errcheck
119124 }
120125}
121126
0 commit comments