From 61054dd3c3cddfb6d5f5e921cf50a6616eee09f4 Mon Sep 17 00:00:00 2001 From: Yoshiki Fujikane <40124947+ffjlabo@users.noreply.github.com> Date: Fri, 20 Dec 2024 10:17:38 +0900 Subject: [PATCH] Fix to parse protoreflect.Message instead of parsing protoreflect.Message.String() (#5436) Signed-off-by: Yoshiki Fujikane --- tool/codegen/protoc-gen-auth/main.go | 34 +++++++++++++++------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/tool/codegen/protoc-gen-auth/main.go b/tool/codegen/protoc-gen-auth/main.go index 67f5c55087..6238e1cdb2 100644 --- a/tool/codegen/protoc-gen-auth/main.go +++ b/tool/codegen/protoc-gen-auth/main.go @@ -19,7 +19,6 @@ import ( "fmt" "html/template" "sort" - "strconv" "strings" "google.golang.org/protobuf/compiler/protogen" @@ -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 })