aboutsummaryrefslogtreecommitdiff
path: root/utils/toml/parse.go
blob: 38cd9c678c77b35d1c153190a3630e15718edd47 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package toml

import (
	"reflect"
	"strings"

	"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(ParseTargetMustBeStructPtr)
	}

	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(ConfigSectionInvalid, sectionName)
	}

	return unmarshalContent(sectionBytes, target)
}

func resolveSectionName(targetValue reflect.Value) string {
	typeName := targetValue.Elem().Type().Name()
	return strings.ToLower(typeName)
}