/
treeContext.go
39 lines (32 loc) · 933 Bytes
/
treeContext.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
package dtree
import (
"context"
"fmt"
)
// contextKey Log Context Key
type contextKey string
const descriptionContext = contextKey("nodes-description")
// contextValue add value context on ctx
func contextValue(ctx context.Context, id int, key string, value interface{}, operator string, tvalue interface{}) context.Context {
description := fmt.Sprintf("%d : %s %v %s %v", id, key, value, operator, tvalue)
if v := ctx.Value(descriptionContext); v != nil {
s, ok := v.([]string)
if ok {
s = append(s, description)
ctx = context.WithValue(ctx, descriptionContext, s)
}
} else {
ctx = context.WithValue(ctx, descriptionContext, []string{description})
}
return ctx
}
// GetNodePathFromContext gets the node path from the context
func GetNodePathFromContext(ctx context.Context) []string {
if v := ctx.Value(descriptionContext); v != nil {
s, ok := v.([]string)
if ok {
return s
}
}
return nil
}