From 00ce05a205026f5ef0c6b223d2b44a665586f5d5 Mon Sep 17 00:00:00 2001 From: FynnTang Date: Mon, 4 Sep 2023 14:07:54 +0800 Subject: [PATCH] feat: add permissionCode --- protoc-gen-go-http/http.go | 17 +++++++++++------ protoc-gen-go-http/template.go | 31 +++++++++++++++---------------- 2 files changed, 26 insertions(+), 22 deletions(-) diff --git a/protoc-gen-go-http/http.go b/protoc-gen-go-http/http.go index 7dcbc6d..49fe4bd 100644 --- a/protoc-gen-go-http/http.go +++ b/protoc-gen-go-http/http.go @@ -85,9 +85,13 @@ func genService(_ *protogen.Plugin, file *protogen.File, g *protogen.GeneratedFi rule, ok := proto.GetExtension(method.Desc.Options(), annotations.E_Http).(*annotations.HttpRule) if rule != nil && ok { for _, bind := range rule.AdditionalBindings { - sd.Methods = append(sd.Methods, buildHTTPRule(g, method, bind)) + md := buildHTTPRule(g, method, bind) + sd.Methods = append(sd.Methods, md) + sd.PermissionCodes = append(sd.PermissionCodes, md.PermissionCodes...) } - sd.Methods = append(sd.Methods, buildHTTPRule(g, method, rule)) + md := buildHTTPRule(g, method, rule) + sd.Methods = append(sd.Methods, md) + sd.PermissionCodes = append(sd.PermissionCodes, md.PermissionCodes...) } else if !omitempty { path := fmt.Sprintf("/%s/%s", service.Desc.FullName(), method.Desc.Name()) sd.Methods = append(sd.Methods, buildMethodDesc(g, method, http.MethodPost, path)) @@ -207,14 +211,15 @@ func buildMethodDesc(g *protogen.GeneratedFile, m *protogen.Method, method, path comment = "// " + m.GoName + strings.TrimPrefix(strings.TrimSuffix(comment, "\n"), "//") } originalComment := strings.ReplaceAll(comment, string(m.Desc.Name())+" ", "") - permissionCodes := make(map[string]string) + + var permissionCodes []*permissionCode if permissionValues := strings.Split(originalComment, "|"); len(permissionValues) > 1 { for _, v := range strings.Split(strings.Split(strings.Trim(permissionValues[1], " "), " ")[0], "/") { - permissionCode := "" + permissionCodeName := "" for _, code := range strings.Split(v, ":") { - permissionCode = permissionCode + strings.ToUpper(code[:1]) + code[1:] + permissionCodeName = permissionCodeName + strings.ToUpper(code[:1]) + code[1:] } - permissionCodes[permissionCode] = v + permissionCodes = append(permissionCodes, &permissionCode{Name: permissionCodeName, Code: v}) } } diff --git a/protoc-gen-go-http/template.go b/protoc-gen-go-http/template.go index aff1254..da17e60 100644 --- a/protoc-gen-go-http/template.go +++ b/protoc-gen-go-http/template.go @@ -10,17 +10,10 @@ var httpTemplate = ` {{$svrType := .ServiceType}} {{$svrName := .ServiceName}} - - -{{- range .MethodSets}} - {{ $pclen := len .PermissionCodes }} - {{ if gt $pclen 1 }} - {{- range $k,$v := .PermissionCodes}} - const PermissionCode{{$k}} = "{{$v}}" - {{- end}} - {{ end }} +{{- range .PermissionCodes}} + const PermissionCode{{.Name}} = "{{.Code}}" {{- end}} - +// {{- range .MethodSets}} const Route{{$svrType}}{{.OriginalName}} = "{{.Path}}" {{- if ne .OriginalComment ""}} {{.OriginalComment}} {{.Method}}{{- end}} @@ -104,12 +97,18 @@ func _{{$svrType}}_{{.Name}}{{.Num}}_HTTP_Handler(srv {{$svrType}}HTTPServer) gi {{end}} ` +type permissionCode struct { + Name string + Code string +} + type serviceDesc struct { - ServiceType string // Greeter - ServiceName string // helloworld.Greeter - Metadata string // api/helloworld/helloworld.proto - Methods []*methodDesc - MethodSets map[string]*methodDesc + ServiceType string // Greeter + ServiceName string // helloworld.Greeter + Metadata string // api/helloworld/helloworld.proto + Methods []*methodDesc + MethodSets map[string]*methodDesc + PermissionCodes []*permissionCode } type methodDesc struct { @@ -121,7 +120,7 @@ type methodDesc struct { Reply string Comment string OriginalComment string - PermissionCodes map[string]string + PermissionCodes []*permissionCode // http_rule Path string Method string