podman
163 строки · 3.8 Кб
1package sprig
2
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 d
30}
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 true
39}
40
41// Basically adapted from text/template.isTrue
42switch g.Kind() {
43default:
44return g.IsNil()
45case reflect.Array, reflect.Slice, reflect.Map, reflect.String:
46return g.Len() == 0
47case reflect.Bool:
48return !g.Bool()
49case reflect.Complex64, reflect.Complex128:
50return g.Complex() == 0
51case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
52return g.Int() == 0
53case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
54return g.Uint() == 0
55case reflect.Float32, reflect.Float64:
56return g.Float() == 0
57case reflect.Struct:
58return false
59}
60}
61
62// coalesce returns the first non-empty value.
63func coalesce(v ...interface{}) interface{} {
64for _, val := range v {
65if !empty(val) {
66return val
67}
68}
69return 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.
74func all(v ...interface{}) bool {
75for _, val := range v {
76if empty(val) {
77return false
78}
79}
80return 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.
85func any(v ...interface{}) bool {
86for _, val := range v {
87if !empty(val) {
88return true
89}
90}
91return false
92}
93
94// fromJson decodes JSON into a structured value, ignoring errors.
95func fromJson(v string) interface{} {
96output, _ := mustFromJson(v)
97return output
98}
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, err
105}
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 "", err
117}
118return string(output), nil
119}
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 "", err
131}
132return string(output), nil
133}
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 "", err
152}
153return 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.
157func ternary(vt interface{}, vf interface{}, v bool) interface{} {
158if v {
159return vt
160}
161
162return vf
163}
164