forked from Rheisen/bconf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_file_loader.go
148 lines (113 loc) · 2.92 KB
/
json_file_loader.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package bconf
import (
"encoding/json"
"fmt"
"os"
"strings"
)
// type JSONMarshal func(v interface{}) ([]byte, error)
type JSONUnmarshal func(data []byte, v interface{}) error
func NewJSONFileLoader() *JSONFileLoader {
return NewJSONFileLoaderWithAttributes(nil)
}
func NewJSONFileLoaderWithAttributes(decoder JSONUnmarshal, filePaths ...string) *JSONFileLoader {
return &JSONFileLoader{
Decoder: decoder,
FilePaths: filePaths,
}
}
type JSONFileLoader struct {
Decoder JSONUnmarshal
FilePaths []string
// Encoder JSONMarshal
}
func (l *JSONFileLoader) Clone() *JSONFileLoader {
clone := *l
clone.FilePaths = make([]string, len(l.FilePaths))
copy(clone.FilePaths, l.FilePaths)
return &clone
}
func (l *JSONFileLoader) CloneLoader() Loader {
return l.Clone()
}
func (l *JSONFileLoader) Name() string {
return "bconf_jsonfile"
}
func (l *JSONFileLoader) Get(fieldSetKey, fieldKey string) (string, bool) {
maps := l.fileMaps()
if len(maps) < 1 {
return "", false
}
return l.findValueInMaps(fieldSetKey, fieldKey, &maps)
}
func (l *JSONFileLoader) GetMap(fieldSetKey string, fieldKeys []string) map[string]string {
values := map[string]string{}
maps := l.fileMaps()
if len(maps) < 1 {
return values
}
for _, fieldKey := range fieldKeys {
val, found := l.findValueInMaps(fieldSetKey, fieldKey, &maps)
if found {
values[fieldKey] = val
}
}
return values
}
func (l *JSONFileLoader) HelpString(fieldSetKey, fieldKey string) string {
return fmt.Sprintf("JSON attribute: %s.%s", fieldSetKey, fieldKey)
}
func (l *JSONFileLoader) findValueInMaps(fieldSetKey, fieldKey string, maps *[]map[string]any) (string, bool) {
if maps == nil {
return "", false
}
for _, fileMap := range *maps {
fieldSetAny, found := fileMap[fieldSetKey]
if !found {
continue
}
fieldSetMap, ok := fieldSetAny.(map[string]any)
if !ok {
continue
}
value, ok := fieldSetMap[fieldKey]
if !ok {
continue
}
bytes, _ := json.Marshal(value)
valueString := string(bytes)
if strings.HasPrefix(valueString, "[") && strings.HasSuffix(valueString, "]") {
valueString = valueString[1 : len(valueString)-1]
valueStringSlice := strings.Split(valueString, ",")
for index, val := range valueStringSlice {
valueStringSlice[index] = strings.Trim(val, "\"")
}
valueString = strings.Join(valueStringSlice, ",")
} else {
valueString = strings.Trim(valueString, "\"")
}
return valueString, true
}
return "", false
}
func (l *JSONFileLoader) fileMaps() []map[string]any {
fileMaps := []map[string]any{}
for _, path := range l.FilePaths {
fileBytes, err := os.ReadFile(path)
if err != nil {
continue
}
fileMap := map[string]any{}
if l.Decoder != nil {
if err := l.Decoder(fileBytes, &fileMap); err != nil {
continue
}
} else {
if err := json.Unmarshal(fileBytes, &fileMap); err != nil {
continue
}
}
fileMaps = append(fileMaps, fileMap)
}
return fileMaps
}