-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmap.go
119 lines (100 loc) · 3.52 KB
/
map.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
111
112
113
114
115
116
117
118
119
package dot
import (
"errors"
"fmt"
"reflect"
"strconv"
"strings"
)
// parseType checks if the key does not match one of the primitive types
func parseType(kind reflect.Kind, value string) (any, error) {
switch kind {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return strconv.ParseInt(value, 10, 64)
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return strconv.ParseUint(value, 10, 64)
case reflect.Float32, reflect.Float64:
// This is a forced measure, as the path to the value is divided by a dot
newValue := strings.ReplaceAll(value, ",", ".")
return strconv.ParseFloat(newValue, 64)
case reflect.Complex64, reflect.Complex128:
return strconv.ParseComplex(value, 128)
case reflect.Bool:
return strconv.ParseBool(value)
case reflect.String, reflect.Interface:
return value, nil
default:
return nil, errors.New("unknown type")
}
}
// keyValue prepares the value of the map key itself
func (d *Dot) keyValue(key reflect.Value, value string, currentPath string) (any, error) {
// Trying to find the appropriate key in placeholders
if placeholder, ok := d.Placeholders[value]; ok {
refPlaceholder := reflect.ValueOf(placeholder)
if refPlaceholder.Type() != key.Type() {
return nil, fmt.Errorf(
`the map key type is %s you cannot use the placeholder of type %s in path "%s"`,
key.Type(), refPlaceholder.Type(), currentPath,
)
}
return placeholder, nil
}
// Trying to catch a primitive key type
result, err := parseType(key.Kind(), value)
if err != nil {
// It was not possible to convert the value into a primitive map key type
if strings.Contains(err.Error(), "strconv") {
return nil, fmt.Errorf(
`the map key has an invalid key-value "%s" in path "%s" of type %s`,
value, currentPath, key.Type(),
)
}
// If no primitive type was found, it means there is no key placeholder
return nil, fmt.Errorf(
`unknown placeholder of type %s as map key in path "%s"`,
key.Type(), currentPath,
)
}
return result, nil
}
// prepareKey creates a key of the appropriate type for the map
func (d *Dot) prepareKey(innerObj reflect.Value, value string, currentPath string) (reflect.Value, error) {
thisMap := reflect.TypeOf(innerObj.Interface())
key := reflect.New(thisMap.Key()).Elem()
// Prepare the contents of the key to match its type
content, err := d.keyValue(key, value, currentPath)
if err != nil {
return reflect.Value{}, err
}
// Convert the type so that it exactly matches the required type
keyValue := reflect.ValueOf(content)
if keyValue.Type().ConvertibleTo(key.Type()) {
keyValue = keyValue.Convert(key.Type())
}
// Inserting the key value
key.Set(keyValue)
return key, nil
}
// inMap inserts a value into the map along the indicated path
func (d *Dot) inMap(innerObj reflect.Value, currentPath string, parts []string) error {
// Create a map if one does not exist
if innerObj.IsNil() {
innerObj.Set(reflect.MakeMap(innerObj.Type()))
}
// The map key can be of any type. As with the value,
// we must first create a key of the correct type.
key, err := d.prepareKey(innerObj, parts[0], currentPath)
if err != nil {
return err
}
// Initialise the value of the corresponding map type
value := reflect.New(innerObj.Type().Elem()).Elem()
// Insert the value recursively into the variable we just created
if err := d.insert(value, currentPath, parts[1:], Map); err != nil {
return err
}
// Having a key of the appropriate type, specify its value
innerObj.SetMapIndex(key, value)
return nil
}