From 55fbd36f78feb8db2140a0cb33086e382cb8dd1e Mon Sep 17 00:00:00 2001 From: FynnTang Date: Sat, 2 Sep 2023 15:34:02 +0800 Subject: [PATCH] feat: add permissionCode --- protoc-gen-go-http/http.go | 15 ++++++++++++++- protoc-gen-go-http/template.go | 13 +++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/protoc-gen-go-http/http.go b/protoc-gen-go-http/http.go index 7aa4673..7dcbc6d 100644 --- a/protoc-gen-go-http/http.go +++ b/protoc-gen-go-http/http.go @@ -206,6 +206,18 @@ func buildMethodDesc(g *protogen.GeneratedFile, m *protogen.Method, method, path if comment != "" { comment = "// " + m.GoName + strings.TrimPrefix(strings.TrimSuffix(comment, "\n"), "//") } + originalComment := strings.ReplaceAll(comment, string(m.Desc.Name())+" ", "") + permissionCodes := make(map[string]string) + if permissionValues := strings.Split(originalComment, "|"); len(permissionValues) > 1 { + for _, v := range strings.Split(strings.Split(strings.Trim(permissionValues[1], " "), " ")[0], "/") { + permissionCode := "" + for _, code := range strings.Split(v, ":") { + permissionCode = permissionCode + strings.ToUpper(code[:1]) + code[1:] + } + permissionCodes[permissionCode] = v + } + } + return &methodDesc{ Name: m.GoName, OriginalName: string(m.Desc.Name()), @@ -213,7 +225,8 @@ func buildMethodDesc(g *protogen.GeneratedFile, m *protogen.Method, method, path Request: g.QualifiedGoIdent(m.Input.GoIdent), Reply: g.QualifiedGoIdent(m.Output.GoIdent), Comment: comment, - OriginalComment: strings.ReplaceAll(comment, string(m.Desc.Name())+" ", ""), + OriginalComment: originalComment, + PermissionCodes: permissionCodes, Path: replacePathToGinPath(path), Method: method, HasVars: len(vars) > 0, diff --git a/protoc-gen-go-http/template.go b/protoc-gen-go-http/template.go index 9cf3c2c..aff1254 100644 --- a/protoc-gen-go-http/template.go +++ b/protoc-gen-go-http/template.go @@ -10,6 +10,18 @@ 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 }} +{{- end}} + + {{- range .MethodSets}} const Route{{$svrType}}{{.OriginalName}} = "{{.Path}}" {{- if ne .OriginalComment ""}} {{.OriginalComment}} {{.Method}}{{- end}} {{- end}} @@ -109,6 +121,7 @@ type methodDesc struct { Reply string Comment string OriginalComment string + PermissionCodes map[string]string // http_rule Path string Method string