| 1 |
package sprig |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"encoding/json" |
| 6 |
"reflect" |
| 7 |
"slices" |
| 8 |
"strings" |
| 9 |
) |
| 10 |
|
| 11 |
// defaultValue checks whether `given` is set, and returns default if not set. |
| 12 |
// |
| 13 |
// This returns `d` if `given` appears not to be set, and `given` otherwise. |
| 14 |
// |
| 15 |
// For numeric types 0 is unset. |
| 16 |
// For strings, maps, arrays, and slices, len() = 0 is considered unset. |
| 17 |
// For bool, false is unset. |
| 18 |
// Structs are never considered unset. |
| 19 |
// |
| 20 |
// For everything else, including pointers, a nil value is unset. |
| 21 |
func defaultValue(d any, given ...any) any { |
| 22 |
if empty(given) || empty(given[0]) { |
| 23 |
return d |
| 24 |
} |
| 25 |
return given[0] |
| 26 |
} |
| 27 |
|
| 28 |
// empty returns true if the given value has the zero value for its type. |
| 29 |
// This is a helper function used by defaultValue, coalesce, all, and anyNonEmpty. |
| 30 |
// |
| 31 |
// The following values are considered empty: |
| 32 |
// - Invalid values |
| 33 |
// - nil values |
| 34 |
// - Zero-length arrays, slices, maps, and strings |
| 35 |
// - Boolean false |
| 36 |
// - Zero for all numeric types |
| 37 |
// - Structs are never considered empty |
| 38 |
// |
| 39 |
// Parameters: |
| 40 |
// - given: The value to check for emptiness |
| 41 |
// |
| 42 |
// Returns: |
| 43 |
// - bool: True if the value is considered empty, false otherwise |
| 44 |
func empty(given any) bool { |
| 45 |
g := reflect.ValueOf(given) |
| 46 |
if !g.IsValid() { |
| 47 |
return true |
| 48 |
} |
| 49 |
// Basically adapted from text/template.isTrue |
| 50 |
switch g.Kind() { |
| 51 |
default: |
| 52 |
return g.IsNil() |
| 53 |
case reflect.Array, reflect.Slice, reflect.Map, reflect.String: |
| 54 |
return g.Len() == 0 |
| 55 |
case reflect.Bool: |
| 56 |
return !g.Bool() |
| 57 |
case reflect.Complex64, reflect.Complex128: |
| 58 |
return g.Complex() == 0 |
| 59 |
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: |
| 60 |
return g.Int() == 0 |
| 61 |
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: |
| 62 |
return g.Uint() == 0 |
| 63 |
case reflect.Float32, reflect.Float64: |
| 64 |
return g.Float() == 0 |
| 65 |
case reflect.Struct: |
| 66 |
return false |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
// coalesce returns the first non-empty value from a list of values. |
| 71 |
// If all values are empty, it returns nil. |
| 72 |
// |
| 73 |
// This is useful for providing a series of fallback values. |
| 74 |
// |
| 75 |
// Parameters: |
| 76 |
// - v: A variadic list of values to check |
| 77 |
// |
| 78 |
// Returns: |
| 79 |
// - any: The first non-empty value, or nil if all values are empty |
| 80 |
func coalesce(v ...any) any { |
| 81 |
for _, val := range v { |
| 82 |
if !empty(val) { |
| 83 |
return val |
| 84 |
} |
| 85 |
} |
| 86 |
return nil |
| 87 |
} |
| 88 |
|
| 89 |
// all checks if all values in a list are non-empty. |
| 90 |
// Returns true if every value in the list is non-empty. |
| 91 |
// If the list is empty, returns true (vacuously true). |
| 92 |
// |
| 93 |
// Parameters: |
| 94 |
// - v: A variadic list of values to check |
| 95 |
// |
| 96 |
// Returns: |
| 97 |
// - bool: True if all values are non-empty, false otherwise |
| 98 |
func all(v ...any) bool { |
| 99 |
return !slices.ContainsFunc(v, empty) |
| 100 |
} |
| 101 |
|
| 102 |
// anyNonEmpty checks if at least one value in a list is non-empty. |
| 103 |
// Returns true if any value in the list is non-empty. |
| 104 |
// If the list is empty, returns false. |
| 105 |
// |
| 106 |
// Parameters: |
| 107 |
// - v: A variadic list of values to check |
| 108 |
// |
| 109 |
// Returns: |
| 110 |
// - bool: True if at least one value is non-empty, false otherwise |
| 111 |
func anyNonEmpty(v ...any) bool { |
| 112 |
for _, val := range v { |
| 113 |
if !empty(val) { |
| 114 |
return true |
| 115 |
} |
| 116 |
} |
| 117 |
return false |
| 118 |
} |
| 119 |
|
| 120 |
// fromJSON decodes a JSON string into a structured value. |
| 121 |
// This function ignores any errors that occur during decoding. |
| 122 |
// If the JSON is invalid, it returns nil. |
| 123 |
// |
| 124 |
// Parameters: |
| 125 |
// - v: The JSON string to decode |
| 126 |
// |
| 127 |
// Returns: |
| 128 |
// - any: The decoded value, or nil if decoding failed |
| 129 |
func fromJSON(v string) any { |
| 130 |
output, _ := mustFromJSON(v) |
| 131 |
return output |
| 132 |
} |
| 133 |
|
| 134 |
// mustFromJSON decodes a JSON string into a structured value. |
| 135 |
// Unlike fromJSON, this function returns any errors that occur during decoding. |
| 136 |
// |
| 137 |
// Parameters: |
| 138 |
// - v: The JSON string to decode |
| 139 |
// |
| 140 |
// Returns: |
| 141 |
// - any: The decoded value |
| 142 |
// - error: Any error that occurred during decoding |
| 143 |
func mustFromJSON(v string) (any, error) { |
| 144 |
var output any |
| 145 |
err := json.Unmarshal([]byte(v), &output) |
| 146 |
return output, err |
| 147 |
} |
| 148 |
|
| 149 |
// toJSON encodes a value into a JSON string. |
| 150 |
// This function ignores any errors that occur during encoding. |
| 151 |
// If the value cannot be encoded, it returns an empty string. |
| 152 |
// |
| 153 |
// Parameters: |
| 154 |
// - v: The value to encode to JSON |
| 155 |
// |
| 156 |
// Returns: |
| 157 |
// - string: The JSON string representation of the value |
| 158 |
func toJSON(v any) string { |
| 159 |
output, _ := json.Marshal(v) |
| 160 |
return string(output) |
| 161 |
} |
| 162 |
|
| 163 |
// mustToJSON encodes a value into a JSON string. |
| 164 |
// Unlike toJSON, this function returns any errors that occur during encoding. |
| 165 |
// |
| 166 |
// Parameters: |
| 167 |
// - v: The value to encode to JSON |
| 168 |
// |
| 169 |
// Returns: |
| 170 |
// - string: The JSON string representation of the value |
| 171 |
// - error: Any error that occurred during encoding |
| 172 |
func mustToJSON(v any) (string, error) { |
| 173 |
output, err := json.Marshal(v) |
| 174 |
if err != nil { |
| 175 |
return "", err |
| 176 |
} |
| 177 |
return string(output), nil |
| 178 |
} |
| 179 |
|
| 180 |
// toPrettyJSON encodes a value into a pretty (indented) JSON string. |
| 181 |
// This function ignores any errors that occur during encoding. |
| 182 |
// If the value cannot be encoded, it returns an empty string. |
| 183 |
// |
| 184 |
// Parameters: |
| 185 |
// - v: The value to encode to JSON |
| 186 |
// |
| 187 |
// Returns: |
| 188 |
// - string: The indented JSON string representation of the value |
| 189 |
func toPrettyJSON(v any) string { |
| 190 |
output, _ := json.MarshalIndent(v, "", " ") |
| 191 |
return string(output) |
| 192 |
} |
| 193 |
|
| 194 |
// mustToPrettyJSON encodes a value into a pretty (indented) JSON string. |
| 195 |
// Unlike toPrettyJSON, this function returns any errors that occur during encoding. |
| 196 |
// |
| 197 |
// Parameters: |
| 198 |
// - v: The value to encode to JSON |
| 199 |
// |
| 200 |
// Returns: |
| 201 |
// - string: The indented JSON string representation of the value |
| 202 |
// - error: Any error that occurred during encoding |
| 203 |
func mustToPrettyJSON(v any) (string, error) { |
| 204 |
output, err := json.MarshalIndent(v, "", " ") |
| 205 |
if err != nil { |
| 206 |
return "", err |
| 207 |
} |
| 208 |
return string(output), nil |
| 209 |
} |
| 210 |
|
| 211 |
// toRawJSON encodes a value into a JSON string with no escaping of HTML characters. |
| 212 |
// This function panics if an error occurs during encoding. |
| 213 |
// Unlike toJSON, HTML characters like <, >, and & are not escaped. |
| 214 |
// |
| 215 |
// Parameters: |
| 216 |
// - v: The value to encode to JSON |
| 217 |
// |
| 218 |
// Returns: |
| 219 |
// - string: The JSON string representation of the value without HTML escaping |
| 220 |
func toRawJSON(v any) string { |
| 221 |
output, err := mustToRawJSON(v) |
| 222 |
if err != nil { |
| 223 |
panic(err) |
| 224 |
} |
| 225 |
return output |
| 226 |
} |
| 227 |
|
| 228 |
// mustToRawJSON encodes a value into a JSON string with no escaping of HTML characters. |
| 229 |
// Unlike toRawJSON, this function returns any errors that occur during encoding. |
| 230 |
// HTML characters like <, >, and & are not escaped in the output. |
| 231 |
// |
| 232 |
// Parameters: |
| 233 |
// - v: The value to encode to JSON |
| 234 |
// |
| 235 |
// Returns: |
| 236 |
// - string: The JSON string representation of the value without HTML escaping |
| 237 |
// - error: Any error that occurred during encoding |
| 238 |
func mustToRawJSON(v any) (string, error) { |
| 239 |
buf := new(bytes.Buffer) |
| 240 |
enc := json.NewEncoder(buf) |
| 241 |
enc.SetEscapeHTML(false) |
| 242 |
if err := enc.Encode(&v); err != nil { |
| 243 |
return "", err |
| 244 |
} |
| 245 |
return strings.TrimSuffix(buf.String(), "\n"), nil |
| 246 |
} |
| 247 |
|
| 248 |
// ternary implements a conditional (ternary) operator. |
| 249 |
// It returns the first value if the condition is true, otherwise returns the second value. |
| 250 |
// This is similar to the ?: operator in many programming languages. |
| 251 |
// |
| 252 |
// Parameters: |
| 253 |
// - vt: The value to return if the condition is true |
| 254 |
// - vf: The value to return if the condition is false |
| 255 |
// - v: The boolean condition to evaluate |
| 256 |
// |
| 257 |
// Returns: |
| 258 |
// - any: Either vt or vf depending on the value of v |
| 259 |
func ternary(vt any, vf any, v bool) any { |
| 260 |
if v { |
| 261 |
return vt |
| 262 |
} |
| 263 |
return vf |
| 264 |
} |