Skip to content

Commit

Permalink
Fix to parse protoreflect.Message instead of parsing protoreflect.Mes…
Browse files Browse the repository at this point in the history
…sage.String() (#5436)

Signed-off-by: Yoshiki Fujikane <[email protected]>
  • Loading branch information
ffjlabo authored Dec 20, 2024
1 parent 314ce3a commit 61054dd
Showing 1 changed file with 18 additions and 16 deletions.
34 changes: 18 additions & 16 deletions tool/codegen/protoc-gen-auth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"fmt"
"html/template"
"sort"
"strconv"
"strings"

"google.golang.org/protobuf/compiler/protogen"
Expand Down Expand Up @@ -120,26 +119,29 @@ func generateMethods(extTypes *protoregistry.Types, ms []*protogen.Method) ([]*M

method := &Method{Name: m.GoName}
opts.ProtoReflect().Range(func(fd protoreflect.FieldDescriptor, v protoreflect.Value) bool {
if !fd.IsExtension() || fd.Name() != methodOptionsRBAC || v.String() == "" {
if !fd.IsExtension() || fd.Name() != methodOptionsRBAC || fd.Kind() != protoreflect.MessageKind {
return true
}

vs := strings.Split(v.String(), " ")
for _, v := range vs {
kv := strings.SplitN(v, ":", 2)
key, value := kv[0], kv[1]

switch key {
case keyMethodOptionsRBACResouce:
method.Resource = value
case keyMethodOptionsRBACAction:
method.Action = value
case keyMethodOptionsRBACIgnored:
if v, err := strconv.ParseBool(value); err == nil {
method.Ignored = v
v.Message().Range(func(fd protoreflect.FieldDescriptor, v protoreflect.Value) bool {
switch fd.Kind() {
case protoreflect.EnumKind:
if fd.Name() == keyMethodOptionsRBACResouce {
method.Resource = string(fd.Enum().Values().ByNumber(v.Enum()).Name())
}

if fd.Name() == keyMethodOptionsRBACAction {
method.Action = string(fd.Enum().Values().ByNumber(v.Enum()).Name())
}
case protoreflect.BoolKind:
if fd.Name() == keyMethodOptionsRBACIgnored {
method.Ignored = v.Bool()
}
}
}

return true
})

return true
})

Expand Down

0 comments on commit 61054dd

Please sign in to comment.