Skip to main content

2 posts tagged with "binding"

View All Tags

Filters Nobody Planned For: Binding attr[color]=red Into Go Maps in Fiber v3.7

ยท 23 min read
Fiber Team
Maintainers

A shop sells jackets. The product list has filters for color and size, and the handler binds them into a struct with a Color field and a Size field. Then the merchandising team adds "material" to the product data, and a week later "hood" and "waterproof rating". Every new attribute is a new struct field, a new WHERE clause, a code review and a deploy, for something that is really just another row in the attributes table. The frontend team would rather send whatever the data has: ?attr[color]=red&attr[material]=down.

Plenty of APIs already speak this shape. PHP turns it into nested arrays and Rails into nested hashes, Stripe takes metadata[...] this way, JSON:API sets aside the filter parameter family for it, and Express parsed it into objects by default until version 5 switched its default query parser to a simpler one. In Fiber, a struct field of type map[string]string used to be skipped by the binder without a word. A request for exactly this has been open since 2023, and Fiber v3.7.0 closes it: map fields of a struct now bind from query strings and form bodies. This post builds a facet filter on top of it, backed by SQLite, and spends most of its time on what a map hands you: keys chosen by whoever sends the request. Everything was built and tested against Fiber v3.7.0 with modernc.org/sqlite v1.62.0. Map fields need Fiber v3.7.0 or later, which needs Go 1.26 like every release since v3.6.0.

Binding in Practice

ยท 6 min read
Fiber Team
Maintainers

Request parsing is one of the easiest places to create hidden technical debt.

At the beginning of a project, mixed parsing styles seem harmless. A quick c.Query() here, a manual json.Unmarshal(c.Body(), &req) there, a c.Params() somewhere else. After enough endpoints, input behavior becomes unpredictable. One handler parses the body with a JSON decoder, another uses form tags, a third reads query parameters with individual calls. When bugs appear, you have to trace parsing logic per endpoint instead of trusting a shared convention.

Fiber v3 binding exists to make that convention explicit. c.Bind() is not just a new method name. It is a structured API that supports every input source, has defined precedence rules, integrates validation, and supports custom decoders. If your team agrees on binding, input handling stops being a source of surprises.