refactor(i18n): centralise default state snapshot
Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
6e202e8230
commit
5dc9847eec
4 changed files with 27 additions and 29 deletions
|
|
@ -337,7 +337,7 @@ func (s *CoreService) CurrentDebug() bool {
|
|||
// State returns a copy-safe snapshot of the wrapped service configuration.
|
||||
func (s *CoreService) State() ServiceState {
|
||||
if s == nil || s.svc == nil {
|
||||
return State()
|
||||
return defaultServiceStateSnapshot()
|
||||
}
|
||||
return s.svc.State()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,6 +10,11 @@ import (
|
|||
|
||||
func TestCoreServiceNilSafe(t *testing.T) {
|
||||
var svc *CoreService
|
||||
savedDefault := defaultService.Load()
|
||||
t.Cleanup(func() {
|
||||
defaultService.Store(savedDefault)
|
||||
})
|
||||
defaultService.Store(nil)
|
||||
|
||||
assert.NotPanics(t, func() {
|
||||
assert.Equal(t, ModeNormal, svc.Mode())
|
||||
|
|
@ -28,9 +33,10 @@ func TestCoreServiceNilSafe(t *testing.T) {
|
|||
assert.Equal(t, "hello", svc.T("hello"))
|
||||
assert.Equal(t, "hello", svc.Raw("hello"))
|
||||
assert.Equal(t, core.Result{Value: "hello", OK: false}, svc.Translate("hello"))
|
||||
assert.Equal(t, State(), svc.State())
|
||||
assert.Equal(t, State(), svc.CurrentState())
|
||||
assert.Equal(t, defaultServiceStateSnapshot(), svc.State())
|
||||
assert.Equal(t, defaultServiceStateSnapshot(), svc.CurrentState())
|
||||
})
|
||||
assert.Nil(t, defaultService.Load())
|
||||
|
||||
assert.NoError(t, svc.OnStartup(nil))
|
||||
svc.SetMode(ModeCollect)
|
||||
|
|
|
|||
14
i18n.go
14
i18n.go
|
|
@ -180,19 +180,7 @@ func CurrentDebug() bool {
|
|||
|
||||
// State returns a copy-safe snapshot of the default service configuration.
|
||||
func State() ServiceState {
|
||||
return defaultServiceValue(ServiceState{
|
||||
Language: "en",
|
||||
RequestedLanguage: "",
|
||||
LanguageExplicit: false,
|
||||
AvailableLanguages: []string{},
|
||||
Mode: ModeNormal,
|
||||
Fallback: "en",
|
||||
Formality: FormalityNeutral,
|
||||
Direction: DirLTR,
|
||||
IsRTL: false,
|
||||
Debug: false,
|
||||
Handlers: []KeyHandler{},
|
||||
}, func(svc *Service) ServiceState {
|
||||
return defaultServiceValue(defaultServiceStateSnapshot(), func(svc *Service) ServiceState {
|
||||
return svc.State()
|
||||
})
|
||||
}
|
||||
|
|
|
|||
30
state.go
30
state.go
|
|
@ -4,6 +4,22 @@ import (
|
|||
"dappco.re/go/core"
|
||||
)
|
||||
|
||||
func defaultServiceStateSnapshot() ServiceState {
|
||||
return ServiceState{
|
||||
Language: "en",
|
||||
RequestedLanguage: "",
|
||||
LanguageExplicit: false,
|
||||
AvailableLanguages: []string{},
|
||||
Mode: ModeNormal,
|
||||
Fallback: "en",
|
||||
Formality: FormalityNeutral,
|
||||
Direction: DirLTR,
|
||||
IsRTL: false,
|
||||
Debug: false,
|
||||
Handlers: []KeyHandler{},
|
||||
}
|
||||
}
|
||||
|
||||
// ServiceState captures the current configuration of a service in one
|
||||
// copy-safe snapshot.
|
||||
type ServiceState struct {
|
||||
|
|
@ -59,19 +75,7 @@ func (s ServiceState) String() string {
|
|||
|
||||
func (s *Service) State() ServiceState {
|
||||
if s == nil {
|
||||
return ServiceState{
|
||||
Language: "en",
|
||||
RequestedLanguage: "",
|
||||
LanguageExplicit: false,
|
||||
AvailableLanguages: []string{},
|
||||
Mode: ModeNormal,
|
||||
Fallback: "en",
|
||||
Formality: FormalityNeutral,
|
||||
Direction: DirLTR,
|
||||
IsRTL: false,
|
||||
Debug: false,
|
||||
Handlers: []KeyHandler{},
|
||||
}
|
||||
return defaultServiceStateSnapshot()
|
||||
}
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue