withCreationState adds a new creationState to the given context. If the context already has one, it returns the same context.
(parent context.Context)
| 42 | // withCreationState adds a new creationState to the given context. |
| 43 | // If the context already has one, it returns the same context. |
| 44 | func withCreationState(parent context.Context) context.Context { |
| 45 | if parent == nil { |
| 46 | parent = context.Background() |
| 47 | } |
| 48 | |
| 49 | val := parent.Value(ctxCreationStateContextKey) |
| 50 | if val != nil { |
| 51 | return parent |
| 52 | } |
| 53 | |
| 54 | state := newCreationState() |
| 55 | return context.WithValue(parent, ctxCreationStateContextKey, state) |
| 56 | } |
| 57 | |
| 58 | // creationStateFromContext gets the creationState from the context. |
| 59 | // It assumes the context contains a valid value. |