feat:add warning when http path filed use map or list (#1526)

* feat:add warning when http path filed use map or list
pull/1533/head
海雨 3 years ago committed by GitHub
parent a2f53128cf
commit 23a96a3d63
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 15
      cmd/protoc-gen-go-http/http.go

@ -5,6 +5,8 @@ import (
"os" "os"
"strings" "strings"
"google.golang.org/protobuf/reflect/protoreflect"
"github.com/go-kratos/kratos/v2" "github.com/go-kratos/kratos/v2"
"google.golang.org/genproto/googleapis/api/annotations" "google.golang.org/genproto/googleapis/api/annotations"
"google.golang.org/protobuf/compiler/protogen" "google.golang.org/protobuf/compiler/protogen"
@ -155,6 +157,17 @@ func buildHTTPRule(g *protogen.GeneratedFile, m *protogen.Method, rule *annotati
func buildMethodDesc(g *protogen.GeneratedFile, m *protogen.Method, method, path string) *methodDesc { func buildMethodDesc(g *protogen.GeneratedFile, m *protogen.Method, method, path string) *methodDesc {
defer func() { methodSets[m.GoName]++ }() defer func() { methodSets[m.GoName]++ }()
vars := buildPathVars(m, path)
fields := m.Input.Desc.Fields()
for _, v := range vars {
fd := fields.ByName(protoreflect.Name(v))
if fd.IsMap() {
fmt.Fprintf(os.Stderr, "\u001B[31mWARN\u001B[m: The field in path:'%s' shouldn't be a map.\n", v)
}
if fd.IsList() {
fmt.Fprintf(os.Stderr, "\u001B[31mWARN\u001B[m: The field in path:'%s' shouldn't be a list.\n", v)
}
}
return &methodDesc{ return &methodDesc{
Name: m.GoName, Name: m.GoName,
Num: methodSets[m.GoName], Num: methodSets[m.GoName],
@ -162,7 +175,7 @@ func buildMethodDesc(g *protogen.GeneratedFile, m *protogen.Method, method, path
Reply: g.QualifiedGoIdent(m.Output.GoIdent), Reply: g.QualifiedGoIdent(m.Output.GoIdent),
Path: path, Path: path,
Method: method, Method: method,
HasVars: len(buildPathVars(m, path)) > 0, HasVars: len(vars) > 0,
} }
} }

Loading…
Cancel
Save