-
Notifications
You must be signed in to change notification settings - Fork 13
/
context.go
171 lines (138 loc) · 3.57 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// Copyright (c) 2019 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.
package database
import (
"context"
)
// contextKey defines the key type for
// storing database types in a context.
type contextKey int
const (
// buildKey defines the key type for
// storing a Build type in a context.
buildKey contextKey = iota
// logKey defines the key type for
// storing a Log type in a context.
logKey
// repoKey defines the key type for
// storing a Repo type in a context.
repoKey
// secretKey defines the key type for
// storing a Secret type in a context.
secretKey
// stepKey defines the key type for
// storing a Step type in a context.
stepKey
// userKey defines the key type for
// storing a User type in a context.
userKey
)
// BuildFromContext retrieves the Build type from the context.
func BuildFromContext(c context.Context) *Build {
// get build value from context
v := c.Value(buildKey)
if v == nil {
return nil
}
// cast build value to expected Build type
b, ok := v.(*Build)
if !ok {
return nil
}
return b
}
// BuildWithContext inserts the Build type to the context.
func BuildWithContext(c context.Context, b *Build) context.Context {
return context.WithValue(c, buildKey, b)
}
// LogFromContext retrieves the Log type from the context.
func LogFromContext(c context.Context) *Log {
// get log value from context
v := c.Value(logKey)
if v == nil {
return nil
}
// cast log value to expected Log type
l, ok := v.(*Log)
if !ok {
return nil
}
return l
}
// LogWithContext inserts the Log type to the context.
func LogWithContext(c context.Context, l *Log) context.Context {
return context.WithValue(c, logKey, l)
}
// RepoFromContext retrieves the Repo type from the context.
func RepoFromContext(c context.Context) *Repo {
// get repo value from context
v := c.Value(repoKey)
if v == nil {
return nil
}
// cast repo value to expected Repo type
r, ok := v.(*Repo)
if !ok {
return nil
}
return r
}
// RepoWithContext inserts the Repo type to the context.
func RepoWithContext(c context.Context, r *Repo) context.Context {
return context.WithValue(c, repoKey, r)
}
// SecretFromContext retrieves the Secret type from the context.
func SecretFromContext(c context.Context) *Secret {
// get secret value from context
v := c.Value(secretKey)
if v == nil {
return nil
}
// cast secret value to expected Secret type
s, ok := v.(*Secret)
if !ok {
return nil
}
return s
}
// SecretWithContext inserts the Secret type to the context.
func SecretWithContext(c context.Context, s *Secret) context.Context {
return context.WithValue(c, secretKey, s)
}
// StepFromContext retrieves the Step type from the context.
func StepFromContext(c context.Context) *Step {
// get step value from context
v := c.Value(stepKey)
if v == nil {
return nil
}
// cast step value to expected Step type
s, ok := v.(*Step)
if !ok {
return nil
}
return s
}
// StepWithContext inserts the Step type to the context.
func StepWithContext(c context.Context, s *Step) context.Context {
return context.WithValue(c, stepKey, s)
}
// UserFromContext retrieves the User type from the context.
func UserFromContext(c context.Context) *User {
// get user value from context
v := c.Value(userKey)
if v == nil {
return nil
}
// cast user value to expected User type
u, ok := v.(*User)
if !ok {
return nil
}
return u
}
// UserWithContext inserts the User type to the context.
func UserWithContext(c context.Context, u *User) context.Context {
return context.WithValue(c, userKey, u)
}