1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

context: add a new context module, based on Golang's context, intended to be used in webservers (#9563)

This commit is contained in:
Ulises Jeremias Cornejo Fandos
2021-04-12 13:32:51 -03:00
committed by GitHub
parent b54188dfea
commit 07a6f4e445
10 changed files with 778 additions and 1 deletions

23
vlib/context/value_test.v Normal file
View File

@@ -0,0 +1,23 @@
import context
type ValueContextKey = string
// This example demonstrates how a value can be passed to the context
// and also how to retrieve it if it exists.
fn test_with_value() {
f := fn (ctx context.ValueContext, key ValueContextKey) string {
if value := ctx.value(key) {
if !isnil(value) {
return *(&string(value))
}
}
return 'key not found'
}
key := ValueContextKey('language')
value := 'VAL'
ctx := context.with_value(context.background(), key, &value)
assert value == f(ctx, key)
assert 'key not found' == f(ctx, ValueContextKey('color'))
}