feat(lvt): migrate templates to method dispatch pattern#6
Conversation
Remove Store interface with Change() method in favor of automatic method dispatch. Actions now route to methods by name (e.g., action "increment" -> Increment() method). Changes: - Generator templates: Convert Change() switch to individual methods - Kit templates (simple, single, multi): Same pattern - Auth handler: Register, Login, MagicLink, ForgotPassword, SwitchView - E2E tests: LoadingTestState, FocusTestState migrated - Golden files and README updated BREAKING CHANGE: Generated code no longer uses Change() method. Existing apps must migrate to method dispatch pattern. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
Code Review: Migration to Method Dispatch PatternI've completed a comprehensive review of PR #6. Overall, this is an excellent refactoring that modernizes the codebase and aligns with livetemplate core v0.6.0. Here's my detailed analysis: ✅ Strengths1. Clean Architecture Migration
2. Excellent Code Organization
3. Strong Test Coverage
4. Documentation Quality
🔍 Code Quality ObservationsPositive Patterns:
Example of Clean Implementation:// Add handles the "add" action to create a new resource
func (s *UserState) Add(ctx *livetemplate.ActionContext) error {
var input AddInput
if err := ctx.BindAndValidate(&input, validate); err != nil {
return err
}
// ... clean implementation
s.LastUpdated = formatTime()
return nil
}🎯 Potential Improvements (Minor)1. Context Parameter UsageSome methods accept func (s *State) CancelEdit(_ *livetemplate.ActionContext) error
func (s *State) Back(_ *livetemplate.ActionContext) error
func (s *State) NextPage(_ *livetemplate.ActionContext) error
func (s *State) PrevPage(_ *livetemplate.ActionContext) errorSuggestion: This is acceptable but consider if these could be simplified to not require the context parameter at all in the livetemplate core. (This may be a core library design decision, so not a blocker.) 2. Duplicate Database Context CreationEvery method creates // Helper method
func (s *State) dbContext() context.Context {
return context.Background()
}This would make it easier to add context timeouts or tracing later. However, this is very minor and current approach is fine. 3. Auth Handler - Action Name ConsistencyIn auth handler, action names use underscores ( 🔒 Security Analysis✅ Secure Practices:
|
There was a problem hiding this comment.
Pull request overview
This PR migrates all LiveTemplate generator templates from the Store.Change() switch-case pattern to the new method dispatch pattern introduced in livetemplate core v0.6.0. The migration removes the Store interface requirement and enables automatic action routing based on method names, making the code more idiomatic Go with better IDE support.
Key Changes
- Handler pattern migration: All 13 resource actions (Add, Edit, Update, CancelEdit, View, Back, Delete, Search, Sort, NextPage, PrevPage, GotoPage, LoadMore) converted from switch cases to individual methods
- API updates: Adapted to v0.6.0 where
livetemplate.New()no longer returns an error - Simplified initialization: Removed defensive
Init()logic since EditingID/EditingResource are now set after initialization in page mode - Documentation: Updated README examples and inline comments to demonstrate the new method dispatch pattern
Reviewed changes
Copilot reviewed 15 out of 15 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
testdata/golden/view_handler.go.golden |
Golden file updated: removed empty Change(), added example comment for action methods |
testdata/golden/resource_handler.go.golden |
Golden file updated: 13 resource actions converted to methods, livetemplate.New() error handling removed |
internal/generator/templates/view/handler.go.tmpl |
Generator template updated: helpful example comment for adding action methods |
internal/generator/templates/resource/handler.go.tmpl |
Generator template updated: all CRUD and pagination actions converted to methods |
internal/generator/templates/app/home.go.tmpl |
Home page template updated: removed empty Change() method |
internal/kits/system/simple/templates/app/main.go.tmpl |
Simple kit updated: Increment/Decrement/Reset as individual methods |
internal/kits/system/single/templates/* |
Single-page kit templates mirror generator changes |
internal/kits/system/multi/templates/* |
Multi-page kit templates mirror generator changes, including auth handler (Register, Login, MagicLink, ForgotPassword, SwitchView methods) |
e2e/livetemplate_core_test.go |
E2E tests updated: removed empty Change() from LoadingTestState, FocusTestState uses Increment() method |
README.md |
Documentation updated: shows method dispatch pattern in example code |
Summary
Store.Change()switch-case pattern to automatic method dispatchThis aligns with the livetemplate core v0.6.0 changes that removed the
Storeinterface in favor of method dispatch (PR #67).Changes
Generator Templates
resource/handler.go.tmpl: 13 actions converted to individual methods (Add, Edit, Update, CancelEdit, View, Back, Delete, Search, Sort, NextPage, PrevPage, GotoPage, LoadMore)view/handler.go.tmpl: RemovedChange(), added comment with example methodapp/home.go.tmpl: RemovedChange()(static page, no actions)Kit Templates
main.go.tmpl- Increment, Decrement, Reset methodsAuth Handler
Register(),Login(),MagicLink(),ForgotPassword(),SwitchView()methodsE2E Tests
LoadingTestState: Removed emptyChange()methodFocusTestState: Converted toIncrement()methodTest plan
go test -v ./... -short- all unit tests passBreaking Change
Generated code will use method dispatch instead of
Change()switch statements. Existing apps need to migrate manually.🤖 Generated with Claude Code