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

context, vweb: add ability to set and get values on vweb.Context (#18564)

This commit is contained in:
Casper Kuethe
2023-06-27 00:25:45 +02:00
committed by GitHub
parent 7a9c885b31
commit 21d9730cde
6 changed files with 153 additions and 3 deletions

View File

@ -65,12 +65,41 @@ pub interface Any {}
pub interface Context {
deadline() ?time.Time
value(key Key) ?Any
str() string
mut:
done() chan int
err() IError
}
// str returns the `str` method of the corresponding Context struct
pub fn (ctx &Context) str() string {
// since `Context` is an interface we have to manually match every possible
// type that implements `Context` if we want to use a `Context` as a field in a struct
// since the `Context` interface has to implement its own `str` method.
match ctx {
BackgroundContext {
return ctx.str()
}
EmptyContext {
return ctx.str()
}
TodoContext {
return ctx.str()
}
CancelContext {
return ctx.str()
}
TimerContext {
return ctx.str()
}
ValueContext {
return ctx.str()
}
else {
return context_name(ctx)
}
}
}
fn context_name(ctx Context) string {
return typeof(ctx)
}

View File

@ -2,7 +2,7 @@ module context
fn test_background() {
ctx := background()
assert 'context.Background' == ctx.str()
assert '&context.Background' == ctx.str()
if _ := ctx.value('') {
panic('This should never happen')
}
@ -10,7 +10,7 @@ fn test_background() {
fn test_todo() {
ctx := todo()
assert 'context.TODO' == ctx.str()
assert '&context.TODO' == ctx.str()
if _ := ctx.value('') {
panic('This should never happen')
}