podman
163 строки · 3.8 Кб
1package sprig2
3import (4"bytes"5"encoding/json"6"math/rand"7"reflect"8"strings"9"time"10)
11
12func init() {13rand.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.
26func dfault(d interface{}, given ...interface{}) interface{} {27
28if empty(given) || empty(given[0]) {29return d30}31return given[0]32}
33
34// empty returns true if the given value has the zero value for its type.
35func empty(given interface{}) bool {36g := reflect.ValueOf(given)37if !g.IsValid() {38return true39}40
41// Basically adapted from text/template.isTrue42switch g.Kind() {43default:44return g.IsNil()45case reflect.Array, reflect.Slice, reflect.Map, reflect.String:46return g.Len() == 047case reflect.Bool:48return !g.Bool()49case reflect.Complex64, reflect.Complex128:50return g.Complex() == 051case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:52return g.Int() == 053case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:54return g.Uint() == 055case reflect.Float32, reflect.Float64:56return g.Float() == 057case reflect.Struct:58return false59}60}
61
62// coalesce returns the first non-empty value.
63func coalesce(v ...interface{}) interface{} {64for _, val := range v {65if !empty(val) {66return val67}68}69return nil70}
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.
74func all(v ...interface{}) bool {75for _, val := range v {76if empty(val) {77return false78}79}80return true81}
82
83// any returns true if empty(x) is false for any x in the list.
84// If the list is empty, return false.
85func any(v ...interface{}) bool {86for _, val := range v {87if !empty(val) {88return true89}90}91return false92}
93
94// fromJson decodes JSON into a structured value, ignoring errors.
95func fromJson(v string) interface{} {96output, _ := mustFromJson(v)97return output98}
99
100// mustFromJson decodes JSON into a structured value, returning errors.
101func mustFromJson(v string) (interface{}, error) {102var output interface{}103err := json.Unmarshal([]byte(v), &output)104return output, err105}
106
107// toJson encodes an item into a JSON string
108func toJson(v interface{}) string {109output, _ := json.Marshal(v)110return string(output)111}
112
113func mustToJson(v interface{}) (string, error) {114output, err := json.Marshal(v)115if err != nil {116return "", err117}118return string(output), nil119}
120
121// toPrettyJson encodes an item into a pretty (indented) JSON string
122func toPrettyJson(v interface{}) string {123output, _ := json.MarshalIndent(v, "", " ")124return string(output)125}
126
127func mustToPrettyJson(v interface{}) (string, error) {128output, err := json.MarshalIndent(v, "", " ")129if err != nil {130return "", err131}132return string(output), nil133}
134
135// toRawJson encodes an item into a JSON string with no escaping of HTML characters.
136func toRawJson(v interface{}) string {137output, err := mustToRawJson(v)138if err != nil {139panic(err)140}141return string(output)142}
143
144// mustToRawJson encodes an item into a JSON string with no escaping of HTML characters.
145func mustToRawJson(v interface{}) (string, error) {146buf := new(bytes.Buffer)147enc := json.NewEncoder(buf)148enc.SetEscapeHTML(false)149err := enc.Encode(&v)150if err != nil {151return "", err152}153return strings.TrimSuffix(buf.String(), "\n"), nil154}
155
156// ternary returns the first value if the last value is true, otherwise returns the second value.
157func ternary(vt interface{}, vf interface{}, v bool) interface{} {158if v {159return vt160}161
162return vf163}
164