Skip to content

feat: update counter and todos to use automatic method dispatch#11

Merged
adnaan merged 3 commits intomainfrom
feat/automatic-method-dispatch
Dec 3, 2025
Merged

feat: update counter and todos to use automatic method dispatch#11
adnaan merged 3 commits intomainfrom
feat/automatic-method-dispatch

Conversation

@adnaan
Copy link
Copy Markdown
Contributor

@adnaan adnaan commented Dec 3, 2025

Summary

Updates counter and todos examples to use the new Controller pattern with automatic method dispatch instead of implementing the Store interface with a Change() switch statement.

Depends on: livetemplate/livetemplate#66

Changes

counter/main.go

  • Removed Change() method with switch statement
  • Added individual action methods: Increment(), Decrement(), Reset()

todos/main.go

  • Removed Change() method with switch statement
  • Added individual action methods: Add(), Toggle(), Delete(), Search(), Sort(), NextPage(), PrevPage(), GotoPage(), ClearCompleted()

Before vs After

// BEFORE: Switch statement
func (s *Counter) Change(ctx *ActionContext) error {
    switch ctx.Action {
    case "increment": s.Count++
    case "decrement": s.Count--
    case "reset": s.Count = 0
    }
    return nil
}

// AFTER: Direct method dispatch
func (s *Counter) Increment(ctx *ActionContext) error {
    s.Count++
    return nil
}

func (s *Counter) Decrement(ctx *ActionContext) error {
    s.Count--
    return nil
}

func (s *Counter) Reset(ctx *ActionContext) error {
    s.Count = 0
    return nil
}

Test plan

  • counter example builds and tests pass
  • todos example builds and tests pass

🤖 Generated with Claude Code

Updates examples to use the new Controller pattern with automatic method dispatch
instead of implementing the Store interface with a Change() switch statement.

## Changes

### counter/main.go
- Removed `Change()` method with switch statement
- Added individual action methods: `Increment()`, `Decrement()`, `Reset()`
- Added documentation explaining the Controller pattern

### todos/main.go
- Removed `Change()` method with switch statement
- Added individual action methods:
  - `Add()` - creates a new todo
  - `Toggle()` - toggles completion status
  - `Delete()` - removes a todo
  - `Search()` - filters todos
  - `Sort()` - changes sort order
  - `NextPage()` / `PrevPage()` / `GotoPage()` - pagination
  - `ClearCompleted()` - removes completed todos
- Added documentation explaining the pattern

## Benefits
- Cleaner code with single-responsibility methods
- Better IDE support (method completion, go-to-definition)
- Actions support snake_case, camelCase, and case-insensitive matching
- No boilerplate switch statement needed

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings December 3, 2025 07:28
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors the counter and todos examples to use the new Controller pattern with automatic method dispatch, eliminating the need for manual Change() switch statements.

  • Replaces switch-based action routing with direct method dispatch
  • Adds comprehensive documentation explaining the Controller pattern
  • Cleans up unused dependencies from go.mod/go.sum

Reviewed changes

Copilot reviewed 4 out of 6 changed files in this pull request and generated no comments.

Show a summary per file
File Description
counter/main.go Refactored from single Change() switch to individual Increment(), Decrement(), and Reset() methods with automatic dispatch
todos/main.go Refactored from single Change() switch to nine individual action methods (Add(), Toggle(), Delete(), Search(), Sort(), NextPage(), PrevPage(), GotoPage(), ClearCompleted())
counter/go.mod Removed unused dependencies (charmbracelet, muesli, modelcontextprotocol, and other terminal UI libraries)
counter/go.sum Updated checksums to reflect dependency removals
todos/go.mod Removed same set of unused dependencies as counter
todos/go.sum Updated checksums to reflect dependency removals

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

adnaan and others added 2 commits December 3, 2025 19:29
The Handle() signature changed from Handle(stores ...Store) to
Handle(stores ...interface{}) in v0.5.1 to support the new automatic
method dispatch pattern.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
v0.5.2 includes the Handle(stores ...interface{}) API change that
supports automatic method dispatch without requiring the Store interface.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
@adnaan adnaan merged commit 75fe618 into main Dec 3, 2025
17 of 18 checks passed
@adnaan adnaan deleted the feat/automatic-method-dispatch branch December 3, 2025 19:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants