context.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package common
  2. import (
  3. "context"
  4. "time"
  5. context2 "git.ikuban.com/server/kratos-utils/v2/transport/http/context"
  6. )
  7. func NewContext(parentCtx context.Context) context.Context {
  8. ctx := &AccountContext{ctx: parentCtx}
  9. return ctx
  10. }
  11. func NewContextWithAccountID(parentCtx context.Context, accountID int64) context.Context {
  12. ctx := &AccountContext{ctx: parentCtx}
  13. newCtx := context2.AppendToContext(ctx, "account_id", accountID)
  14. return newCtx
  15. }
  16. func NewContextWithValues(parentCtx context.Context, values map[string]interface{}) context.Context {
  17. ctx := &AccountContext{ctx: parentCtx}
  18. newCtx := context.Context(ctx)
  19. for k, v := range values {
  20. _k := k
  21. _v := v
  22. newCtx = context2.AppendToContext(ctx, _k, _v)
  23. }
  24. return newCtx
  25. }
  26. type AccountContext struct {
  27. ctx context.Context
  28. }
  29. func (this *AccountContext) Deadline() (deadline time.Time, ok bool) {
  30. return
  31. }
  32. func (*AccountContext) Done() <-chan struct{} {
  33. return nil
  34. }
  35. func (this *AccountContext) Err() error {
  36. return nil
  37. }
  38. func (this *AccountContext) Value(key interface{}) interface{} {
  39. return this.ctx.Value(key)
  40. }