-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate.go
More file actions
74 lines (71 loc) · 2.36 KB
/
validate.go
File metadata and controls
74 lines (71 loc) · 2.36 KB
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
69
70
71
72
73
74
package pgqueue
import (
"encoding/json"
"fmt"
)
func validateJobSpec[P any](s JobSpec[P]) error {
if s.Queue == "" {
return fmt.Errorf("pgqueue: JobSpec.Queue must not be empty")
}
if len(s.Queue) > 128 {
return fmt.Errorf("pgqueue: JobSpec.Queue exceeds 128 characters")
}
if s.PollInterval < 0 {
return fmt.Errorf("pgqueue: JobSpec.PollInterval must not be negative")
}
if s.MaxRetries < 0 {
return fmt.Errorf("pgqueue: JobSpec.MaxRetries must not be negative")
}
if s.RetryDelay < 0 {
return fmt.Errorf("pgqueue: JobSpec.RetryDelay must not be negative")
}
if s.LeaseDuration < 0 {
return fmt.Errorf("pgqueue: JobSpec.LeaseDuration must not be negative")
}
if s.FinalizeBuffer < 0 {
return fmt.Errorf("pgqueue: JobSpec.FinalizeBuffer must not be negative")
}
if s.LeaseDuration > 0 && s.FinalizeBuffer >= s.LeaseDuration {
return fmt.Errorf("pgqueue: JobSpec.FinalizeBuffer (%v) must be less than LeaseDuration (%v)", s.FinalizeBuffer, s.LeaseDuration)
}
return nil
}
func validateTickerSpec[P any](s TickerSpec[P]) error {
if s.Queue == "" {
return fmt.Errorf("pgqueue: TickerSpec.Queue must not be empty")
}
if len(s.Queue) > 128 {
return fmt.Errorf("pgqueue: TickerSpec.Queue exceeds 128 characters")
}
if s.Key == "" {
return fmt.Errorf("pgqueue: TickerSpec.Key must not be empty")
}
if len(s.Key) > 128 {
return fmt.Errorf("pgqueue: TickerSpec.Key exceeds 128 characters")
}
if s.Every <= 0 {
return fmt.Errorf("pgqueue: TickerSpec.Every must be positive")
}
if s.PollInterval < 0 {
return fmt.Errorf("pgqueue: TickerSpec.PollInterval must not be negative")
}
if s.MaxRetries < 0 {
return fmt.Errorf("pgqueue: TickerSpec.MaxRetries must not be negative")
}
if s.RetryDelay < 0 {
return fmt.Errorf("pgqueue: TickerSpec.RetryDelay must not be negative")
}
if s.LeaseDuration < 0 {
return fmt.Errorf("pgqueue: TickerSpec.LeaseDuration must not be negative")
}
if s.FinalizeBuffer < 0 {
return fmt.Errorf("pgqueue: TickerSpec.FinalizeBuffer must not be negative")
}
if s.LeaseDuration > 0 && s.FinalizeBuffer >= s.LeaseDuration {
return fmt.Errorf("pgqueue: TickerSpec.FinalizeBuffer (%v) must be less than LeaseDuration (%v)", s.FinalizeBuffer, s.LeaseDuration)
}
if _, err := json.Marshal(s.InitialPayload); err != nil {
return fmt.Errorf("pgqueue: TickerSpec.InitialPayload is not JSON-marshalable: %w", err)
}
return nil
}