Skip to content

Conversation

@aldas
Copy link
Contributor

@aldas aldas commented Dec 25, 2025

Add generic functions to get typed PathParam/QueryParam/FormParam values. Also *Or variants for default values.
Note: those who have forgotten - structs can not have generic methods. only generic functions are allowed.
Types that are supported:

  • bool
  • float32
  • float64
  • int
  • int8
  • int16
  • int32
  • int64
  • uint
  • uint8/byte
  • uint16
  • uint32
  • uint64
  • string
  • echo.BindUnmarshaler interface
  • encoding.TextUnmarshaler interface
  • json.Unmarshaler interface
  • time.Duration
  • time.Time use echo.TimeOpts or echo.TimeLayout to set time parsing configuration

Scalar values:

  • id, err := echo.PathParam[int](c, "id")
  • id, err := echo.QueryParam[int](c, "id")
  • id, err := echo.FormParam[int](c, "id")
  • id, err := echo.PathParamOr[int](c, "id", -1)
  • id, err := echo.QueryParamOr[int](c, "id", -1)
  • id, err := echo.FormParamOr[int](c, "id", -1)

For getting slices:

  • ids, err := echo.QueryParams[int](c, "id") returns []int
  • ids, err := echo.FormParams[int](c, "id")

Generic parse functions:

  • id, err := echo.ParseValue[int]("123")
  • id, err := echo.ParseValueOr[int]("123",-1)
  • ids, err := echo.ParseValues[int]([]string{"123", "124"})
  • ids, err := echo.ParseValuesOr[int]([]string{"123", "124"}, []int{666})

Example

	e.GET(`/user/:id`, func(c echo.Context) error {
		id, err := echo.PathParam[int](c, "id")
		if err == nil {
			return err
		}
		return c.String(http.StatusOK, fmt.Sprintf("Hello %v\n", id))
	})

For Context.Get() generic versions

  • v, err := ContextGetOr[int64](c, "key", 999)
  • v, err := ContextGet[int64](c, "key")

Example with JWT middleware:

	e.Use(echojwt.JWT([]byte("secret")))

	e.GET(`/user/:id`, func(c echo.Context) error {
		token, err := echo.ContextGet[*jwt.Token](c, "user")
		if err == nil {
			return echo.ErrUnauthorized.WithInternal(err)
		}
		return c.String(http.StatusOK, fmt.Sprintf("Hello %v\n", token.Claims))
	})

@codecov
Copy link

codecov bot commented Dec 25, 2025

Codecov Report

❌ Patch coverage is 90.10601% with 28 lines in your changes missing coverage. Please review.
✅ Project coverage is 93.50%. Comparing base (f24aaff) to head (dd2e023).
⚠️ Report is 35 commits behind head on master.

Files with missing lines Patch % Lines
binder_generic.go 89.55% 16 Missing and 12 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2856      +/-   ##
==========================================
+ Coverage   93.25%   93.50%   +0.25%     
==========================================
  Files          39       42       +3     
  Lines        4652     4237     -415     
==========================================
- Hits         4338     3962     -376     
+ Misses        218      157      -61     
- Partials       96      118      +22     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

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

@aldas
Copy link
Contributor Author

aldas commented Dec 26, 2025

@vishr please review if you have time.

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