-
Notifications
You must be signed in to change notification settings - Fork 0
/
configSet.go
110 lines (87 loc) · 2.47 KB
/
configSet.go
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Package confiq is a Go package for populating structs from structured data formats such JSON, TOML, YAML or Env.
package confiq
import (
"errors"
"fmt"
"reflect"
)
const defaultTag = "cfg"
var (
errCannotGetKeyFromNonMap = errors.New("cannot get key from non-map type")
errKeyNotFound = errors.New("key not found")
errCannotGetIndexFromNonSlice = errors.New("cannot get index from non-slice type")
errIndexOutOfBounds = errors.New("index out of bounds")
)
type decoder struct {
tag string
}
type decodeSettings struct {
strict bool
prefix string
}
// ConfigSet is a configuration set that can be used to load and decode configuration values into a struct.
type ConfigSet struct {
value *any
decoder *decoder
}
// New creates a new ConfigSet with the given options.
func New(options ...configSetOption) *ConfigSet {
var (
value any
configSet = &ConfigSet{
value: &value,
decoder: &decoder{tag: defaultTag},
}
)
for _, option := range options {
option(configSet)
}
return configSet
}
// Get returns the configuration value at the given path as an interface.
func (c *ConfigSet) Get(path string) (any, error) {
return c.getByPath(path)
}
func (c *ConfigSet) getByPath(path string) (any, error) {
currentValue := *c.value
for path != "" {
var currentSegment segment
currentSegment, path = getNextSegment(path)
switch v := currentSegment.(type) {
case keySegment:
segmentValue, err := getMapValue(currentValue, v.asString())
if err != nil {
return nil, err
}
currentValue = segmentValue
case indexSegment:
segmentValue, err := getSliceValue(currentValue, v.asInt())
if err != nil {
return nil, err
}
currentValue = segmentValue
}
}
return currentValue, nil
}
func getMapValue(originMap any, key string) (any, error) {
v := reflect.ValueOf(originMap)
if v.Kind() != reflect.Map {
return nil, fmt.Errorf("%w: %s(%T)", errCannotGetKeyFromNonMap, key, originMap)
}
value := v.MapIndex(reflect.ValueOf(key))
if !value.IsValid() {
return nil, fmt.Errorf("%w: %s", errKeyNotFound, key)
}
return value.Interface(), nil
}
func getSliceValue(originSlice any, index int) (any, error) {
v := reflect.ValueOf(originSlice)
if v.Kind() != reflect.Slice {
return nil, fmt.Errorf("%w: %d(%T)", errCannotGetIndexFromNonSlice, index, originSlice)
}
if v.Len() <= index || index < 0 {
return nil, fmt.Errorf("%w: %d", errIndexOutOfBounds, index)
}
return v.Index(index).Interface(), nil
}