/
hasprefix.go
54 lines (38 loc) · 914 Bytes
/
hasprefix.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
package path
import (
"strconv"
"strings"
"time"
"cloud.google.com/go/civil"
)
func HasPrefix(obj any, path string, matchStr string) (bool, error) {
return op(obj, path, matchStr, hasPrefix)
}
func hasPrefix(obj, match any, matchStr string) bool {
var objStr string
switch objt := obj.(type) {
case int:
objStr = strconv.FormatInt(int64(objt), 10)
case int64:
objStr = strconv.FormatInt(objt, 10)
case uint:
objStr = strconv.FormatUint(uint64(objt), 10)
case uint64:
objStr = strconv.FormatUint(objt, 10)
case float32:
objStr = strconv.FormatFloat(float64(objt), 'f', -1, 32)
case float64:
objStr = strconv.FormatFloat(objt, 'f', -1, 64)
case string:
objStr = objt
case bool:
objStr = strconv.FormatBool(objt)
case time.Time:
objStr = objt.String()
case civil.Date:
objStr = objt.String()
default:
panic(obj)
}
return strings.HasPrefix(objStr, matchStr)
}