generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 7
/
request_key.go
64 lines (54 loc) · 1.49 KB
/
request_key.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
package model
import (
"fmt"
"regexp"
"strings"
)
// A RequestKey represents an inbound request into the cluster.
type RequestKey = KeyType[RequestKeyPayload, *RequestKeyPayload]
func NewRequestKey(origin Origin, key string) RequestKey {
return newKey[RequestKeyPayload](string(origin), key)
}
func ParseRequestKey(name string) (RequestKey, error) {
return parseKey[RequestKeyPayload](name)
}
// Origin of a request.
type Origin string
const (
OriginIngress Origin = "ingress"
OriginCron Origin = "cron"
OriginPubsub Origin = "pubsub"
)
func ParseOrigin(origin string) (Origin, error) {
switch origin {
case "ingress":
return OriginIngress, nil
case "cron":
return OriginCron, nil
case "pubsub":
return OriginPubsub, nil
default:
return "", fmt.Errorf("unknown origin %q", origin)
}
}
var requestKeyNormaliserRe = regexp.MustCompile("[^a-zA-Z0-9]+")
type RequestKeyPayload struct {
Origin Origin
Key string
}
func (r *RequestKeyPayload) Kind() string { return "req" }
func (r *RequestKeyPayload) String() string { return fmt.Sprintf("%s-%s", r.Origin, r.Key) }
func (r *RequestKeyPayload) Parse(parts []string) error {
if len(parts) < 2 {
return fmt.Errorf("expected <origin>-<key> but got %q", strings.Join(parts, "-"))
}
origin, err := ParseOrigin(parts[0])
if err != nil {
return err
}
r.Origin = origin
key := strings.Join(parts[1:], "-")
r.Key = requestKeyNormaliserRe.ReplaceAllString(key, "-")
return nil
}
func (r *RequestKeyPayload) RandomBytes() int { return 10 }