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 4, 2023
1 parent 55fbd36 commit 00ce05a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 22 deletions.
17 changes: 11 additions & 6 deletions protoc-gen-go-http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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})
}
}

Expand Down
31 changes: 15 additions & 16 deletions protoc-gen-go-http/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down

0 comments on commit 00ce05a

Please sign in to comment.