| 1 |
package sprig |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"encoding/json" |
| 6 |
"math/rand" |
| 7 |
"reflect" |
| 8 |
"strings" |
| 9 |
"time" |
| 10 |
) |
| 11 |
|
| 12 |
func init() { |
| 13 |
rand.Seed(time.Now().UnixNano()) |
| 14 |
} |
| 15 |
|
| 16 |
// dfault checks whether `given` is set, and returns default if not set. |
| 17 |
// |
| 18 |
// This returns `d` if `given` appears not to be set, and `given` otherwise. |
| 19 |
// |
| 20 |
// For numeric types 0 is unset. |
| 21 |
// For strings, maps, arrays, and slices, len() = 0 is considered unset. |
| 22 |
// For bool, false is unset. |
| 23 |
// Structs are never considered unset. |
| 24 |
// |
| 25 |
// For everything else, including pointers, a nil value is unset. |
| 26 |
func dfault(d interface{}, given ...interface{}) interface{} { |
| 27 |
|
| 28 |
if empty(given) || empty(given[0]) { |
| 29 |
return d |
| 30 |
} |
| 31 |
return given[0] |
| 32 |
} |
| 33 |
|
| 34 |
// empty returns true if the given value has the zero value for its type. |
| 35 |
func empty(given interface{}) bool { |
| 36 |
g := reflect.ValueOf(given) |
| 37 |
if !g.IsValid() { |
| 38 |
return true |
| 39 |
} |
| 40 |
|
| 41 |
// Basically adapted from text/template.isTrue |
| 42 |
switch g.Kind() { |
| 43 |
default: |
| 44 |
return g.IsNil() |
| 45 |
case reflect.Array, reflect.Slice, reflect.Map, reflect.String: |
| 46 |
return g.Len() == 0 |
| 47 |
case reflect.Bool: |
| 48 |
return !g.Bool() |
| 49 |
case reflect.Complex64, reflect.Complex128: |
| 50 |
return g.Complex() == 0 |
| 51 |
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: |
| 52 |
return g.Int() == 0 |
| 53 |
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: |
| 54 |
return g.Uint() == 0 |
| 55 |
case reflect.Float32, reflect.Float64: |
| 56 |
return g.Float() == 0 |
| 57 |
case reflect.Struct: |
| 58 |
return false |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
// coalesce returns the first non-empty value. |
| 63 |
func coalesce(v ...interface{}) interface{} { |
| 64 |
for _, val := range v { |
| 65 |
if !empty(val) { |
| 66 |
return val |
| 67 |
} |
| 68 |
} |
| 69 |
return nil |
| 70 |
} |
| 71 |
|
| 72 |
// all returns true if empty(x) is false for all values x in the list. |
| 73 |
// If the list is empty, return true. |
| 74 |
func all(v ...interface{}) bool { |
| 75 |
for _, val := range v { |
| 76 |
if empty(val) { |
| 77 |
return false |
| 78 |
} |
| 79 |
} |
| 80 |
return true |
| 81 |
} |
| 82 |
|
| 83 |
// any returns true if empty(x) is false for any x in the list. |
| 84 |
// If the list is empty, return false. |
| 85 |
func any(v ...interface{}) bool { |
| 86 |
for _, val := range v { |
| 87 |
if !empty(val) { |
| 88 |
return true |
| 89 |
} |
| 90 |
} |
| 91 |
return false |
| 92 |
} |
| 93 |
|
| 94 |
// fromJson decodes JSON into a structured value, ignoring errors. |
| 95 |
func fromJson(v string) interface{} { |
| 96 |
output, _ := mustFromJson(v) |
| 97 |
return output |
| 98 |
} |
| 99 |
|
| 100 |
// mustFromJson decodes JSON into a structured value, returning errors. |
| 101 |
func mustFromJson(v string) (interface{}, error) { |
| 102 |
var output interface{} |
| 103 |
err := json.Unmarshal([]byte(v), &output) |
| 104 |
return output, err |
| 105 |
} |
| 106 |
|
| 107 |
// toJson encodes an item into a JSON string |
| 108 |
func toJson(v interface{}) string { |
| 109 |
output, _ := json.Marshal(v) |
| 110 |
return string(output) |
| 111 |
} |
| 112 |
|
| 113 |
func mustToJson(v interface{}) (string, error) { |
| 114 |
output, err := json.Marshal(v) |
| 115 |
if err != nil { |
| 116 |
return "", err |
| 117 |
} |
| 118 |
return string(output), nil |
| 119 |
} |
| 120 |
|
| 121 |
// toPrettyJson encodes an item into a pretty (indented) JSON string |
| 122 |
func toPrettyJson(v interface{}) string { |
| 123 |
output, _ := json.MarshalIndent(v, "", " ") |
| 124 |
return string(output) |
| 125 |
} |
| 126 |
|
| 127 |
func mustToPrettyJson(v interface{}) (string, error) { |
| 128 |
output, err := json.MarshalIndent(v, "", " ") |
| 129 |
if err != nil { |
| 130 |
return "", err |
| 131 |
} |
| 132 |
return string(output), nil |
| 133 |
} |
| 134 |
|
| 135 |
// toRawJson encodes an item into a JSON string with no escaping of HTML characters. |
| 136 |
func toRawJson(v interface{}) string { |
| 137 |
output, err := mustToRawJson(v) |
| 138 |
if err != nil { |
| 139 |
panic(err) |
| 140 |
} |
| 141 |
return string(output) |
| 142 |
} |
| 143 |
|
| 144 |
// mustToRawJson encodes an item into a JSON string with no escaping of HTML characters. |
| 145 |
func mustToRawJson(v interface{}) (string, error) { |
| 146 |
buf := new(bytes.Buffer) |
| 147 |
enc := json.NewEncoder(buf) |
| 148 |
enc.SetEscapeHTML(false) |
| 149 |
err := enc.Encode(&v) |
| 150 |
if err != nil { |
| 151 |
return "", err |
| 152 |
} |
| 153 |
return strings.TrimSuffix(buf.String(), "\n"), nil |
| 154 |
} |
| 155 |
|
| 156 |
// ternary returns the first value if the last value is true, otherwise returns the second value. |
| 157 |
func ternary(vt interface{}, vf interface{}, v bool) interface{} { |
| 158 |
if v { |
| 159 |
return vt |
| 160 |
} |
| 161 |
|
| 162 |
return vf |
| 163 |
} |