Skip to content

Commit

Permalink
feat: add permissionCode
Browse files Browse the repository at this point in the history
  • Loading branch information
fynntang committed Sep 2, 2023
1 parent 62923bf commit 55fbd36
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
15 changes: 14 additions & 1 deletion protoc-gen-go-http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,14 +206,27 @@ 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()),
Num: methodSets[m.GoName],
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,
Expand Down
13 changes: 13 additions & 0 deletions protoc-gen-go-http/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Expand Down Expand Up @@ -109,6 +121,7 @@ type methodDesc struct {
Reply string
Comment string
OriginalComment string
PermissionCodes map[string]string
// http_rule
Path string
Method string
Expand Down

0 comments on commit 55fbd36

Please sign in to comment.