MCPcopy Index your code
hub / github.com/DoNewsCode/core / getString

Function getString

helper.go:9–32  ·  view source on GitHub ↗
(data map[string]interface{}, key ...string)

Source from the content-addressed store, hash-verified

7)
8
9func getString(data map[string]interface{}, key ...string) (string, error) {
10 if len(key) <= 0 {
11 panic("key must be provided at least once")
12 }
13 for i := 0; i < len(key)-1; i++ {
14 value, ok := data[key[i]]
15 if !ok {
16 return "", fmt.Errorf("%s doesn't exist", strings.Join(key[0:i+1], "."))
17 }
18 data, ok = value.(map[string]interface{})
19 if !ok {
20 return "", fmt.Errorf("%s is not a map", strings.Join(key[0:i+1], "."))
21 }
22 }
23 value, ok := data[key[len(key)-1]]
24 if !ok {
25 return "", fmt.Errorf("%s doesn't exist", strings.Join(key, "."))
26 }
27 str, ok := value.(string)
28 if !ok {
29 return str, errors.New("must be a string")
30 }
31 return str, nil
32}
33
34func getBool(data map[string]interface{}, key ...string) (bool, error) {
35 if len(key) <= 0 {

Callers 1

provideDefaultConfigFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected