diff options
| author | Jeff Carr <[email protected]> | 2025-10-20 05:50:38 -0500 |
|---|---|---|
| committer | Jeff Carr <[email protected]> | 2025-10-20 05:50:38 -0500 |
| commit | 8a24584262a90956e012cee7b03c8c2b9f4b794c (patch) | |
| tree | 8b7d0afd04e717d7c07da5eb2894debd74ec2307 /autogenpbName.go | |
| parent | b6e93c08d601a7a6c27a0fdcdf98f6cb7dc9ccd8 (diff) | |
reworking this to make it more sane. hopefully.
Diffstat (limited to 'autogenpbName.go')
| -rw-r--r-- | autogenpbName.go | 111 |
1 files changed, 111 insertions, 0 deletions
diff --git a/autogenpbName.go b/autogenpbName.go new file mode 100644 index 0000000..f2e492c --- /dev/null +++ b/autogenpbName.go @@ -0,0 +1,111 @@ +package config + +import ( + "fmt" + + "google.golang.org/protobuf/proto" + "google.golang.org/protobuf/reflect/protoreflect" +) + +// Because you followed autogenpb's advice (you did follow it right?) you now +// win the automation lottery. +// +// for this .proto file, GetProtobufName(pb) returns "repos" +// Then, ConfigLoad(), ConfigSave(), CacheLoad() and CacheSave() +// all do exactly what is expected: +// +// Automatically work with the files: +// +// ~/.config/<appname>/repos.pb +// +// or +// +// ~/.cache/<appname/repos.pb +// +// message Repos { +// string uuid = 1; +// string version = 2; +// repeated Repo repos = 3; +func GetProtobufName(pb proto.Message) (string, error) { + // 1. Get the protoreflect.Message interface. + msg := pb.ProtoReflect() + + // 2. Get the message's descriptor. + descriptor := msg.Descriptor() + + // 3. Get all the field descriptors for the message. + fields := descriptor.Fields() + + // find string "uuid" + fieldNumber := protoreflect.FieldNumber(1) + fieldDescriptor := fields.ByNumber(fieldNumber) + + if fieldDescriptor == nil { + return "", fmt.Errorf("message %q has no field with number 1", descriptor.FullName()) + } + if fieldDescriptor.Name() != "uuid" { + return "", fmt.Errorf("message %q field(1) != 'uuid' is (%s)", descriptor.FullName(), fieldDescriptor.Name()) + } + + // find string "version" + fieldNumber = protoreflect.FieldNumber(2) + fieldDescriptor = fields.ByNumber(fieldNumber) + + if fieldDescriptor == nil { + return "", fmt.Errorf("message %q has no field(2)", descriptor.FullName()) + } + if fieldDescriptor.Name() != "version" { + return "", fmt.Errorf("message %q field(2) != 'version' is (%s)", descriptor.FullName(), fieldDescriptor.Name()) + } + + // 4. Find the field descriptor specifically by its number (3). + // This is the crucial step. + fieldNumber = protoreflect.FieldNumber(3) + fieldDescriptor = fields.ByNumber(fieldNumber) + + // 5. Add some safety checks. + if fieldDescriptor == nil { + return "", fmt.Errorf("message %q has no field with number 3", descriptor.FullName()) + } + + // Optional but recommended: verify it's a repeated field as you expect. + // IsList() is the reflection equivalent of `repeated`. + if !fieldDescriptor.IsList() { + return "", fmt.Errorf("field with number 3 (%q) is not a repeated field", fieldDescriptor.Name()) + } + + // 6. Get the name from the descriptor and return it. + // The name is of type protoreflect.Name, so we cast it to a string. + return string(fieldDescriptor.Name()), nil +} + +func GetThirdFieldName(pb proto.Message) (string, error) { + // 1. Get the protoreflect.Message interface. + msg := pb.ProtoReflect() + + // 2. Get the message's descriptor. + descriptor := msg.Descriptor() + + // 3. Get all the field descriptors for the message. + fields := descriptor.Fields() + + // 4. Find the field descriptor specifically by its number (3). + // This is the crucial step. + fieldNumber := protoreflect.FieldNumber(3) + fieldDescriptor := fields.ByNumber(fieldNumber) + + // 5. Add some safety checks. + if fieldDescriptor == nil { + return "", fmt.Errorf("message %q has no field with number 3", descriptor.FullName()) + } + + // Optional but recommended: verify it's a repeated field as you expect. + // IsList() is the reflection equivalent of `repeated`. + if !fieldDescriptor.IsList() { + return "", fmt.Errorf("field with number 3 (%q) is not a repeated field", fieldDescriptor.Name()) + } + + // 6. Get the name from the descriptor and return it. + // The name is of type protoreflect.Name, so we cast it to a string. + return string(fieldDescriptor.Name()), nil +} |
