-
Notifications
You must be signed in to change notification settings - Fork 145
/
action.go
68 lines (55 loc) · 1.43 KB
/
action.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
68
package types
import (
"fmt"
"strings"
"unicode"
"unicode/utf8"
)
type Action struct {
// Required. The service that this belongs to
Service string
// Required. The verb to perform.
Verb string
// Optional. A way to unique identify the step.
Subresource string
}
func (o Action) String() string {
if o.Subresource != "" {
return fmt.Sprintf("%s:%s:%s", o.Service, o.Verb, o.Subresource)
}
return o.IntegrationVerbString()
}
func (o Action) IntegrationVerbString() string {
return fmt.Sprintf("%s:%s", o.Service, o.Verb)
}
// ParseActionID parses an action ID into its constituent parts.
func ParseActionID(actionID string) (Action, error) {
parts := strings.Split(actionID, ":")
numParts := len(parts)
if numParts < 2 || numParts > 3 {
return Action{}, fmt.Errorf("invalid action id %s, must have at least 2 strings separated : (colon)", actionID)
}
Service := firstToLower(parts[0])
verb := strings.ToLower(parts[1])
var subresource string
if numParts == 3 {
subresource = firstToLower(parts[2])
}
return Action{
Service: Service,
Verb: verb,
Subresource: subresource,
}, nil
}
// source: https://stackoverflow.com/questions/75988064/make-first-letter-of-string-lower-case-in-golang
func firstToLower(s string) string {
r, size := utf8.DecodeRuneInString(s)
if r == utf8.RuneError && size <= 1 {
return s
}
lc := unicode.ToLower(r)
if r == lc {
return s
}
return string(lc) + s[size:]
}