generated from SAP/repository-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.go
48 lines (40 loc) · 887 Bytes
/
util.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
/*
SPDX-FileCopyrightText: 2023 SAP SE or an SAP affiliate company and component-operator-runtime contributors
SPDX-License-Identifier: Apache-2.0
*/
package component
import (
"crypto/sha256"
"encoding/hex"
"math/rand"
"strings"
"time"
)
// TODO: consolidate all the util files into an internal reuse package
func ref[T any](x T) *T {
return &x
}
func must[T any](x T, err error) T {
if err != nil {
panic(err)
}
return x
}
func sha256hex(data []byte) string {
sum := sha256.Sum256(data)
return hex.EncodeToString(sum[:])
}
func capitalize(s string) string {
if len(s) <= 1 {
return s
}
return strings.ToUpper(s[0:1]) + s[1:]
}
func addJitter(d *time.Duration, minPercent int, maxPercent int) {
if minPercent > maxPercent {
return
}
min := int64(minPercent)
max := int64(maxPercent)
*d = *d + time.Duration((rand.Int63n(max-min+1)+min)*int64(*d)/100)
}