mirror of
https://github.com/go-kratos/kratos.git
synced 2025-03-17 21:07:54 +02:00
fix bind vars
This commit is contained in:
parent
f83c9ad053
commit
01d93ec61a
@ -18,11 +18,6 @@ func Register{{.ServiceType}}HTTPServer(s http1.ServiceRegistrar, srv {{.Service
|
||||
{{range .Methods}}
|
||||
func _HTTP_{{$.ServiceType}}_{{.Name}}_{{.Num}}(srv interface{}, ctx context.Context, req *http.Request, dec func(interface{}) error) (interface{}, error) {
|
||||
var in {{.Request}}
|
||||
{{if ne (len .Vars) 0}}
|
||||
if err := http1.BindVars(req, &in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
{{end}}
|
||||
{{if eq .Body ""}}
|
||||
if err := http1.BindForm(req, &in); err != nil {
|
||||
return nil, err
|
||||
@ -35,6 +30,11 @@ func _HTTP_{{$.ServiceType}}_{{.Name}}_{{.Num}}(srv interface{}, ctx context.Con
|
||||
if err := dec(in{{.Body}}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
{{end}}
|
||||
{{if ne (len .Vars) 0}}
|
||||
if err := http1.BindVars(req, &in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
{{end}}
|
||||
out, err := srv.({{$.ServiceType}}Server).{{.Name}}(ctx, &in)
|
||||
if err != nil {
|
||||
|
@ -25,9 +25,9 @@ func contentSubtype(contentType string) string {
|
||||
// guaranteed since != baseContentType and has baseContentType prefix
|
||||
switch contentType[len(baseContentType)] {
|
||||
case '/', ';':
|
||||
// this will return true for "application/grpc+" or "application/grpc;"
|
||||
// which the previous validContentType function tested to be valid, so we
|
||||
// just say that no content-subtype is specified in this case
|
||||
if i := strings.Index(contentType, ";"); i != -1 {
|
||||
return contentType[len(baseContentType)+1 : i]
|
||||
}
|
||||
return contentType[len(baseContentType)+1:]
|
||||
default:
|
||||
return ""
|
||||
|
23
transport/http/default_test.go
Normal file
23
transport/http/default_test.go
Normal file
@ -0,0 +1,23 @@
|
||||
package http
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestSubtype(t *testing.T) {
|
||||
tests := []struct {
|
||||
input string
|
||||
expected string
|
||||
}{
|
||||
{"application/json", "json"},
|
||||
{"application/json;", "json"},
|
||||
{"application/json; charset=utf-8", "json"},
|
||||
{"application/", ""},
|
||||
{"application", ""},
|
||||
{"foo", ""},
|
||||
{"", ""},
|
||||
}
|
||||
for _, test := range tests {
|
||||
if contentSubtype(test.input) != test.expected {
|
||||
t.Errorf("expected %s got %s", test.expected, test.input)
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user