From 0cea8723631cab5204c6855dfa8e04a7bcfbba0b Mon Sep 17 00:00:00 2001 From: Virgil Date: Thu, 2 Apr 2026 14:33:27 +0000 Subject: [PATCH] refactor(i18n): add service option stringers Co-Authored-By: Virgil --- core_service.go | 30 ++++++++++++++++++++++++++++++ core_service_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/core_service.go b/core_service.go index 78a4b26..0620f0f 100644 --- a/core_service.go +++ b/core_service.go @@ -53,6 +53,36 @@ type FSSource struct { Dir string } +// String returns a compact summary of the filesystem source. +func (s FSSource) String() string { + if s.Dir == "" { + return core.Sprintf("FSSource{fs=%T}", s.FS) + } + return core.Sprintf("FSSource{fs=%T dir=%q}", s.FS, s.Dir) +} + +// String returns a compact summary of the service options. +func (o ServiceOptions) String() string { + extraFS := "[]" + if len(o.ExtraFS) > 0 { + parts := make([]string, len(o.ExtraFS)) + for i, src := range o.ExtraFS { + parts[i] = src.String() + } + extraFS = "[" + core.Join(", ", parts...) + "]" + } + return core.Sprintf( + "ServiceOptions{language=%q fallback=%q formality=%s location=%q mode=%s debug=%t extraFS=%s}", + o.Language, + o.Fallback, + o.Formality, + o.Location, + o.Mode, + o.Debug, + extraFS, + ) +} + // NewCoreService creates an i18n Core service factory. // Automatically loads locale filesystems from: // 1. Embedded go-i18n base translations (grammar, verbs, nouns) diff --git a/core_service_test.go b/core_service_test.go index d929578..f54c49f 100644 --- a/core_service_test.go +++ b/core_service_test.go @@ -2,6 +2,7 @@ package i18n import ( "testing" + "testing/fstest" "dappco.re/go/core" "github.com/stretchr/testify/assert" @@ -75,3 +76,29 @@ func TestCoreServiceMissingKeysReturnsCopies(t *testing.T) { require.Len(t, again, 1) assert.Equal(t, "bar", again[0].Args["foo"]) } + +func TestServiceOptionsAndFSSourceString(t *testing.T) { + opts := ServiceOptions{ + Language: "en-GB", + Fallback: "en", + Formality: FormalityFormal, + Location: "workspace", + Mode: ModeCollect, + Debug: true, + ExtraFS: []FSSource{ + {FS: fstest.MapFS{}, Dir: "locales"}, + }, + } + + got := opts.String() + assert.Contains(t, got, `language="en-GB"`) + assert.Contains(t, got, `fallback="en"`) + assert.Contains(t, got, `formality=formal`) + assert.Contains(t, got, `location="workspace"`) + assert.Contains(t, got, `mode=collect`) + assert.Contains(t, got, `debug=true`) + assert.Contains(t, got, `FSSource{fs=fstest.MapFS dir="locales"}`) + + src := FSSource{FS: fstest.MapFS{}, Dir: "translations"} + assert.Equal(t, `FSSource{fs=fstest.MapFS dir="translations"}`, src.String()) +}