diff options
Diffstat (limited to 'utils/toml/parse.go')
| -rw-r--r-- | utils/toml/parse.go | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/utils/toml/parse.go b/utils/toml/parse.go new file mode 100644 index 0000000..7e2e6f2 --- /dev/null +++ b/utils/toml/parse.go @@ -0,0 +1,40 @@ +package toml + +import ( + "reflect" + "strings" + + "dove/messages" + "dove/utils/errors" +) + +func Parse(target any) error { + targetValue := reflect.ValueOf(target) + if targetValue.Kind() != reflect.Pointer || targetValue.Elem().Kind() != reflect.Struct { + return errors.Error(messages.ParseTargetMustBeStructPointer) + } + + ApplyDefaults(target) + + if loadedData == nil { + return nil + } + + sectionName := resolveSectionName(targetValue) + sectionData, exists := loadedData[sectionName] + if !exists { + return nil + } + + sectionBytes, marshalError := marshalSection(sectionData) + if marshalError != nil { + return errors.Error(messages.ConfigSectionInvalid, sectionName) + } + + return unmarshalContent(sectionBytes, target) +} + +func resolveSectionName(targetValue reflect.Value) string { + typeName := targetValue.Elem().Type().Name() + return strings.ToLower(typeName) +} |
