1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
package main
import (
"fmt"
"os"
"path/filepath"
"go.wit.com/lib/config"
"go.wit.com/log"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
)
func FindFiles(lookhere string) ([]string, error) {
var allfiles []string
err := filepath.Walk(lookhere, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
allfiles = append(allfiles, path)
}
return nil
})
return allfiles, err
}
// this is terrible and I regret making this
func printDebInfoStrings(pb proto.Message, fallback bool) (string, error) {
var verbose bool
var controlfile 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())
}
// 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 controlfile, nil // Not an error, just nothing to print.
}
// 5. Now, iterate over the fields of the nested debInfo message.
debInfoDescriptor := debInfoMsg.Descriptor()
fields := debInfoDescriptor.Fields()
if verbose {
fmt.Printf("--- Listing String Fields in '%s' --- \n", debInfoFieldDesc.Name())
}
foundStrings := false
for i := 0; i < fields.Len(); i++ {
fieldDesc := fields.Get(i)
// 6. Check if the field's kind is a string.
if fieldDesc.Kind() == protoreflect.StringKind {
var value string
var fieldName protoreflect.Name
// Get the value from the debInfo message object.
value = debInfoMsg.Get(fieldDesc).String()
fieldName = fieldDesc.Name()
if value == "" {
if verbose {
log.Info(string(fieldName), "is blank")
}
newval, err := config.GetString(pb, string(fieldName))
if err != nil {
if verbose {
log.Info("GetString() failed", newval, err)
}
continue
} else {
if verbose {
log.Info("GetString() worked:", newval)
}
value = newval
}
}
if value == "" {
// still blank after backup lookup
continue
}
// Print the result.
fmt.Printf("%s: \"%s\"\n", string(fieldName), value)
controlfile += fmt.Sprintf("%s: %s\n", string(fieldName), value)
foundStrings = true
}
}
if !foundStrings {
fmt.Println(" (No string fields found)")
}
return controlfile, nil
}
|