http/binding: rename encode url (#1039)

* rename binding url
pull/1040/head
Tony Chen 4 years ago committed by GitHub
parent eca0f35cb5
commit 2729caeecf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 6
      api/metadata/metadata_http.pb.go
  2. 2
      cmd/protoc-gen-go-http/http.go
  3. 2
      cmd/protoc-gen-go-http/template.go
  4. 12
      examples/blog/api/blog/v1/blog_http.pb.go
  5. 4
      examples/helloworld/helloworld/helloworld_http.pb.go
  6. 4
      examples/traces/api/message/message_http.pb.go
  7. 4
      examples/traces/api/user/user_http.pb.go
  8. 12
      internal/testproto/echo_service_http.pb.go
  9. 34
      transport/http/binding/encode.go
  10. 8
      transport/http/binding/proto_test.go

@ -15,7 +15,7 @@ import (
// is compatible with the kratos package it is being compiled against.
var _ = new(context.Context)
var _ = new(transport.Transporter)
var _ = binding.EncodeVars
var _ = binding.EncodeURL
const _ = http.SupportPackageIsVersion1
@ -86,7 +86,7 @@ func NewMetadataHTTPClient(client *http.Client) MetadataHTTPClient {
func (c *MetadataHTTPClientImpl) GetServiceDesc(ctx context.Context, in *GetServiceDescRequest, opts ...http.CallOption) (*GetServiceDescReply, error) {
var out GetServiceDescReply
path := binding.EncodeVars("/services/{name}", in, true)
path := binding.EncodeURL("/services/{name}", in, true)
opts = append(opts, http.Operation("/kratos.api.Metadata/GetServiceDesc"))
err := c.cc.Invoke(ctx, "GET", path, nil, &out, opts...)
if err != nil {
@ -97,7 +97,7 @@ func (c *MetadataHTTPClientImpl) GetServiceDesc(ctx context.Context, in *GetServ
func (c *MetadataHTTPClientImpl) ListServices(ctx context.Context, in *ListServicesRequest, opts ...http.CallOption) (*ListServicesReply, error) {
var out ListServicesReply
path := binding.EncodeVars("/services", in, true)
path := binding.EncodeURL("/services", in, true)
opts = append(opts, http.Operation("/kratos.api.Metadata/ListServices"))
err := c.cc.Invoke(ctx, "GET", path, nil, &out, opts...)
if err != nil {

@ -45,7 +45,7 @@ func generateFileContent(gen *protogen.Plugin, file *protogen.File, g *protogen.
g.P("// is compatible with the kratos package it is being compiled against.")
g.P("var _ = new(", contextPackage.Ident("Context"), ")")
g.P("var _ = new(", transportPackage.Ident("Transporter"), ")")
g.P("var _ = ", bindingPackage.Ident("EncodeVars"))
g.P("var _ = ", bindingPackage.Ident("EncodeURL"))
g.P("const _ = ", transportHTTPPackage.Ident("SupportPackageIsVersion1"))
g.P()

@ -65,7 +65,7 @@ func New{{.ServiceType}}HTTPClient (client *http.Client) {{.ServiceType}}HTTPCli
{{range .MethodSets}}
func (c *{{$svrType}}HTTPClientImpl) {{.Name}}(ctx context.Context, in *{{.Request}}, opts ...http.CallOption) (*{{.Reply}}, error) {
var out {{.Reply}}
path := binding.EncodeVars("{{.Path}}", in, {{.IsQuery}})
path := binding.EncodeURL("{{.Path}}", in, {{.IsQuery}})
opts = append(opts, http.Operation("/{{$svrName}}/{{.Name}}"))
{{if .HasBody -}}
err := c.cc.Invoke(ctx, "{{.Method}}", path, in{{.Body}}, &out{{.ResponseBody}}, opts...)

@ -15,7 +15,7 @@ import (
// is compatible with the kratos package it is being compiled against.
var _ = new(context.Context)
var _ = new(transport.Transporter)
var _ = binding.EncodeVars
var _ = binding.EncodeURL
const _ = http.SupportPackageIsVersion1
@ -158,7 +158,7 @@ func NewBlogServiceHTTPClient(client *http.Client) BlogServiceHTTPClient {
func (c *BlogServiceHTTPClientImpl) CreateArticle(ctx context.Context, in *CreateArticleRequest, opts ...http.CallOption) (*CreateArticleReply, error) {
var out CreateArticleReply
path := binding.EncodeVars("/v1/article/", in, false)
path := binding.EncodeURL("/v1/article/", in, false)
opts = append(opts, http.Operation("/blog.api.v1.BlogService/CreateArticle"))
err := c.cc.Invoke(ctx, "POST", path, in, &out, opts...)
if err != nil {
@ -169,7 +169,7 @@ func (c *BlogServiceHTTPClientImpl) CreateArticle(ctx context.Context, in *Creat
func (c *BlogServiceHTTPClientImpl) DeleteArticle(ctx context.Context, in *DeleteArticleRequest, opts ...http.CallOption) (*DeleteArticleReply, error) {
var out DeleteArticleReply
path := binding.EncodeVars("/v1/article/{id}", in, false)
path := binding.EncodeURL("/v1/article/{id}", in, false)
opts = append(opts, http.Operation("/blog.api.v1.BlogService/DeleteArticle"))
err := c.cc.Invoke(ctx, "DELETE", path, nil, &out, opts...)
if err != nil {
@ -180,7 +180,7 @@ func (c *BlogServiceHTTPClientImpl) DeleteArticle(ctx context.Context, in *Delet
func (c *BlogServiceHTTPClientImpl) GetArticle(ctx context.Context, in *GetArticleRequest, opts ...http.CallOption) (*GetArticleReply, error) {
var out GetArticleReply
path := binding.EncodeVars("/v1/article/{id}", in, true)
path := binding.EncodeURL("/v1/article/{id}", in, true)
opts = append(opts, http.Operation("/blog.api.v1.BlogService/GetArticle"))
err := c.cc.Invoke(ctx, "GET", path, nil, &out, opts...)
if err != nil {
@ -191,7 +191,7 @@ func (c *BlogServiceHTTPClientImpl) GetArticle(ctx context.Context, in *GetArtic
func (c *BlogServiceHTTPClientImpl) ListArticle(ctx context.Context, in *ListArticleRequest, opts ...http.CallOption) (*ListArticleReply, error) {
var out ListArticleReply
path := binding.EncodeVars("/v1/article/", in, true)
path := binding.EncodeURL("/v1/article/", in, true)
opts = append(opts, http.Operation("/blog.api.v1.BlogService/ListArticle"))
err := c.cc.Invoke(ctx, "GET", path, nil, &out, opts...)
if err != nil {
@ -202,7 +202,7 @@ func (c *BlogServiceHTTPClientImpl) ListArticle(ctx context.Context, in *ListArt
func (c *BlogServiceHTTPClientImpl) UpdateArticle(ctx context.Context, in *UpdateArticleRequest, opts ...http.CallOption) (*UpdateArticleReply, error) {
var out UpdateArticleReply
path := binding.EncodeVars("/v1/article/{id}", in, false)
path := binding.EncodeURL("/v1/article/{id}", in, false)
opts = append(opts, http.Operation("/blog.api.v1.BlogService/UpdateArticle"))
err := c.cc.Invoke(ctx, "PUT", path, in, &out, opts...)
if err != nil {

@ -15,7 +15,7 @@ import (
// is compatible with the kratos package it is being compiled against.
var _ = new(context.Context)
var _ = new(transport.Transporter)
var _ = binding.EncodeVars
var _ = binding.EncodeURL
const _ = http.SupportPackageIsVersion1
@ -64,7 +64,7 @@ func NewGreeterHTTPClient(client *http.Client) GreeterHTTPClient {
func (c *GreeterHTTPClientImpl) SayHello(ctx context.Context, in *HelloRequest, opts ...http.CallOption) (*HelloReply, error) {
var out HelloReply
path := binding.EncodeVars("/helloworld/{name}", in, true)
path := binding.EncodeURL("/helloworld/{name}", in, true)
opts = append(opts, http.Operation("/helloworld.Greeter/SayHello"))
err := c.cc.Invoke(ctx, "GET", path, nil, &out, opts...)
if err != nil {

@ -15,7 +15,7 @@ import (
// is compatible with the kratos package it is being compiled against.
var _ = new(context.Context)
var _ = new(transport.Transporter)
var _ = binding.EncodeVars
var _ = binding.EncodeURL
const _ = http.SupportPackageIsVersion1
@ -64,7 +64,7 @@ func NewMessageServiceHTTPClient(client *http.Client) MessageServiceHTTPClient {
func (c *MessageServiceHTTPClientImpl) GetUserMessage(ctx context.Context, in *GetUserMessageRequest, opts ...http.CallOption) (*GetUserMessageReply, error) {
var out GetUserMessageReply
path := binding.EncodeVars("/v1/message/user/{id}/{count}", in, true)
path := binding.EncodeURL("/v1/message/user/{id}/{count}", in, true)
opts = append(opts, http.Operation("/api.message.v1.MessageService/GetUserMessage"))
err := c.cc.Invoke(ctx, "GET", path, in, &out, opts...)
if err != nil {

@ -15,7 +15,7 @@ import (
// is compatible with the kratos package it is being compiled against.
var _ = new(context.Context)
var _ = new(transport.Transporter)
var _ = binding.EncodeVars
var _ = binding.EncodeURL
const _ = http.SupportPackageIsVersion1
@ -64,7 +64,7 @@ func NewUserHTTPClient(client *http.Client) UserHTTPClient {
func (c *UserHTTPClientImpl) GetMyMessages(ctx context.Context, in *GetMyMessagesRequest, opts ...http.CallOption) (*GetMyMessagesReply, error) {
var out GetMyMessagesReply
path := binding.EncodeVars("/v1/user/get/message/{count}", in, true)
path := binding.EncodeURL("/v1/user/get/message/{count}", in, true)
opts = append(opts, http.Operation("/api.user.v1.User/GetMyMessages"))
err := c.cc.Invoke(ctx, "GET", path, in, &out, opts...)
if err != nil {

@ -15,7 +15,7 @@ import (
// is compatible with the kratos package it is being compiled against.
var _ = new(context.Context)
var _ = new(transport.Transporter)
var _ = binding.EncodeVars
var _ = binding.EncodeURL
const _ = http.SupportPackageIsVersion1
@ -247,7 +247,7 @@ func NewEchoServiceHTTPClient(client *http.Client) EchoServiceHTTPClient {
func (c *EchoServiceHTTPClientImpl) Echo(ctx context.Context, in *SimpleMessage, opts ...http.CallOption) (*SimpleMessage, error) {
var out SimpleMessage
path := binding.EncodeVars("/v1/example/echo/{id}", in, false)
path := binding.EncodeURL("/v1/example/echo/{id}", in, false)
opts = append(opts, http.Operation("/testproto.EchoService/Echo"))
err := c.cc.Invoke(ctx, "POST", path, nil, &out, opts...)
if err != nil {
@ -258,7 +258,7 @@ func (c *EchoServiceHTTPClientImpl) Echo(ctx context.Context, in *SimpleMessage,
func (c *EchoServiceHTTPClientImpl) EchoBody(ctx context.Context, in *SimpleMessage, opts ...http.CallOption) (*SimpleMessage, error) {
var out SimpleMessage
path := binding.EncodeVars("/v1/example/echo_body", in, false)
path := binding.EncodeURL("/v1/example/echo_body", in, false)
opts = append(opts, http.Operation("/testproto.EchoService/EchoBody"))
err := c.cc.Invoke(ctx, "POST", path, in, &out, opts...)
if err != nil {
@ -269,7 +269,7 @@ func (c *EchoServiceHTTPClientImpl) EchoBody(ctx context.Context, in *SimpleMess
func (c *EchoServiceHTTPClientImpl) EchoDelete(ctx context.Context, in *SimpleMessage, opts ...http.CallOption) (*SimpleMessage, error) {
var out SimpleMessage
path := binding.EncodeVars("/v1/example/echo_delete/{id}/{num}", in, false)
path := binding.EncodeURL("/v1/example/echo_delete/{id}/{num}", in, false)
opts = append(opts, http.Operation("/testproto.EchoService/EchoDelete"))
err := c.cc.Invoke(ctx, "DELETE", path, nil, &out, opts...)
if err != nil {
@ -280,7 +280,7 @@ func (c *EchoServiceHTTPClientImpl) EchoDelete(ctx context.Context, in *SimpleMe
func (c *EchoServiceHTTPClientImpl) EchoPatch(ctx context.Context, in *DynamicMessageUpdate, opts ...http.CallOption) (*DynamicMessageUpdate, error) {
var out DynamicMessageUpdate
path := binding.EncodeVars("/v1/example/echo_patch", in, false)
path := binding.EncodeURL("/v1/example/echo_patch", in, false)
opts = append(opts, http.Operation("/testproto.EchoService/EchoPatch"))
err := c.cc.Invoke(ctx, "PATCH", path, in.Body, &out, opts...)
if err != nil {
@ -291,7 +291,7 @@ func (c *EchoServiceHTTPClientImpl) EchoPatch(ctx context.Context, in *DynamicMe
func (c *EchoServiceHTTPClientImpl) EchoResponseBody(ctx context.Context, in *DynamicMessageUpdate, opts ...http.CallOption) (*DynamicMessageUpdate, error) {
var out DynamicMessageUpdate
path := binding.EncodeVars("/v1/example/echo_response_body", in, false)
path := binding.EncodeURL("/v1/example/echo_response_body", in, false)
opts = append(opts, http.Operation("/testproto.EchoService/EchoResponseBody"))
err := c.cc.Invoke(ctx, "POST", path, in, &out.Body, opts...)
if err != nil {

@ -18,8 +18,8 @@ import (
"google.golang.org/protobuf/types/known/wrapperspb"
)
// EncodeVars binds proto message to url path
func EncodeVars(pathPattern string, msg proto.Message, isQuery bool) string {
// EncodeURL encode proto message to url path.
func EncodeURL(pathPattern string, msg proto.Message, needQuery bool) string {
if msg == nil || (reflect.ValueOf(msg).Kind() == reflect.Ptr && reflect.ValueOf(msg).IsNil()) {
return pathPattern
}
@ -40,8 +40,8 @@ func EncodeVars(pathPattern string, msg proto.Message, isQuery bool) string {
}
return in
})
if isQuery {
u, err := encodeQuery(msg)
if needQuery {
u, err := EncodeQuery(msg)
if err == nil && len(u) > 0 {
for key := range pathParams {
delete(u, key)
@ -52,10 +52,22 @@ func EncodeVars(pathPattern string, msg proto.Message, isQuery bool) string {
}
}
}
return path
}
// EncodeQuery encode proto message to url query.
func EncodeQuery(msg proto.Message) (url.Values, error) {
if msg == nil || (reflect.ValueOf(msg).Kind() == reflect.Ptr && reflect.ValueOf(msg).IsNil()) {
return url.Values{}, nil
}
u := make(url.Values)
err := encodeByField(u, "", msg.ProtoReflect())
if err != nil {
return nil, err
}
return u, nil
}
func getValueByField(v protoreflect.Message, fieldPath []string) (string, error) {
var fd protoreflect.FieldDescriptor
for i, fieldName := range fieldPath {
@ -77,18 +89,6 @@ func getValueByField(v protoreflect.Message, fieldPath []string) (string, error)
return encodeField(fd, v.Get(fd))
}
func encodeQuery(msg proto.Message) (url.Values, error) {
if msg == nil || (reflect.ValueOf(msg).Kind() == reflect.Ptr && reflect.ValueOf(msg).IsNil()) {
return url.Values{}, nil
}
u := make(url.Values)
err := encodeByField(u, "", msg.ProtoReflect())
if err != nil {
return nil, err
}
return u, nil
}
func encodeByField(u url.Values, path string, v protoreflect.Message) error {
for i := 0; i < v.Descriptor().Fields().Len(); i++ {
fd := v.Descriptor().Fields().Get(i)

@ -6,25 +6,25 @@ import (
)
func TestProtoPath(t *testing.T) {
url := EncodeVars("http://helloworld.Greeter/helloworld/{name}/sub/{sub.name}", &HelloRequest{Name: "test", Sub: &Sub{Name: "2233!!!"}}, false)
url := EncodeURL("http://helloworld.Greeter/helloworld/{name}/sub/{sub.name}", &HelloRequest{Name: "test", Sub: &Sub{Name: "2233!!!"}}, false)
fmt.Println(url)
if url != `http://helloworld.Greeter/helloworld/test/sub/2233!!!` {
t.Fatalf("proto path not expected!actual: %s ", url)
}
url = EncodeVars("http://helloworld.Greeter/helloworld/sub", &HelloRequest{Name: "test", Sub: &Sub{Name: "2233!!!"}}, false)
url = EncodeURL("http://helloworld.Greeter/helloworld/sub", &HelloRequest{Name: "test", Sub: &Sub{Name: "2233!!!"}}, false)
fmt.Println(url)
if url != `http://helloworld.Greeter/helloworld/sub` {
t.Fatalf("proto path not expected!actual: %s ", url)
}
url = EncodeVars("http://helloworld.Greeter/helloworld/{name}/sub/{sub.name}", &HelloRequest{Name: "test"}, false)
url = EncodeURL("http://helloworld.Greeter/helloworld/{name}/sub/{sub.name}", &HelloRequest{Name: "test"}, false)
fmt.Println(url)
if url != `http://helloworld.Greeter/helloworld/test/sub/` {
t.Fatalf("proto path not expected!actual: %s ", url)
}
url = EncodeVars("http://helloworld.Greeter/helloworld/{name}/sub/{sub.name33}", &HelloRequest{Name: "test"}, false)
url = EncodeURL("http://helloworld.Greeter/helloworld/{name}/sub/{sub.name33}", &HelloRequest{Name: "test"}, false)
fmt.Println(url)
if url != `http://helloworld.Greeter/helloworld/test/sub/{sub.name33}` {
t.Fatalf("proto path not expected!actual: %s ", url)

Loading…
Cancel
Save