From a86ec5e086c3394f11a8b3cc61711a0b0e047520 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Wed, 8 Oct 2025 01:23:29 -0500 Subject: start a generic pb scanner --- findFilename.go | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to 'findFilename.go') diff --git a/findFilename.go b/findFilename.go index 1160e08..1b36f38 100644 --- a/findFilename.go +++ b/findFilename.go @@ -53,7 +53,7 @@ func GetFilename(pb proto.Message) (string, error) { } // sets "Filename" if it exists in the protobuf -func SetFilename(pb proto.Message, filename string) bool { +func SetFilename(pb proto.Message, filename string) (bool, error) { msg := pb.ProtoReflect() // This is the entry point to the reflection API. descriptor := msg.Descriptor() // Get the message's descriptor, which contains metadata about its fields. @@ -62,12 +62,17 @@ func SetFilename(pb proto.Message, filename string) bool { fieldDescriptor := descriptor.Fields().ByName(fieldName) if fieldDescriptor == nil { - return false + fieldName = protoreflect.Name("filename") + fieldDescriptor = descriptor.Fields().ByName(fieldName) + } + + if fieldDescriptor == nil { + return false, fmt.Errorf("fieldDescriptor == nil") } if fieldDescriptor.Kind() != protoreflect.StringKind { // The field exists but is not a string, so we can't return it as one. - return false + return false, fmt.Errorf("The field exists but is not a string") } valueToSet := protoreflect.ValueOfString(filename) @@ -77,5 +82,5 @@ func SetFilename(pb proto.Message, filename string) bool { msg.Set(fieldDescriptor, valueToSet) // 7. Convert the protoreflect.Value to a native Go string. - return true + return true, nil } -- cgit v1.2.3