summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--findFilename.go20
-rw-r--r--load.go11
-rw-r--r--save.go18
3 files changed, 29 insertions, 20 deletions
diff --git a/findFilename.go b/findFilename.go
index d69b538..c99e289 100644
--- a/findFilename.go
+++ b/findFilename.go
@@ -1,6 +1,8 @@
package config
import (
+ "fmt"
+
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
)
@@ -8,8 +10,8 @@ import (
// Gemini AI can help author some pretty good protobuf code.
// I never remember the syntax for 'reflect' on these things.
-// returns "Filename" if it exists in the protobuf
-func GetFilename(pb proto.Message) (string, bool) {
+// this will try both "filename" and "Filename"
+func GetFilename(pb proto.Message) (string, error) {
// 1. Get the protoreflect.Message interface from the message.
// This is the entry point to the reflection API.
msg := pb.ProtoReflect()
@@ -19,18 +21,24 @@ func GetFilename(pb proto.Message) (string, bool) {
// 3. Find the specific field descriptor by its protobuf name ("Filename").
// Note: The field name must match the name in the .proto file.
- fieldName := protoreflect.Name("Filename")
+ fieldName := protoreflect.Name("filename")
fieldDescriptor := descriptor.Fields().ByName(fieldName)
+ // try upper case
+ if fieldDescriptor == nil {
+ fieldName = protoreflect.Name("Filename")
+ fieldDescriptor = descriptor.Fields().ByName(fieldName)
+ }
+
// 4. Check if the field was found. If not, return false.
if fieldDescriptor == nil {
- return "", false
+ return "", fmt.Errorf("fieldDescriptor == nil")
}
// 5. (Optional but recommended) Verify the field is a string type.
if fieldDescriptor.Kind() != protoreflect.StringKind {
// The field exists but is not a string, so we can't return it as one.
- return "", false
+ return "", fmt.Errorf("The field exists but is not a string")
}
// 6. If the field exists and is a string, get its value.
@@ -38,7 +46,7 @@ func GetFilename(pb proto.Message) (string, bool) {
value := msg.Get(fieldDescriptor)
// 7. Convert the protoreflect.Value to a native Go string.
- return value.String(), true
+ return value.String(), nil
}
// sets "Filename" if it exists in the protobuf
diff --git a/load.go b/load.go
index 16e6170..856a0b4 100644
--- a/load.go
+++ b/load.go
@@ -71,9 +71,10 @@ func ConfigLoad(pb proto.Message, argname string, protoname string) error {
}
func Load(pb proto.Message) error {
- fullname, ok := GetFilename(pb)
- if !ok {
- return ErrProtoFilename
+ fullname, err := GetFilename(pb)
+ if err != nil {
+ log.Info("filename =", fullname, err)
+ return err
}
if strings.HasSuffix(fullname, ".text") {
return loadTEXT(pb, fullname)
@@ -171,7 +172,7 @@ func loadTEXT(pb proto.Message, fullname string) error {
return ErrMarshal
}
- if fn, ok := GetFilename(pb); ok {
+ if fn, err := GetFilename(pb); err != nil {
if fn != fullname {
log.Info("config.ConfigLoad() new filename:", fullname)
SetFilename(pb, fullname)
@@ -207,7 +208,7 @@ func loadJSON(pb proto.Message, fullname string) error {
return ErrMarshal
}
- if fn, ok := GetFilename(pb); ok {
+ if fn, err := GetFilename(pb); err != nil {
if fn != fullname {
log.Info("config.ConfigLoad() new filename:", fullname)
SetFilename(pb, fullname)
diff --git a/save.go b/save.go
index 3e4b9af..dccbcaa 100644
--- a/save.go
+++ b/save.go
@@ -20,9 +20,9 @@ func ConfigSave(pb proto.Message) error {
}
func Save(pb proto.Message) error {
- fullname, ok := GetFilename(pb)
- if !ok {
- return ErrProtoFilename
+ fullname, err := GetFilename(pb)
+ if err != nil {
+ return err
}
if strings.HasSuffix(fullname, ".pb") {
SavePB(pb, fullname)
@@ -82,9 +82,9 @@ func ConfigSaveWithHeader(pb proto.Message, header string) error {
func saveTEXT(pb proto.Message, header string) error {
// get pb.Filename if it is there in the .proto file
- fullname, ok := GetFilename(pb)
- if !ok {
- return ErrProtoFilename
+ fullname, err := GetFilename(pb)
+ if err != nil {
+ return err
}
if !strings.HasSuffix(fullname, ".text") {
// todo: append .text here?
@@ -107,9 +107,9 @@ func saveTEXT(pb proto.Message, header string) error {
func saveJSON(pb proto.Message) error {
// get pb.Filename if it is there in the .proto file
- fullname, ok := GetFilename(pb)
- if !ok {
- return ErrProtoFilename
+ fullname, err := GetFilename(pb)
+ if err != nil {
+ return err
}
if !strings.HasSuffix(fullname, ".text") {
// todo: append .text here?