podman
56 строк · 1.4 Кб
1package validate
2
3import (
4"context"
5)
6
7// validateCtxKey is the key type of context key in this pkg
8type validateCtxKey string
9
10const (
11operationTypeKey validateCtxKey = "operationTypeKey"
12)
13
14type operationType string
15
16const (
17request operationType = "request"
18response operationType = "response"
19none operationType = "none" // not specified in ctx
20)
21
22var operationTypeEnum = []operationType{request, response, none}
23
24// WithOperationRequest returns a new context with operationType request
25// in context value
26func WithOperationRequest(ctx context.Context) context.Context {
27return withOperation(ctx, request)
28}
29
30// WithOperationRequest returns a new context with operationType response
31// in context value
32func WithOperationResponse(ctx context.Context) context.Context {
33return withOperation(ctx, response)
34}
35
36func withOperation(ctx context.Context, operation operationType) context.Context {
37return context.WithValue(ctx, operationTypeKey, operation)
38}
39
40// extractOperationType extracts the operation type from ctx
41// if not specified or of unknown value, return none operation type
42func extractOperationType(ctx context.Context) operationType {
43v := ctx.Value(operationTypeKey)
44if v == nil {
45return none
46}
47res, ok := v.(operationType)
48if !ok {
49return none
50}
51// validate the value is in operation enum
52if err := Enum("", "", res, operationTypeEnum); err != nil {
53return none
54}
55return res
56}
57