~handlerug/handlebot

ref: 5c3342e9f8fd6f47e7bc4ff496b631441fce047a handlebot/weather/context.go -rw-r--r-- 432 bytes
5c3342e9Umar Getagazov Update README.md 5 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package weather

import (
	"context"
	"errors"
)

var weatherCtxKey = &contextKey{"forecaster"}

type contextKey struct {
	name string
}

func Context(ctx context.Context, f *Forecaster) context.Context {
	return context.WithValue(ctx, weatherCtxKey, f)
}

func ForContext(ctx context.Context) *Forecaster {
	f, ok := ctx.Value(weatherCtxKey).(*Forecaster)
	if !ok {
		panic(errors.New("Invalid forecaster context"))
	}
	return f
}