-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcontext.go
112 lines (95 loc) · 3.49 KB
/
context.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package gonertia
import (
"context"
)
type contextKey int
const (
templateDataContextKey = contextKey(iota + 1)
propsContextKey
validationErrorsContextKey
encryptHistoryContextKey
clearHistoryContextKey
)
// SetTemplateData sets template data to the passed context.
func SetTemplateData(ctx context.Context, templateData TemplateData) context.Context {
return context.WithValue(ctx, templateDataContextKey, templateData)
}
// SetTemplateDatum sets single template data item to the passed context.
func SetTemplateDatum(ctx context.Context, key string, val any) context.Context {
templateData := TemplateDataFromContext(ctx)
templateData[key] = val
return SetTemplateData(ctx, templateData)
}
// TemplateDataFromContext returns template data from the context.
func TemplateDataFromContext(ctx context.Context) TemplateData {
templateData, ok := ctx.Value(templateDataContextKey).(TemplateData)
if ok {
return templateData
}
return TemplateData{}
}
// SetProps sets props values to the passed context.
func SetProps(ctx context.Context, props Props) context.Context {
return context.WithValue(ctx, propsContextKey, props)
}
// SetProp sets prop value to the passed context.
func SetProp(ctx context.Context, key string, val any) context.Context {
props := PropsFromContext(ctx)
props[key] = val
return SetProps(ctx, props)
}
// PropsFromContext returns props from the context.
func PropsFromContext(ctx context.Context) Props {
props, ok := ctx.Value(propsContextKey).(Props)
if ok {
return props
}
return Props{}
}
// SetValidationErrors sets validation errors to the passed context.
func SetValidationErrors(ctx context.Context, errors ValidationErrors) context.Context {
return context.WithValue(ctx, validationErrorsContextKey, errors)
}
// AddValidationErrors appends validation errors to the passed context.
func AddValidationErrors(ctx context.Context, errors ValidationErrors) context.Context {
validationErrors := ValidationErrorsFromContext(ctx)
for key, val := range errors {
validationErrors[key] = val
}
return SetValidationErrors(ctx, validationErrors)
}
// SetValidationError sets validation error to the passed context.
func SetValidationError(ctx context.Context, key string, msg string) context.Context {
validationErrors := ValidationErrorsFromContext(ctx)
validationErrors[key] = msg
return SetValidationErrors(ctx, validationErrors)
}
// ValidationErrorsFromContext returns validation errors from the context.
func ValidationErrorsFromContext(ctx context.Context) ValidationErrors {
validationErrors, ok := ctx.Value(validationErrorsContextKey).(ValidationErrors)
if ok {
return validationErrors
}
return ValidationErrors{}
}
// SetEncryptHistory enables or disables history encryption.
func SetEncryptHistory(ctx context.Context, encrypt ...bool) context.Context {
return context.WithValue(ctx, encryptHistoryContextKey, firstOr[bool](encrypt, true))
}
// EncryptHistoryFromContext returns history encryption value from the context.
func EncryptHistoryFromContext(ctx context.Context) (bool, bool) {
encryptHistory, ok := ctx.Value(encryptHistoryContextKey).(bool)
return encryptHistory, ok
}
// ClearHistory cleaning history state.
func ClearHistory(ctx context.Context) context.Context {
return context.WithValue(ctx, clearHistoryContextKey, true)
}
// ClearHistoryFromContext returns clear history value from the context.
func ClearHistoryFromContext(ctx context.Context) bool {
clearHistory, ok := ctx.Value(clearHistoryContextKey).(bool)
if ok {
return clearHistory
}
return false
}