Skip to content

feat: add struct binding example for URL parameters#44

Merged
miclle merged 4 commits intofox-gonic:mainfrom
miclle:feat/add-struct-binding-example
Dec 8, 2025
Merged

feat: add struct binding example for URL parameters#44
miclle merged 4 commits intofox-gonic:mainfrom
miclle:feat/add-struct-binding-example

Conversation

@miclle
Copy link
Copy Markdown
Member

@miclle miclle commented Dec 8, 2025

Summary

This PR enhances the basic example by adding a demonstration of struct binding for URL parameters, showing developers two different ways to extract path parameters in Fox.

Changes

New Endpoint Added

GET /greet/:name - Demonstrates struct binding approach

type UserParams struct {
    Name string `uri:"name" binding:"required"`
}
router.GET("/greet/:name", func(params UserParams) string {
    return "Greetings, " + params.Name + "!"
})

Updated Documentation

  • Updated README.md to include the new example
  • Added curl command to test the new endpoint
  • Added expected output example

Comparison of Two Approaches

1. Direct Parameter Access (existing)

router.GET("/hello/:name", func(ctx *fox.Context) string {
    name := ctx.Param("name")
    return "Hello, " + name + "!"
})

2. Struct Binding (new)

type UserParams struct {
    Name string `uri:"name" binding:"required"`
}
router.GET("/greet/:name", func(params UserParams) string {
    return "Greetings, " + params.Name + "!"
})

Benefits

  • Shows developers both methods for parameter extraction
  • Demonstrates Fox's automatic binding capabilities
  • Struct binding approach provides built-in validation
  • More idiomatic for complex parameter sets

Testing

# Start the example
cd examples/01-basic
go run main.go

# Test the new endpoint
curl http://localhost:8080/greet/alice
# Expected: Greetings, alice!

Files Changed

  • examples/01-basic/main.go - Added UserParams struct and new endpoint
  • examples/01-basic/README.md - Updated documentation

- Add .codecov.yml configuration file
  - Set coverage precision and range (70-100%)
  - Configure project and patch coverage thresholds
  - Enable coverage comments on PRs
  - Ignore test files and examples

- Update GitHub Actions workflow
  - Add codecov/codecov-action@v5 step
  - Upload coverage.txt after tests
  - Only upload from ubuntu-latest to avoid duplicates
  - Use CODECOV_TOKEN secret for authentication

This enables automatic coverage reporting on PRs and commits,
making it easier to track test coverage changes.
Added a new endpoint /greet/:name that demonstrates using struct binding
to extract URL parameters, providing an alternative to ctx.Param() method.

Changes:
- examples/01-basic/main.go: Add UserParams struct and /greet/:name endpoint
- examples/01-basic/README.md: Update documentation with new example

This shows both approaches:
1. ctx.Param() - direct parameter access
2. Struct binding - automatic parameter binding with validation
@codecov
Copy link
Copy Markdown

codecov Bot commented Dec 8, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 87.87%. Comparing base (433a040) to head (b0e27d3).
⚠️ Report is 3 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##             main      #44   +/-   ##
=======================================
  Coverage   87.87%   87.87%           
=======================================
  Files          16       16           
  Lines         726      726           
=======================================
  Hits          638      638           
  Misses         70       70           
  Partials       18       18           
Flag Coverage Δ
unittests 87.87% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@miclle miclle merged commit 1a48912 into fox-gonic:main Dec 8, 2025
10 checks passed
@miclle miclle deleted the feat/add-struct-binding-example branch December 8, 2025 06:07
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.

1 participant