mirror of
https://github.com/retailcrm/mg-transport-core.git
synced 2024-11-22 13:16:04 +03:00
Sentry update, go 1.15 support
* Clone() method in Localizer component allows to clone it just like LocalizationMiddleware does. * RavenClientInterface contains all *raven.Client method, which allows external use without type casting. * Tests and godoc fix for Go 1.15.
This commit is contained in:
parent
66b13fc30b
commit
c79b3b0c0f
@ -5,6 +5,7 @@ go:
|
|||||||
- '1.12'
|
- '1.12'
|
||||||
- '1.13'
|
- '1.13'
|
||||||
- '1.14'
|
- '1.14'
|
||||||
|
- '1.15'
|
||||||
before_install:
|
before_install:
|
||||||
- go mod tidy
|
- go mod tidy
|
||||||
script:
|
script:
|
||||||
|
@ -85,6 +85,23 @@ func DefaultLocalizerMatcher() language.Matcher {
|
|||||||
return language.NewMatcher(DefaultLanguages)
|
return language.NewMatcher(DefaultLanguages)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Clone *core.Localizer. Clone shares it's translations with the parent localizer. Language tag will not be shared.
|
||||||
|
// Because of that you can change clone's language without affecting parent localizer.
|
||||||
|
// This method should be used when LocalizationMiddleware is not feasible (outside of *gin.HandlerFunc).
|
||||||
|
func (l *Localizer) Clone() *Localizer {
|
||||||
|
clone := &Localizer{
|
||||||
|
i18nStorage: l.i18nStorage,
|
||||||
|
TranslationsBox: l.TranslationsBox,
|
||||||
|
LocaleMatcher: l.LocaleMatcher,
|
||||||
|
LanguageTag: l.LanguageTag,
|
||||||
|
TranslationsPath: l.TranslationsPath,
|
||||||
|
loadMutex: l.loadMutex,
|
||||||
|
}
|
||||||
|
clone.SetLanguage(DefaultLanguage)
|
||||||
|
|
||||||
|
return clone
|
||||||
|
}
|
||||||
|
|
||||||
// LocalizationMiddleware returns gin.HandlerFunc which will set localizer language by Accept-Language header
|
// LocalizationMiddleware returns gin.HandlerFunc which will set localizer language by Accept-Language header
|
||||||
// Result Localizer instance will share it's internal data (translations, bundles, etc) with instance which was used
|
// Result Localizer instance will share it's internal data (translations, bundles, etc) with instance which was used
|
||||||
// to append middleware to gin.
|
// to append middleware to gin.
|
||||||
@ -92,18 +109,11 @@ func DefaultLocalizerMatcher() language.Matcher {
|
|||||||
// i18n.Bundle methods (those aren't goroutine-safe to use).
|
// i18n.Bundle methods (those aren't goroutine-safe to use).
|
||||||
// Usage:
|
// Usage:
|
||||||
// engine := gin.New()
|
// engine := gin.New()
|
||||||
// localizer := NewLocalizer("en", DefaultLocalizerBundle(), DefaultLocalizerMatcher(), "translations")
|
// localizer := NewLocalizer("en", DefaultLocalizerMatcher(), "translations")
|
||||||
// engine.Use(localizer.LocalizationMiddleware())
|
// engine.Use(localizer.LocalizationMiddleware())
|
||||||
func (l *Localizer) LocalizationMiddleware() gin.HandlerFunc {
|
func (l *Localizer) LocalizationMiddleware() gin.HandlerFunc {
|
||||||
return func(c *gin.Context) {
|
return func(c *gin.Context) {
|
||||||
clone := &Localizer{
|
clone := l.Clone()
|
||||||
i18nStorage: l.i18nStorage,
|
|
||||||
TranslationsBox: l.TranslationsBox,
|
|
||||||
LocaleMatcher: l.LocaleMatcher,
|
|
||||||
LanguageTag: l.LanguageTag,
|
|
||||||
TranslationsPath: l.TranslationsPath,
|
|
||||||
loadMutex: l.loadMutex,
|
|
||||||
}
|
|
||||||
clone.SetLocale(c.GetHeader("Accept-Language"))
|
clone.SetLocale(c.GetHeader("Accept-Language"))
|
||||||
c.Set(LocalizerContextKey, clone)
|
c.Set(LocalizerContextKey, clone)
|
||||||
}
|
}
|
||||||
|
@ -157,6 +157,19 @@ func (l *LocalizerTest) Test_GetLocalizedMessage() {
|
|||||||
assert.Equal(l.T(), "Test message", message)
|
assert.Equal(l.T(), "Test message", message)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (l *LocalizerTest) Test_Clone() {
|
||||||
|
defer func() {
|
||||||
|
require.Nil(l.T(), recover())
|
||||||
|
}()
|
||||||
|
|
||||||
|
localizer := l.localizer.Clone()
|
||||||
|
localizer.SetLanguage(language.Russian)
|
||||||
|
|
||||||
|
assert.NotEqual(l.T(), l.localizer.LanguageTag, localizer.LanguageTag)
|
||||||
|
assert.Equal(l.T(), "Test message", l.localizer.GetLocalizedMessage("message"))
|
||||||
|
assert.Equal(l.T(), "Тестовое сообщение", localizer.GetLocalizedMessage("message"))
|
||||||
|
}
|
||||||
|
|
||||||
func (l *LocalizerTest) Test_GetLocalizedTemplateMessage() {
|
func (l *LocalizerTest) Test_GetLocalizedTemplateMessage() {
|
||||||
defer func() {
|
defer func() {
|
||||||
require.Nil(l.T(), recover())
|
require.Nil(l.T(), recover())
|
||||||
|
@ -6,6 +6,7 @@ import (
|
|||||||
"math/rand"
|
"math/rand"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
|
"strconv"
|
||||||
"sync"
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
@ -61,6 +62,7 @@ func (r ravenPacket) getRequest() (*raven.Http, bool) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type ravenClientMock struct {
|
type ravenClientMock struct {
|
||||||
|
raven.Client
|
||||||
captured []ravenPacket
|
captured []ravenPacket
|
||||||
mu sync.RWMutex
|
mu sync.RWMutex
|
||||||
wg sync.WaitGroup
|
wg sync.WaitGroup
|
||||||
@ -92,7 +94,7 @@ func (r *ravenClientMock) CaptureMessageAndWait(message string, tags map[string]
|
|||||||
r.mu.Lock()
|
r.mu.Lock()
|
||||||
defer r.mu.Unlock()
|
defer r.mu.Unlock()
|
||||||
defer r.wg.Done()
|
defer r.wg.Done()
|
||||||
eventID := string(rand.Uint64())
|
eventID := strconv.FormatUint(rand.Uint64(), 10)
|
||||||
r.captured = append(r.captured, ravenPacket{
|
r.captured = append(r.captured, ravenPacket{
|
||||||
EventID: eventID,
|
EventID: eventID,
|
||||||
Message: message,
|
Message: message,
|
||||||
|
@ -4,7 +4,28 @@ import "github.com/getsentry/raven-go"
|
|||||||
|
|
||||||
// RavenClientInterface includes all necessary calls from *raven.Client. Therefore, it can be mocked or replaced.
|
// RavenClientInterface includes all necessary calls from *raven.Client. Therefore, it can be mocked or replaced.
|
||||||
type RavenClientInterface interface {
|
type RavenClientInterface interface {
|
||||||
|
SetIgnoreErrors(errs []string) error
|
||||||
|
SetDSN(dsn string) error
|
||||||
|
SetRelease(release string)
|
||||||
|
SetEnvironment(environment string)
|
||||||
|
SetDefaultLoggerName(name string)
|
||||||
|
SetSampleRate(rate float32) error
|
||||||
|
Capture(packet *raven.Packet, captureTags map[string]string) (eventID string, ch chan error)
|
||||||
|
CaptureMessage(message string, tags map[string]string, interfaces ...raven.Interface) string
|
||||||
CaptureMessageAndWait(message string, tags map[string]string, interfaces ...raven.Interface) string
|
CaptureMessageAndWait(message string, tags map[string]string, interfaces ...raven.Interface) string
|
||||||
|
CaptureError(err error, tags map[string]string, interfaces ...raven.Interface) string
|
||||||
CaptureErrorAndWait(err error, tags map[string]string, interfaces ...raven.Interface) string
|
CaptureErrorAndWait(err error, tags map[string]string, interfaces ...raven.Interface) string
|
||||||
|
CapturePanic(f func(), tags map[string]string, interfaces ...raven.Interface) (err interface{}, errorID string)
|
||||||
|
CapturePanicAndWait(f func(), tags map[string]string, interfaces ...raven.Interface) (err interface{}, errorID string)
|
||||||
|
Close()
|
||||||
|
Wait()
|
||||||
|
URL() string
|
||||||
|
ProjectID() string
|
||||||
|
Release() string
|
||||||
IncludePaths() []string
|
IncludePaths() []string
|
||||||
|
SetIncludePaths(p []string)
|
||||||
|
SetUserContext(u *raven.User)
|
||||||
|
SetHttpContext(h *raven.Http)
|
||||||
|
SetTagsContext(t map[string]string)
|
||||||
|
ClearContext()
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user