package debian import ( "google.golang.org/protobuf/proto" "google.golang.org/protobuf/reflect/protoreflect" "go.wit.com/lib/config" ) func SetString(pb proto.Message, varname string, varvalue string) (bool, error) { 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. fieldName := protoreflect.Name(varname) fieldDescriptor := descriptor.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 }