podman

Форк
0
56 строк · 1.4 Кб
1
package validate
2

3
import (
4
	"context"
5
)
6

7
// validateCtxKey is the key type of context key in this pkg
8
type validateCtxKey string
9

10
const (
11
	operationTypeKey validateCtxKey = "operationTypeKey"
12
)
13

14
type operationType string
15

16
const (
17
	request  operationType = "request"
18
	response operationType = "response"
19
	none     operationType = "none" // not specified in ctx
20
)
21

22
var operationTypeEnum = []operationType{request, response, none}
23

24
// WithOperationRequest returns a new context with operationType request
25
// in context value
26
func WithOperationRequest(ctx context.Context) context.Context {
27
	return withOperation(ctx, request)
28
}
29

30
// WithOperationRequest returns a new context with operationType response
31
// in context value
32
func WithOperationResponse(ctx context.Context) context.Context {
33
	return withOperation(ctx, response)
34
}
35

36
func withOperation(ctx context.Context, operation operationType) context.Context {
37
	return 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
42
func extractOperationType(ctx context.Context) operationType {
43
	v := ctx.Value(operationTypeKey)
44
	if v == nil {
45
		return none
46
	}
47
	res, ok := v.(operationType)
48
	if !ok {
49
		return none
50
	}
51
	// validate the value is in operation enum
52
	if err := Enum("", "", res, operationTypeEnum); err != nil {
53
		return none
54
	}
55
	return res
56
}
57

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.