aboutsummaryrefslogtreecommitdiff
path: root/common/context.go
blob: 3a7926454d2d9acdd5a0e7f0e4da2a101e2cc57e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package common

import (
	"context"

	"gorm.io/gorm"
)

type key uint8

const (
	keyDB     key = 0
	keyDebug  key = 1
	keyAuthor key = 2
)

func SetDB(ctx context.Context, db *gorm.DB) context.Context {
	return context.WithValue(ctx, keyDB, db)
}

func GetDB(ctx context.Context) *gorm.DB {
	raw := ctx.Value(keyDB)
	if raw == nil {
		return nil
	}
	return raw.(*gorm.DB)
}

func SetDebug(ctx context.Context, b bool) context.Context {
	return context.WithValue(ctx, keyDebug, b)
}

func IsDebug(ctx context.Context) bool {
	raw := ctx.Value(keyDebug)
	if raw == nil {
		return false
	}
	return raw.(bool)
}

func SetAuthor(ctx context.Context, s string) context.Context {
	return context.WithValue(ctx, keyAuthor, s)
}

func GetAuthor(ctx context.Context) string {
	raw := ctx.Value(keyAuthor)
	if raw == nil {
		return ""
	}
	return raw.(string)
}