Этот коммит содержится в:
Ben Schumacher
2023-11-06 12:26:17 +01:00
коммит произвёл GitHub
родитель 366d1613b7
Коммит 486e836b83
36 изменённых файлов: 191 добавлений и 172 удалений

Просмотреть файл

@@ -22,8 +22,7 @@ type Context struct {
userAgent string
acceptLanguage string
logger mlog.LoggerIFace
context context.Context
context context.Context
}
func NewContext(ctx context.Context, requestId, ipAddress, xForwardedFor, path, userAgent, acceptLanguage string, t i18n.TranslateFunc) *Context {
@@ -54,8 +53,8 @@ func TestContext(t testing.TB) *Context {
return EmptyContext(logger)
}
// Clone creates a shallow copy of Context, allowing clones to apply per-request changes.
func (c *Context) Clone() CTX {
// clone creates a shallow copy of Context, allowing clones to apply per-request changes.
func (c *Context) clone() *Context {
cCopy := *c
return &cCopy
}
@@ -63,6 +62,9 @@ func (c *Context) Clone() CTX {
func (c *Context) T(translationID string, args ...any) string {
return c.t(translationID, args...)
}
func (c *Context) GetT() i18n.TranslateFunc {
return c.t
}
func (c *Context) Session() *model.Session {
return &c.session
}
@@ -84,55 +86,71 @@ func (c *Context) UserAgent() string {
func (c *Context) AcceptLanguage() string {
return c.acceptLanguage
}
func (c *Context) Logger() mlog.LoggerIFace {
return c.logger
}
func (c *Context) Context() context.Context {
return c.context
}
func (c *Context) SetSession(s *model.Session) {
c.session = *s
func (c *Context) WithT(t i18n.TranslateFunc) CTX {
rctx := c.clone()
rctx.t = t
return rctx
}
func (c *Context) WithSession(s *model.Session) CTX {
rctx := c.clone()
rctx.session = *s
return rctx
}
func (c *Context) WithRequestId(s string) CTX {
rctx := c.clone()
rctx.requestId = s
return rctx
}
func (c *Context) WithIPAddress(s string) CTX {
rctx := c.clone()
rctx.ipAddress = s
return rctx
}
func (c *Context) WithXForwardedFor(s string) CTX {
rctx := c.clone()
rctx.xForwardedFor = s
return rctx
}
func (c *Context) WithPath(s string) CTX {
rctx := c.clone()
rctx.path = s
return rctx
}
func (c *Context) WithUserAgent(s string) CTX {
rctx := c.clone()
rctx.userAgent = s
return rctx
}
func (c *Context) WithAcceptLanguage(s string) CTX {
rctx := c.clone()
rctx.acceptLanguage = s
return rctx
}
func (c *Context) WithContext(ctx context.Context) CTX {
rctx := c.clone()
rctx.context = ctx
return rctx
}
func (c *Context) WithLogger(logger mlog.LoggerIFace) CTX {
rctx := c.clone()
rctx.logger = logger
return rctx
}
func (c *Context) SetT(t i18n.TranslateFunc) {
c.t = t
}
func (c *Context) SetRequestId(s string) {
c.requestId = s
}
func (c *Context) SetIPAddress(s string) {
c.ipAddress = s
}
func (c *Context) SetXForwardedFor(s string) {
c.xForwardedFor = s
}
func (c *Context) SetUserAgent(s string) {
c.userAgent = s
}
func (c *Context) SetAcceptLanguage(s string) {
c.acceptLanguage = s
}
func (c *Context) SetPath(s string) {
c.path = s
}
func (c *Context) SetContext(ctx context.Context) {
c.context = ctx
}
func (c *Context) GetT() i18n.TranslateFunc {
return c.t
}
func (c *Context) SetLogger(logger mlog.LoggerIFace) {
c.logger = logger
}
func (c *Context) Logger() mlog.LoggerIFace {
return c.logger
func (c *Context) With(f func(ctx CTX) CTX) CTX {
return f(c)
}
type CTX interface {
Clone() CTX
T(string, ...interface{}) string
GetT() i18n.TranslateFunc
Session() *model.Session
RequestId() string
IPAddress() string
@@ -140,16 +158,17 @@ type CTX interface {
Path() string
UserAgent() string
AcceptLanguage() string
Context() context.Context
SetSession(s *model.Session)
SetT(i18n.TranslateFunc)
SetRequestId(string)
SetIPAddress(string)
SetUserAgent(string)
SetAcceptLanguage(string)
SetPath(string)
SetContext(ctx context.Context)
GetT() i18n.TranslateFunc
SetLogger(mlog.LoggerIFace)
Logger() mlog.LoggerIFace
Context() context.Context
WithT(i18n.TranslateFunc) CTX
WithSession(s *model.Session) CTX
WithRequestId(string) CTX
WithIPAddress(string) CTX
WithXForwardedFor(string) CTX
WithPath(string) CTX
WithUserAgent(string) CTX
WithAcceptLanguage(string) CTX
WithLogger(mlog.LoggerIFace) CTX
WithContext(ctx context.Context) CTX
With(func(ctx CTX) CTX) CTX
}