proposal: use Go 1.27 generic methods for typed Context helpers (PathParam/QueryParam/FormParam/Bind)

Aperta
#3,066 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
35/100
Tipo di issue
Funzionalità
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
go

Direzione di ricerca

Esamina innanzitutto l’insieme esistente di helper tipizzati di v4 e la v5 Context API. Conferma la versione minima di Go necessaria per i metodi generici e determina come dovrebbero coesistere i metodi Context proposti e gli helper esistenti a livello di package. Il lavoro è completo quando l’insieme di helper, il comportamento di compatibilità e il requisito della versione di Go sono concordati e coperti dai test del progetto.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Go 1.27 has been released with support for generic methods (release notes, proposal golang/go#77273). This removes the language limitation mentioned in #2856 — "structs can not have generic methods, only generic functions are allowed" — that forced the typed helpers in v4 to be package-level functions like echo.PathParam[int](c, "id").

Now that v5's Context is a struct and the language supports it, the helpers can become real methods:

Before (v4 style):

id, err := echo.PathParam[int](c, "id")
page, err := echo.QueryParam[int](c, "page")

After (generic methods):

id, err := c.PathParam[int]("id")
page, err := c.QueryParam[int]("page")

Suggested additions (mirroring the v4 helper set):

func (c *Context) PathParam[T any](name string) (T, error)
func (c *Context) QueryParam[T any](name string) (T, error)
func (c *Context) FormParam[T any](name string) (T, error)
func (c *Context) PathParamOr[T any](name string, def T) (T, error)
func (c *Context) QueryParamOr[T any](name string, def T) (T, error)
func (c *Context) FormParamOr[T any](name string, def T) (T, error)
func (c *Context) QueryParams[T any](name string) ([]T, error)
func (c *Context) FormParams[T any](name string) ([]T, error)

Note: since Context is no longer an interface in v5, adding methods is not a breaking change for users — existing code calling c.PathParam("id") keeps compiling, and the existing package-level generic functions can be kept (or deprecated) as thin wrappers.

The only blocker is the minimum Go version: generic methods require go 1.27 in go.mod. Filing this now so it's on the radar for when Echo bumps its minimum supported Go version to 1.27.

Lingua principale
Go
Stelle
32.7k
Fork
2.8k
Merge medio
9h 39m
PR unite (30g)
6

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di labstack/echo

Tutte le issue di labstack/echo

Issue simili

Altre issue su Go

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.