From 23a96a3d63546c6ac5b5d4eb4a5af22d14bb21a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B5=B7=E9=9B=A8?= <99347745@qq.com> Date: Thu, 30 Sep 2021 14:12:27 +0800 Subject: [PATCH] feat:add warning when http path filed use map or list (#1526) * feat:add warning when http path filed use map or list --- cmd/protoc-gen-go-http/http.go | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/cmd/protoc-gen-go-http/http.go b/cmd/protoc-gen-go-http/http.go index 25c443209..4d2b4dd67 100644 --- a/cmd/protoc-gen-go-http/http.go +++ b/cmd/protoc-gen-go-http/http.go @@ -5,6 +5,8 @@ import ( "os" "strings" + "google.golang.org/protobuf/reflect/protoreflect" + "github.com/go-kratos/kratos/v2" "google.golang.org/genproto/googleapis/api/annotations" "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 { 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{ Name: 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), Path: path, Method: method, - HasVars: len(buildPathVars(m, path)) > 0, + HasVars: len(vars) > 0, } }