Skip to content
This repository was archived by the owner on Jun 10, 2025. It is now read-only.

Commit 3988bf8

Browse files
committed
feat: new impl
1 parent f68f21a commit 3988bf8

File tree

5 files changed

+3
-179
lines changed

5 files changed

+3
-179
lines changed

context.go

Lines changed: 0 additions & 34 deletions
This file was deleted.

context_test.go

Lines changed: 0 additions & 32 deletions
This file was deleted.

encoding.go

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -214,19 +214,7 @@ func (r *Encoding) BindQuery(req *http.Request, v any) error {
214214
}
215215

216216
// BindUri binds the passed struct pointer using the uri codec.Marshaler.
217-
// NOTE: before use this, you should set uri params in the request context with RequestWithUri.
218-
//
219-
// Deprecated: Use BindURI instead.
220-
func (r *Encoding) BindUri(req *http.Request, v any) error {
221-
raws := FromRequestUri(req)
222-
if raws == nil {
223-
return errors.New("encoding: must be request with uri in context")
224-
}
225-
return r.mimeUri.Decode(raws, v)
226-
}
227-
228-
// BindUri binds the passed struct pointer using the uri codec.Marshaler.
229-
func (r *Encoding) BindURI(raws url.Values, v any) error {
217+
func (r *Encoding) BindUri(raws url.Values, v any) error {
230218
return r.mimeUri.Decode(raws, v)
231219
}
232220

encoding_test.go

Lines changed: 1 addition & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -494,108 +494,10 @@ func Test_Encoding_BindQuery(t *testing.T) {
494494
}
495495
}
496496

497-
// Deprecated: Because BindUri is deprecated.
498497
func Test_Encoding_BindUri(t *testing.T) {
499498
registry := New()
500499
require.NoError(t, registry.Register(MIMEURI, form.New("json")))
501500

502-
tests := []struct {
503-
name string
504-
genReq func() (*http.Request, error)
505-
want any
506-
wantErr bool
507-
}{
508-
{
509-
"uri - no proto",
510-
func() (*http.Request, error) {
511-
r, err := http.NewRequest(http.MethodGet, "http://example.com/foo/bar", nil)
512-
if err != nil {
513-
return nil, err
514-
}
515-
516-
param := url.Values{}
517-
param.Add("id", "foo")
518-
param.Add("name", "bar")
519-
return RequestWithUri(r, param), nil
520-
},
521-
&TestMode{
522-
Id: "foo",
523-
Name: "bar",
524-
},
525-
false,
526-
},
527-
{
528-
"uri - proto",
529-
func() (*http.Request, error) {
530-
r, err := http.NewRequest(http.MethodGet, "http://example.com?id=11&uint32=1234&bool=true", nil)
531-
if err != nil {
532-
return nil, err
533-
}
534-
param := url.Values{}
535-
param.Add("id", "11")
536-
param.Add("uint32", "1234")
537-
param.Add("bool", "true")
538-
return RequestWithUri(r, param), nil
539-
},
540-
&examplepb.Complex{
541-
Id: 11,
542-
Uint32: wrapperspb.UInt32(1234),
543-
Bool: wrapperspb.Bool(true),
544-
},
545-
false,
546-
},
547-
{
548-
"uri - always in context",
549-
func() (*http.Request, error) {
550-
r, err := http.NewRequest(http.MethodGet, "http://example.com?id=11&uint32=1234&bool=true", nil)
551-
if err != nil {
552-
return nil, err
553-
}
554-
r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
555-
return RequestWithUri(r, nil), nil
556-
},
557-
&examplepb.Complex{},
558-
false,
559-
},
560-
{
561-
"uri - no existing in context",
562-
func() (*http.Request, error) {
563-
r, err := http.NewRequest(http.MethodGet, "http://example.com?id=11&uint32=1234&bool=true", nil)
564-
if err != nil {
565-
return nil, err
566-
}
567-
r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
568-
return r, nil
569-
},
570-
&examplepb.Complex{},
571-
true,
572-
},
573-
}
574-
for _, tt := range tests {
575-
t.Run(tt.name, func(t *testing.T) {
576-
req, err := tt.genReq()
577-
if err != nil {
578-
t.Errorf("genReq() error = %v", err)
579-
}
580-
got := alloc(reflect.TypeOf(tt.want))
581-
if err = registry.BindUri(req, got.Interface()); (err != nil) != tt.wantErr {
582-
t.Errorf("BindUri() error = %v, wantErr %v", err, tt.wantErr)
583-
}
584-
if _, ok := tt.want.(proto.Message); ok {
585-
if diff := proto.Equal(got.Interface().(proto.Message), tt.want.(proto.Message)); !diff {
586-
t.Errorf("got = %v, want %v", got, tt.want)
587-
}
588-
} else {
589-
require.Equal(t, got.Interface(), tt.want)
590-
}
591-
})
592-
}
593-
}
594-
595-
func Test_Encoding_BindURI(t *testing.T) {
596-
registry := New()
597-
require.NoError(t, registry.Register(MIMEURI, form.New("json")))
598-
599501
tests := []struct {
600502
name string
601503
genUri func() (url.Values, error)
@@ -640,7 +542,7 @@ func Test_Encoding_BindURI(t *testing.T) {
640542
t.Errorf("genUri() error = %v", err)
641543
}
642544
got := alloc(reflect.TypeOf(tt.want))
643-
if err = registry.BindURI(raws, got.Interface()); (err != nil) != tt.wantErr {
545+
if err = registry.BindUri(raws, got.Interface()); (err != nil) != tt.wantErr {
644546
t.Errorf("BindURI() error = %v, wantErr %v", err, tt.wantErr)
645547
}
646548
if _, ok := tt.want.(proto.Message); ok {

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/things-go/encoding
22

3-
go 1.20
3+
go 1.21
44

55
require (
66
github.com/go-playground/form/v4 v4.2.1

0 commit comments

Comments
 (0)