-
Notifications
You must be signed in to change notification settings - Fork 330
/
Copy pathutil.go
55 lines (43 loc) · 1.25 KB
/
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
49
50
51
52
53
54
55
package keycloak
import (
"strconv"
"strings"
"time"
)
func getIdFromLocationHeader(locationHeader string) string {
parts := strings.Split(locationHeader, "/")
return parts[len(parts)-1]
}
// Converts duration string to a string representing the number of milliseconds, which is used by the Keycloak API
// Ex: "1h" => "3600000"
func getMillisecondsFromDurationString(s string) (string, error) {
duration, err := time.ParseDuration(s)
if err != nil {
return "", err
}
return strconv.Itoa(int(duration.Seconds() * 1000)), nil
}
// GetDurationStringFromMilliseconds converts a string representing milliseconds from Keycloak API to a duration string used by the provider
// Ex: "3600000" => "1h0m0s"
func GetDurationStringFromMilliseconds(milliseconds string) (string, error) {
ms, err := strconv.Atoi(milliseconds)
if err != nil {
return "", err
}
return (time.Duration(ms) * time.Millisecond).String(), nil
}
func parseBoolAndTreatEmptyStringAsFalse(b string) (bool, error) {
if b == "" {
return false, nil
}
return strconv.ParseBool(b)
}
func atoiAndTreatEmptyStringAsZero(s string) (int, error) {
if s == "" {
return 0, nil
}
return strconv.Atoi(s)
}
func escapeBackslashes(s string) string {
return strings.ReplaceAll(s, "\\", "\\\\")
}