-
Notifications
You must be signed in to change notification settings - Fork 0
/
remote.go
46 lines (40 loc) · 1.15 KB
/
remote.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
// 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 (
"bytes"
"encoding/json"
"gopkg.in/yaml.v3"
)
const (
// RemoteValueJSON indicates that content under a key is in JSON format.
RemoteValueJSON = "json"
// RemoteValueYAML indicates that content under a key is in YAML format.
RemoteValueYAML = "yaml"
// RemoteValuePlain indicates that content under a key is plain text.
RemoteValuePlain = "plain"
)
// getRemoteKVPairConfigMap returns configuration map for a key, according to format.
func getRemoteKVPairConfigMap(key string, value []byte, format string) (map[string]any, error) {
var (
configMap map[string]any
err error
)
switch format {
case RemoteValueJSON:
if err = json.Unmarshal(value, &configMap); err != nil {
return nil, err
}
case RemoteValueYAML:
if err = yaml.Unmarshal(value, &configMap); err != nil {
return nil, err
}
default: // plain
configMap = map[string]any{
key: string(bytes.TrimSpace(value)),
}
}
return configMap, nil
}