summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeff Carr <[email protected]>2025-10-09 11:07:29 -0500
committerJeff Carr <[email protected]>2025-10-09 13:15:55 -0500
commit4b311fe536849b3ceec682bd5df1c3c3d5ea72e4 (patch)
tree1ec9174347725d94d6652abd04392a3c514436ba
parent31f6b7a851cf1000700ecffb98f205c89f336c0a (diff)
dump out all the strings in a protobuf
-rw-r--r--findFilename.go38
1 files changed, 38 insertions, 0 deletions
diff --git a/findFilename.go b/findFilename.go
index 5e81974..658e068 100644
--- a/findFilename.go
+++ b/findFilename.go
@@ -121,3 +121,41 @@ func GetString(pb proto.Message, varname string) (string, error) {
// 7. Convert the protoreflect.Value to a native Go string.
return value.String(), nil
}
+
+func PrintStrings(pb proto.Message) {
+ // 1. Get the protoreflect.Message interface.
+ msg := pb.ProtoReflect()
+
+ // It's good practice to check if the message is valid.
+ if !msg.IsValid() {
+ fmt.Printf("Error: Provided protobuf message is not valid.")
+ return
+ }
+
+ // 2. Get the message's descriptor.
+ descriptor := msg.Descriptor()
+ fmt.Printf("--- Listing String Fields in [%s] ---\n", descriptor.FullName())
+
+ // 3. Get the collection of all field descriptors for this message.
+ fields := descriptor.Fields()
+
+ // 4. Iterate over all the fields.
+ for i := 0; i < fields.Len(); i++ {
+ // Get the descriptor for the field at the current index.
+ fieldDescriptor := fields.Get(i)
+
+ // 5. Check if the field's kind is a string.
+ if fieldDescriptor.Kind() == protoreflect.StringKind {
+ // 6. If it is a string, get its name and value.
+ fieldName := fieldDescriptor.Name()
+ value := msg.Get(fieldDescriptor).String()
+
+ // 7. Print the formatted result.
+ // We add a check to see if the field is populated. An empty string
+ // is a valid value, but you might only want to see set fields.
+ if msg.Has(fieldDescriptor) {
+ fmt.Printf(" %s: \"%s\"\n", fieldName, value)
+ }
+ }
+ }
+}