From de50b15bf685dda8c7adb47e43de8f41ed94c1d6 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Sun, 12 Oct 2025 17:50:28 -0500 Subject: reorder the fields --- printDebInfo.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) (limited to 'printDebInfo.go') diff --git a/printDebInfo.go b/printDebInfo.go index e87a3d2..267b03c 100644 --- a/printDebInfo.go +++ b/printDebInfo.go @@ -182,3 +182,48 @@ func main() { ") } */ + +// func printDebInfoStrings(pb proto.Message, fallback bool) (string, error) { +func GetDebInfoFields(pb proto.Message) ([]string, error) { + var debInfo []string + // 1. Get the reflection interface for the top-level message. + msg := pb.ProtoReflect() + descriptor := msg.Descriptor() + + // 2. Find the FieldDescriptor for the nested "debInfo" message. + // Note: Protobuf field names like "deb_info" are often canonicalized + // to "debInfo" in Go code, but reflection should use the name from the .proto file. + // We'll check for both common variations for robustness. + + debInfoFieldDesc := descriptor.Fields().ByName("debInfo") + if debInfoFieldDesc == nil { + debInfoFieldDesc = descriptor.Fields().ByName("deb_info") + } + + if debInfoFieldDesc == nil { + // return controlfile, fmt.Errorf("field 'debInfo' or 'deb_info' not found in message %s", descriptor.FullName()) + return nil, fmt.Errorf("field 'debInfo' or 'deb_info' not found in message %s", descriptor.FullName()) + } + + // 3. Get the actual nested message object from the parent. + debInfoValue := msg.Get(debInfoFieldDesc) + debInfoMsg := debInfoValue.Message() + + // 4. Check if the nested message is valid and has been set. + if !debInfoMsg.IsValid() { + // fmt.Printf("--- Field '%s' in [%s] is not set. --- \n", debInfoFieldDesc.Name(), descriptor.FullName()) + return nil, fmt.Errorf("debInfoMsg.InValid()") + } + + // 5. Now, iterate over the fields of the nested debInfo message. + debInfoDescriptor := debInfoMsg.Descriptor() + fields := debInfoDescriptor.Fields() + + for i := 0; i < fields.Len(); i++ { + fieldDesc := fields.Get(i) + debInfo = append(debInfo, string(fieldDesc.Name())) + // debInfo = append(debInfo, fieldDesc.Name()) + } + + return debInfo, nil +} -- cgit v1.2.3