diff options
Diffstat (limited to 'printDebInfo.go')
| -rw-r--r-- | printDebInfo.go | 45 |
1 files changed, 45 insertions, 0 deletions
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 +} |
