refactor: Separate CV domain and UI presentation models into distinct packages
**Main Changes:** 1. **Package Restructuring** - Separated mixed concerns into focused packages: - Created `internal/models/cv/` for CV domain logic (CV, Personal, Experience, etc.) - Created `internal/models/ui/` for UI presentation logic (InfoModal, ShortcutsModal, etc.) - Removed monolithic `internal/models/cv.go` (300+ lines → organized packages) 2. **Testing** - Added comprehensive unit tests: - `internal/models/cv/loader_test.go` - CV data loading and validation - `internal/models/ui/loader_test.go` - UI translations loading - All tests passing ✅ 3. **Documentation** - Added Go learning knowledge base: - `_go-learning/architecture/server-design.md` - Goroutines, graceful shutdown explained - `_go-learning/refactorings/001-cv-model-separation.md` - This refactoring documented - Public documentation showcasing Go expertise (README.md kept private) 4. **Handler Updates** - Updated imports to use new package structure: - `internal/handlers/cv.go` - Uses `cvmodel` and `uimodel` aliases **Benefits:** - ✅ Clear separation of concerns (domain vs presentation) - ✅ Better testability (isolated package testing) - ✅ Improved maintainability (smaller, focused files) - ✅ Scalability (each domain can evolve independently) - ✅ Follows Go best practices (small, cohesive packages) **Other Updates:** - Updated middleware security checks - Template improvements - Organized completed prompts **Testing:** - All Go unit tests pass (cv, ui, handlers) - Server verified with curl tests (English, Spanish, Health endpoints) - Frontend functionality unchanged (refactoring is transparent to UI)
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
package ui
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
// LoadUI loads UI translations from a JSON file for the specified language
|
||||
func LoadUI(lang string) (*UI, error) {
|
||||
if lang != "en" && lang != "es" {
|
||||
return nil, fmt.Errorf("unsupported language: %s", lang)
|
||||
}
|
||||
|
||||
filename := fmt.Sprintf("data/ui-%s.json", lang)
|
||||
filepath, err := findDataFile(filename)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
data, err := os.ReadFile(filepath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error reading file %s: %w", filename, err)
|
||||
}
|
||||
|
||||
var uiData UI
|
||||
if err := json.Unmarshal(data, &uiData); err != nil {
|
||||
return nil, fmt.Errorf("error parsing JSON: %w", err)
|
||||
}
|
||||
|
||||
return &uiData, nil
|
||||
}
|
||||
|
||||
// findDataFile locates a data file by searching up the directory tree
|
||||
func findDataFile(filename string) (string, error) {
|
||||
// Try current directory first
|
||||
if _, err := os.Stat(filename); err == nil {
|
||||
return filename, nil
|
||||
}
|
||||
|
||||
// Try parent directories (for tests running from subdirectories)
|
||||
paths := []string{
|
||||
filename, // Current dir
|
||||
"../" + filename, // One level up
|
||||
"../../" + filename, // Two levels up (for tests in internal/handlers)
|
||||
"../../../" + filename, // Three levels up
|
||||
}
|
||||
|
||||
for _, path := range paths {
|
||||
if _, err := os.Stat(path); err == nil {
|
||||
return path, nil
|
||||
}
|
||||
}
|
||||
|
||||
return "", fmt.Errorf("file not found: %s (searched: current dir, ../, ../../, ../../../)", filename)
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
package ui_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/juanatsap/cv-site/internal/models/ui"
|
||||
)
|
||||
|
||||
func TestLoadUI(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
lang string
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "English UI",
|
||||
lang: "en",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "Spanish UI",
|
||||
lang: "es",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "Invalid language",
|
||||
lang: "fr",
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "Empty language",
|
||||
lang: "",
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
uiData, err := ui.LoadUI(tt.lang)
|
||||
|
||||
if (err != nil) != tt.wantErr {
|
||||
t.Errorf("LoadUI() error = %v, wantErr %v", err, tt.wantErr)
|
||||
return
|
||||
}
|
||||
|
||||
if !tt.wantErr {
|
||||
if uiData == nil {
|
||||
t.Error("LoadUI() returned nil UI data")
|
||||
return
|
||||
}
|
||||
|
||||
// Validate UI has essential data
|
||||
if uiData.InfoModal.Title == "" {
|
||||
t.Error("LoadUI() returned UI with empty InfoModal title")
|
||||
}
|
||||
|
||||
if uiData.ShortcutsModal.Title == "" {
|
||||
t.Error("LoadUI() returned UI with empty ShortcutsModal title")
|
||||
}
|
||||
|
||||
// Validate TechStack is populated
|
||||
if uiData.InfoModal.TechStack.GoHono == "" {
|
||||
t.Error("LoadUI() returned UI with empty TechStack.GoHono")
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadUI_ModalData(t *testing.T) {
|
||||
uiData, err := ui.LoadUI("en")
|
||||
if err != nil {
|
||||
t.Fatalf("LoadUI() failed: %v", err)
|
||||
}
|
||||
|
||||
// Test InfoModal structure
|
||||
if uiData.InfoModal.Description == "" {
|
||||
t.Error("InfoModal.Description should not be empty")
|
||||
}
|
||||
|
||||
if uiData.InfoModal.ViewSource == "" {
|
||||
t.Error("InfoModal.ViewSource should not be empty")
|
||||
}
|
||||
|
||||
// Test ShortcutsModal structure
|
||||
if uiData.ShortcutsModal.Description == "" {
|
||||
t.Error("ShortcutsModal.Description should not be empty")
|
||||
}
|
||||
|
||||
// Test that shortcuts sections exist
|
||||
if uiData.ShortcutsModal.Sections.Zoom.Title == "" {
|
||||
t.Error("Zoom shortcuts section should have a title")
|
||||
}
|
||||
|
||||
if uiData.ShortcutsModal.Sections.Actions.Title == "" {
|
||||
t.Error("Actions shortcuts section should have a title")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package ui
|
||||
|
||||
import "html/template"
|
||||
|
||||
// UI represents user interface translations and configuration
|
||||
type UI struct {
|
||||
InfoModal InfoModal `json:"infoModal"`
|
||||
ShortcutsModal ShortcutsModal `json:"shortcutsModal"`
|
||||
}
|
||||
|
||||
type InfoModal struct {
|
||||
Title string `json:"title"`
|
||||
Description template.HTML `json:"description"`
|
||||
TechStack TechStack `json:"techStack"`
|
||||
ViewSource string `json:"viewSource"`
|
||||
ViewSourceSubtext string `json:"viewSourceSubtext"`
|
||||
}
|
||||
|
||||
type TechStack struct {
|
||||
GoHono string `json:"goHono"`
|
||||
HTMX string `json:"htmx"`
|
||||
HTML5 string `json:"html5"`
|
||||
CSS3 string `json:"css3"`
|
||||
}
|
||||
|
||||
type ShortcutsModal struct {
|
||||
Title string `json:"title"`
|
||||
Description string `json:"description"`
|
||||
Sections ShortcutsSections `json:"sections"`
|
||||
}
|
||||
|
||||
type ShortcutsSections struct {
|
||||
Zoom ShortcutGroup `json:"zoom"`
|
||||
ViewControls ShortcutGroup `json:"viewControls"`
|
||||
Navigation ShortcutGroup `json:"navigation"`
|
||||
Actions ShortcutGroup `json:"actions"`
|
||||
Browser ShortcutGroup `json:"browser"`
|
||||
}
|
||||
|
||||
type ShortcutGroup struct {
|
||||
Title string `json:"title"`
|
||||
ZoomIn *ShortcutItem `json:"zoomIn,omitempty"`
|
||||
ZoomOut *ShortcutItem `json:"zoomOut,omitempty"`
|
||||
ZoomReset *ShortcutItem `json:"zoomReset,omitempty"`
|
||||
ToggleLength *ShortcutItem `json:"toggleLength,omitempty"`
|
||||
ToggleIcons *ShortcutItem `json:"toggleIcons,omitempty"`
|
||||
ToggleTheme *ShortcutItem `json:"toggleTheme,omitempty"`
|
||||
ExpandAll *ShortcutItem `json:"expandAll,omitempty"`
|
||||
CollapseAll *ShortcutItem `json:"collapseAll,omitempty"`
|
||||
ScrollToTop *ShortcutItem `json:"scrollToTop,omitempty"`
|
||||
Print *ShortcutItem `json:"print,omitempty"`
|
||||
CloseModal *ShortcutItem `json:"closeModal,omitempty"`
|
||||
ShowHelp *ShortcutItem `json:"showHelp,omitempty"`
|
||||
Tab *ShortcutItem `json:"tab,omitempty"`
|
||||
Enter *ShortcutItem `json:"enter,omitempty"`
|
||||
}
|
||||
|
||||
type ShortcutItem struct {
|
||||
Key string `json:"key"`
|
||||
Description string `json:"description"`
|
||||
}
|
||||
Reference in New Issue
Block a user