-
Notifications
You must be signed in to change notification settings - Fork 1
/
kw_sdk.go
89 lines (75 loc) · 2.33 KB
/
kw_sdk.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package main
//nolint:godox
// TODO: figure out if it's worth to move this to a dedicated library
// Message is the optional string used to build validation responses
type Message string
// Code is the optional error code associated with validation responses
type Code uint16
const (
// NoMessage can be used when building a response that doesn't have any
// message to be shown to the user
NoMessage Message = ""
// NoCode can be used when building a response that doesn't have any
// error code to be shown to the user
NoCode Code = 0
)
// ValidationResponse defines the response given when validating a request
type ValidationResponse struct {
Accepted bool `json:"accepted"`
// Optional - ignored if accepted
Message *string `json:"message,omitempty"`
// Optional - ignored if accepted
Code *uint16 `json:"code,omitempty"`
}
// SettingsValidationResponse is the response sent by a policy when validating
// its settings
type SettingsValidationResponse struct {
Valid bool `json:"valid"`
// Optional - ignored if valid
Message *string `json:"message,omitempty"`
}
// AcceptRequest can be used inside of the `validate` function to accept the
// incoming request
func AcceptRequest() ValidationResponse {
return ValidationResponse{
Accepted: true,
}
}
// RejectRequest can be used inside of the `validate` function to reject the
// incoming request
// * `message`: optional message to show to the user
// * `code`: optional error code to show to the user
func RejectRequest(message Message, code Code) ValidationResponse {
response := ValidationResponse{
Accepted: false,
}
if message != NoMessage {
msg := string(message)
response.Message = &msg
}
if code != NoCode {
c := uint16(code)
response.Code = &c
}
return response
}
// AcceptSettings be used inside of the `validateSettings` function to accept the
// incoming settings
func AcceptSettings() SettingsValidationResponse {
return SettingsValidationResponse{
Valid: true,
}
}
// RejectSettings can be used inside of the `validate_settings` function to
// mark the user provided settings as invalid
// * `message`: optional message to show to the user
func RejectSettings(message Message) SettingsValidationResponse {
response := SettingsValidationResponse{
Valid: false,
}
if message != NoMessage {
msg := string(message)
response.Message = &msg
}
return response
}