/
context.go
30 lines (24 loc) · 1.01 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
// Copyright © 2020 by PACE Telematics GmbH. All rights reserved.
// Created at 2020/12/18 by Vincent Landgraf
package redact
import "context"
type patternRedactorKey struct{}
// WithContext allows storing the PatternRedactor inside a context for passing it on
func (r *PatternRedactor) WithContext(ctx context.Context) context.Context {
return context.WithValue(ctx, patternRedactorKey{}, r)
}
// Ctx returns the PatternRedactor stored within the context. If no redactor
// has been defined, an empty redactor is returned that does nothing
func Ctx(ctx context.Context) *PatternRedactor {
if rd, ok := ctx.Value(patternRedactorKey{}).(*PatternRedactor); ok {
return rd.Clone()
}
return NewPatternRedactor(RedactionSchemeDoNothing())
}
// ContextTransfer copies a request representation from one context to another.
func ContextTransfer(ctx, targetCtx context.Context) context.Context {
if redactor := Ctx(ctx); redactor != nil {
return context.WithValue(targetCtx, patternRedactorKey{}, redactor)
}
return targetCtx
}