2021-04-16 11:54:28 +03:00
|
|
|
// This module defines the Context type, which carries deadlines, cancellation signals,
|
|
|
|
// and other request-scoped values across API boundaries and between processes.
|
2021-09-27 17:52:20 +03:00
|
|
|
// Based on: https://github.com/golang/go/tree/master/src/context
|
2021-04-16 11:54:28 +03:00
|
|
|
// Last commit: https://github.com/golang/go/commit/52bf14e0e8bdcd73f1ddfb0c4a1d0200097d3ba2
|
2021-04-12 19:32:51 +03:00
|
|
|
module context
|
|
|
|
|
|
|
|
import time
|
|
|
|
|
|
|
|
// A ValueContext carries a key-value pair. It implements Value for that key and
|
|
|
|
// delegates all other calls to the embedded Context.
|
|
|
|
pub struct ValueContext {
|
2021-09-27 17:52:20 +03:00
|
|
|
key Key
|
|
|
|
value Any
|
2021-04-12 19:32:51 +03:00
|
|
|
mut:
|
|
|
|
context Context
|
|
|
|
}
|
|
|
|
|
|
|
|
// with_value returns a copy of parent in which the value associated with key is
|
|
|
|
// val.
|
|
|
|
//
|
|
|
|
// Use context Values only for request-scoped data that transits processes and
|
|
|
|
// APIs, not for passing optional parameters to functions.
|
|
|
|
//
|
|
|
|
// The provided key must be comparable and should not be of type
|
|
|
|
// string or any other built-in type to avoid collisions between
|
|
|
|
// packages using context. Users of with_value should define their own
|
|
|
|
// types for keys
|
2021-09-27 17:52:20 +03:00
|
|
|
pub fn with_value(parent Context, key Key, value Any) Context {
|
2021-04-12 19:32:51 +03:00
|
|
|
return &ValueContext{
|
|
|
|
context: parent
|
|
|
|
key: key
|
|
|
|
value: value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-27 17:52:20 +03:00
|
|
|
pub fn (ctx &ValueContext) deadline() ?time.Time {
|
2021-04-12 19:32:51 +03:00
|
|
|
return ctx.context.deadline()
|
|
|
|
}
|
|
|
|
|
2021-09-27 17:52:20 +03:00
|
|
|
pub fn (ctx &ValueContext) done() chan int {
|
2021-04-12 19:32:51 +03:00
|
|
|
return ctx.context.done()
|
|
|
|
}
|
|
|
|
|
2021-09-27 17:52:20 +03:00
|
|
|
pub fn (ctx &ValueContext) err() IError {
|
2021-04-12 19:32:51 +03:00
|
|
|
return ctx.context.err()
|
|
|
|
}
|
|
|
|
|
2021-09-27 17:52:20 +03:00
|
|
|
pub fn (ctx &ValueContext) value(key Key) ?Any {
|
2021-04-12 19:32:51 +03:00
|
|
|
if ctx.key == key {
|
|
|
|
return ctx.value
|
|
|
|
}
|
|
|
|
return ctx.context.value(key)
|
|
|
|
}
|
|
|
|
|
2021-09-27 17:52:20 +03:00
|
|
|
pub fn (ctx &ValueContext) str() string {
|
2021-04-12 19:32:51 +03:00
|
|
|
return context_name(ctx.context) + '.with_value'
|
|
|
|
}
|