forked from hairyhenderson/gomplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypeconv.go
133 lines (115 loc) · 3.25 KB
/
typeconv.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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package main
import (
"encoding/json"
"fmt"
"log"
"strconv"
"strings"
yaml "gopkg.in/yaml.v2"
)
// TypeConv - type conversion function
type TypeConv struct {
}
// Bool converts a string to a boolean value, using strconv.ParseBool under the covers.
// Possible true values are: 1, t, T, TRUE, true, True
// All other values are considered false.
func (t *TypeConv) Bool(in string) bool {
if b, err := strconv.ParseBool(in); err == nil {
return b
}
return false
}
func unmarshalObj(obj map[string]interface{}, in string, f func([]byte, interface{}) error) map[string]interface{} {
err := f([]byte(in), &obj)
if err != nil {
log.Fatalf("Unable to unmarshal object %s: %v", in, err)
}
return obj
}
func unmarshalArray(obj []interface{}, in string, f func([]byte, interface{}) error) []interface{} {
err := f([]byte(in), &obj)
if err != nil {
log.Fatalf("Unable to unmarshal array %s: %v", in, err)
}
return obj
}
// JSON - Unmarshal a JSON Object
func (t *TypeConv) JSON(in string) map[string]interface{} {
obj := make(map[string]interface{})
return unmarshalObj(obj, in, json.Unmarshal)
}
// JSONArray - Unmarshal a JSON Array
func (t *TypeConv) JSONArray(in string) []interface{} {
obj := make([]interface{}, 1)
return unmarshalArray(obj, in, json.Unmarshal)
}
// YAML - Unmarshal a YAML Object
func (t *TypeConv) YAML(in string) map[string]interface{} {
obj := make(map[string]interface{})
return unmarshalObj(obj, in, yaml.Unmarshal)
}
// YAMLArray - Unmarshal a YAML Array
func (t *TypeConv) YAMLArray(in string) []interface{} {
obj := make([]interface{}, 1)
return unmarshalArray(obj, in, yaml.Unmarshal)
}
func marshalObj(obj interface{}, f func(interface{}) ([]byte, error)) string {
b, err := f(obj)
if err != nil {
log.Fatalf("Unable to marshal object %s: %v", obj, err)
}
return string(b)
}
// ToJSON - Stringify a struct as JSON
func (t *TypeConv) ToJSON(in interface{}) string {
return marshalObj(in, json.Marshal)
}
// ToYAML - Stringify a struct as YAML
func (t *TypeConv) ToYAML(in interface{}) string {
return marshalObj(in, yaml.Marshal)
}
// Slice creates a slice from a bunch of arguments
func (t *TypeConv) Slice(args ...interface{}) []interface{} {
return args
}
// Join concatenates the elements of a to create a single string.
// The separator string sep is placed between elements in the resulting string.
//
// This is functionally identical to strings.Join, except that each element is
// coerced to a string first
func (t *TypeConv) Join(a []interface{}, sep string) string {
b := make([]string, len(a))
for i := range a {
b[i] = toString(a[i])
}
return strings.Join(b, sep)
}
// Has determines whether or not a given object has a property with the given key
func (t *TypeConv) Has(in map[string]interface{}, key string) bool {
_, ok := in[key]
return ok
}
func toString(in interface{}) string {
if s, ok := in.(string); ok {
return s
}
if s, ok := in.(fmt.Stringer); ok {
return s.String()
}
if i, ok := in.(int); ok {
return strconv.Itoa(i)
}
if u, ok := in.(uint64); ok {
return strconv.FormatUint(u, 10)
}
if f, ok := in.(float64); ok {
return strconv.FormatFloat(f, 'f', -1, 64)
}
if b, ok := in.(bool); ok {
return strconv.FormatBool(b)
}
if in == nil {
return "nil"
}
return fmt.Sprintf("%s", in)
}