summaryrefslogtreecommitdiff
path: root/common/options.go
blob: f0ededf5a4bdf14bfb24b253ae474956f8e24285 (plain)
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
package options

import (
	"encoding/json"
	"errors"
	"github.com/OperatorFoundation/shapeshifter-dispatcher/common/log"
	interconv "github.com/mufti1/interconv/package"
	"strings"
)

func ParseOptions(s string) (map[string]interface{}, error) {
	var result map[string]interface{}

	if len(s) == 0 {
		return map[string]interface{}{}, nil
	}

	decoder := json.NewDecoder(strings.NewReader(s))
	if err := decoder.Decode(&result); err != nil {
		log.Errorf("Error decoding JSON %q", err)
		return nil, err
	}

	return result, nil
}

func ParseServerOptions(s string) (params map[string]map[string]interface{}, err error) {
	result := make(map[string]map[string]interface{})

	if len(s) == 0 {
		return result, nil
	}

	decoder := json.NewDecoder(strings.NewReader(s))
	if err := decoder.Decode(&result); err != nil {
		log.Errorf("Error decoding JSON %q", err)
		return nil, err
	}

	return result, nil
}

func CoerceToString(futureString interface{}) (*string, error) {
		var result string

		switch futureString.(type) {
		case string:
			var icerr error
			result, icerr = interconv.ParseString(futureString)
			if icerr != nil {
				return nil, icerr
			}
			return &result, nil
		default:
			return nil, errors.New("unable to coerce empty interface to string")
		}
}