diff options
| author | Jeff Carr <[email protected]> | 2025-10-10 11:53:16 -0500 |
|---|---|---|
| committer | Jeff Carr <[email protected]> | 2025-10-10 16:22:21 -0500 |
| commit | 2b2c2960641a8ad47682bfc362aeea0fdaeb8e0b (patch) | |
| tree | ae72bdb4a0256926fbf4b06116804d2a2d8572c0 | |
| parent | 32d097405950217bf0597e4ec23d22160f0a2b22 (diff) | |
PB DebInfo lookup
| -rw-r--r-- | populatePackagePB.go | 88 |
1 files changed, 88 insertions, 0 deletions
diff --git a/populatePackagePB.go b/populatePackagePB.go index ad39069..34127a4 100644 --- a/populatePackagePB.go +++ b/populatePackagePB.go @@ -1,6 +1,9 @@ package debian import ( + "errors" + "fmt" + "google.golang.org/protobuf/proto" "google.golang.org/protobuf/reflect/protoreflect" @@ -33,3 +36,88 @@ func SetString(pb proto.Message, varname string, varvalue string) (bool, error) // 7. Convert the protoreflect.Value to a native Go string. return true, nil } + +func SetDebInfoString(pb proto.Message, varname string, varvalue string) error { + // 1. Get the reflection interface for the top-level message (e.g., Package). + msg := pb.ProtoReflect() + descriptor := msg.Descriptor() + + // 2. Find the FieldDescriptor for the nested message field (named "debInfo"). + // This is the corrected line: call .Fields().ByName(). + debInfoFieldDescriptor := descriptor.Fields().ByName("debInfo") + if debInfoFieldDescriptor == nil { + return fmt.Errorf("field 'debInfo' not found in message %s", descriptor.FullName()) + } + + // 3. From the field descriptor, get the MessageDescriptor for the DebInfo type. + // This gives us the "schema" for the DebInfo message. + debInfoMsgDescriptor := debInfoFieldDescriptor.Message() + if debInfoMsgDescriptor == nil { + // This would happen if the 'debInfo' field was not a message type. + // return ErrFieldNotMessage + return errors.New("debInfo failed to turn into a message?") + } + + // 4. Now, find the FieldDescriptor for the target string field (varname) + // WITHIN the DebInfo message's schema. + fieldName := protoreflect.Name(varname) + fieldDescriptor := debInfoMsgDescriptor.Fields().ByName(fieldName) + if fieldDescriptor == nil { + return fmt.Errorf("field '%s' not found in nested message 'debInfo'", varname) + } + + // 5. Validate that the target field is a string. + if fieldDescriptor.Kind() != protoreflect.StringKind { + return config.ErrProtoVarNotString + } + + // 6. CRITICAL STEP: Get a mutable reference to the actual nested DebInfo message. + // msg.Mutable() is perfect for this. It will: + // a) Get the existing DebInfo message if it's already been set. + // b) Create a NEW, empty DebInfo message if it's currently nil. + // Then, we get the reflection interface for that nested message. + debInfoMsg := msg.Mutable(debInfoFieldDescriptor).Message() + + // 7. Prepare the value and SET it on the nested message. + valueToSet := protoreflect.ValueOfString(varvalue) + debInfoMsg.Set(fieldDescriptor, valueToSet) + + return nil +} + +/* + 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. + + debInfo := protoreflect.Name("debInfo") + debInfoDescriptor := descriptor.ByName(debInfo) + // debInfoMsgDescriptor := debInfoDescriptor.Descriptor() // Get the message's descriptor, which contains metadata about its fields. + + if debInfoDescriptor == nil { + panic("DEB INFO COULD NOT BE FOUND") + // return false, config.ErrProtoNoVarName + } + + fieldName := protoreflect.Name(varname) + fieldDescriptor := debInfoDescriptor.Fields().ByName(fieldName) + + if fieldDescriptor == nil { + return false, config.ErrProtoNoVarName + } + + if fieldDescriptor.Kind() != protoreflect.StringKind { + // The field exists but is not a string, so we can't return it as one. + return false, config.ErrProtoVarNotString + } + + valueToSet := protoreflect.ValueOfString(varvalue) + + // 6. If the field exists and is a string, get its value. + // The value is returned as a protoreflect.Value. + msg.Set(fieldDescriptor, valueToSet) + + // 7. Convert the protoreflect.Value to a native Go string. + return true, nil +} +*/ |
