summaryrefslogtreecommitdiffstats
path: root/tests/internal/value/value.go
blob: 87027dd8b246fa96e52aab9881d93267e3fa2322 (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
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
package value

import (
	"fmt"
	"reflect"
)

func ToIcinga2Config(value interface{}) string {
	if value == nil {
		return "null"
	}

	refVal := reflect.ValueOf(value)
	switch refVal.Kind() {
	case reflect.Slice:
		vs := ""
		for i := 0; i < refVal.Len(); i++ {
			vs += ToIcinga2Config(refVal.Index(i).Interface()) + ","
		}
		return "[" + vs + "]"
	case reflect.Map:
		kvs := ""
		iter := refVal.MapRange()
		for iter.Next() {
			kvs += ToIcinga2Config(iter.Key().Interface()) + "=" + ToIcinga2Config(iter.Value().Interface()) + ","
		}
		return "{" + kvs + "}"
	}

	switch v := value.(type) {
	case interface{ Icinga2ConfigValue() string }:
		return v.Icinga2ConfigValue()
	case string:
		// TODO(jb): probably not perfect quoting, but good enough for now
		return fmt.Sprintf("%q", v)
	case *string:
		if v != nil {
			return ToIcinga2Config(*v)
		} else {
			return "null"
		}
	case bool, int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64:
		return fmt.Sprintf("%v", v)
	default:
		panic(fmt.Errorf("ToIcinga2Config called on unknown type %T", value))
	}
}

func ToIcinga2Api(value interface{}) interface{} {
	if value == nil {
		return nil
	}

	refVal := reflect.ValueOf(value)
	switch refVal.Kind() {
	case reflect.Slice:
		r := make([]interface{}, refVal.Len())
		for i := range r {
			r[i] = ToIcinga2Api(refVal.Index(i).Interface())
		}
		return r
	case reflect.Map:
		r := make(map[string]interface{})
		iter := refVal.MapRange()
		for iter.Next() {
			// TODO: perform a better check than the type assertion
			r[ToIcinga2Api(iter.Key().Interface()).(string)] = ToIcinga2Api(iter.Value().Interface())
		}
		return r
	}

	switch v := value.(type) {
	case interface{ Icinga2ApiValue() interface{} }:
		return v.Icinga2ApiValue()
	case string, []string, bool, int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64:
		return v
	case *string:
		if v != nil {
			return ToIcinga2Api(*v)
		} else {
			return nil
		}
	default:
		panic(fmt.Errorf("ToIcinga2Api called on unknown type %T", value))
	}
}

func ToIcingaDb(value interface{}) interface{} {
	switch v := value.(type) {
	case interface{ IcingaDbValue() interface{} }:
		return v.IcingaDbValue()
	case bool:
		if v {
			return "y"
		} else {
			return "n"
		}
	case string, *string, int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, float32, float64:
		return v
	default:
		panic(fmt.Errorf("ToIcinga2Api called on unknown type %T", value))
	}
}