-
-
Notifications
You must be signed in to change notification settings - Fork 180
/
Copy pathparser.go
67 lines (53 loc) · 1.33 KB
/
parser.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
56
57
58
59
60
61
62
63
64
65
66
67
package utils
import (
"errors"
"strings"
"time"
)
// ParseDurationInSeconds parses input s, removes ms/us/ns and returns result duration
func ParseDurationInSeconds(s string) (time.Duration, error) {
d, err := time.ParseDuration(s)
if err != nil {
return 0, err
}
d = d.Truncate(time.Second)
if d <= 0 {
return 0, errors.New(`duration must be greater than 0s`)
}
return d, nil
}
// Helper function to parse string array values
func ParseStringArray(value string) []*string {
if value == "" {
return nil
}
splitValues := strings.Split(value, "|")
var result []*string
for _, s := range splitValues {
temp := s
result = append(result, &temp)
}
return result
}
// Helper function to parse reference string array values
func ParseReferenceStringArray(value *string) []string {
if value == nil {
return nil
}
// Dereference the pointer to get the string value
strValue := *value
// Remove JSON brackets
strValue = strings.Trim(strValue, "{}")
splitValues := strings.Split(strValue, ",")
var result []string
for _, s := range splitValues {
// Split each key-value pair by colon ':'
parts := strings.SplitN(s, ":", 2)
if len(parts) > 0 {
unquoted := strings.Trim(strings.TrimSpace(parts[0]), `"`)
// Extract and append only the key (UUID)
result = append(result, unquoted)
}
}
return result
}