forked from kyverno/kyverno
-
Notifications
You must be signed in to change notification settings - Fork 2
/
common.go
36 lines (32 loc) · 812 Bytes
/
common.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
package validate
import (
"fmt"
"strconv"
)
// convertToString converts value to string
func convertToString(value interface{}) (string, error) {
switch typed := value.(type) {
case string:
return string(typed), nil
case float64:
return fmt.Sprintf("%f", typed), nil
case int64:
return strconv.FormatInt(typed, 10), nil
case int:
return strconv.Itoa(typed), nil
default:
return "", fmt.Errorf("Could not convert %T to string", value)
}
}
func getRawKeyIfWrappedWithAttributes(str string) string {
if len(str) < 2 {
return str
}
if str[0] == '(' && str[len(str)-1] == ')' {
return str[1 : len(str)-1]
} else if (str[0] == '$' || str[0] == '^' || str[0] == '+' || str[0] == '=') && (str[1] == '(' && str[len(str)-1] == ')') {
return str[2 : len(str)-1]
} else {
return str
}
}