-
Notifications
You must be signed in to change notification settings - Fork 0
/
loader_properties.go
48 lines (40 loc) · 1.19 KB
/
loader_properties.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
// Copyright The ActForGood Authors.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file or at
// https://github.com/actforgood/xconf/blob/main/LICENSE.
package xconf
import (
"os"
"github.com/magiconair/properties"
)
// PropertiesFileLoader loads Java Properties configuration from a file.
// The location of properties content based file is given as parameter.
func PropertiesFileLoader(filePath string) Loader {
return LoaderFunc(func() (map[string]any, error) {
content, err := os.ReadFile(filePath)
if err != nil {
return nil, err
}
return PropertiesBytesLoader(content).Load()
})
}
// PropertiesBytesLoader loads Properties configuration from bytes.
func PropertiesBytesLoader(propertiesContent []byte) Loader {
return LoaderFunc(func() (map[string]any, error) {
loader := properties.Loader{
Encoding: properties.UTF8,
DisableExpansion: false,
}
cfg, err := loader.LoadBytes(propertiesContent)
if err != nil {
return nil, err
}
keys := cfg.Keys()
configMap := make(map[string]any, len(keys))
for _, key := range keys {
value, _ := cfg.Get(key)
configMap[key] = value
}
return configMap, nil
})
}