Documentation
¶
Index ¶
- Variables
- func BindAllPaths(req interface{}, r *http.Request, pathVars []string) error
- func BindBody(r *http.Request, req interface{}) error
- func BindBodyField(r *http.Request, req interface{}, fieldPath string) error
- func BindPath(req interface{}, fieldPath string, value string) error
- func BindQuery(req interface{}, query url.Values) error
- func GetCodec() encoding.Codec
- func MapError(err error) int
- func PathParam(r *http.Request, name string) string
- func RegisterError(err error, code int)
- func SetCodec(c encoding.Codec)
- func SetContentType(ct string)
- func SetPathParamFunc(f func(*http.Request, string) string)
- func WriteError(w http.ResponseWriter, err error)
- func WriteResponse(w http.ResponseWriter, r *http.Request, v interface{})
- func WriteStreamChunk(w http.ResponseWriter, contentType string, data []byte) error
- type Router
Examples ¶
Constants ¶
This section is empty.
Variables ¶
Functions ¶
func BindQuery ¶
BindQuery 将 URL query 参数绑定到结构体 req。
采用反射直填(与 gin/echo 的 form binding 一致,与 BindPath 同源):
- 单值 → 按字段类型转换(string/int/uint/float/bool)
- 多值 → []string 或 []T(T 为标量类型)
- 单值给 slice 字段 → 自动包成单元素 slice
- 字段匹配:json tag → camelCase 回退 → 大小写不敏感(与 BindPath 统一)
相比 JSON 中转方案,本实现同时解决了:性能(无序列化)、功能(支持 int/bool 等标量)、 一致性(与 BindPath 共用 findField/setScalar)三个问题。
Example ¶
ExampleBindQuery shows a query-binding roundtrip that runs entirely in memory: a request carrying a fixed query string is built with httptest, its query values are bound into a request struct by BindQuery, and the populated typed fields are read afterwards.
In an application the request arrives from the framework's HTTP entry point rather than a test constructor: the caller extracts the query values from the incoming request and passes them to BindQuery together with a freshly allocated request struct, so the application handler only ever sees the populated typed fields. Request bodies and path parameters are bound through the sibling entry points BindBody and BindAllPaths.
package main
import (
"fmt"
"net/http"
"net/http/httptest"
"github.com/tx7do/go-wind-plugins/transport/http/binding"
)
func main() {
httpReq := httptest.NewRequest(http.MethodGet, "/items?name=alice&id=42", nil)
var req struct {
Name string `json:"name"`
ID int `json:"id"`
}
if err := binding.BindQuery(&req, httpReq.URL.Query()); err != nil {
return
}
fmt.Println(req.Name, req.ID)
}
Output: alice 42
func RegisterError ¶
func SetContentType ¶
func SetContentType(ct string)
func WriteError ¶
func WriteError(w http.ResponseWriter, err error)
WriteError 将错误写入 HTTP 响应。
结构化错误(windErrors.WindError,含被 %w 包装进错误链的情况)按框架 错误模型写出:HTTP 状态码取自 windErrors.CodeToHTTP 对错误 Code 的 权威映射(gRPC code → HTTP 状态,与 Envoy / Google API transcoding 同表), 响应体为错误自身的 JSON 形状(reason / details 字段,字段标注见 go-wind/errors)。消费端按 reason 驱动本地化文案、按状态码驱动路由分支, 二者均依赖本函数的映射。
非结构化错误不在上述模型内:命中哨兵表(ErrBadRequest 等)按表映射状态, 其余统一回退 500;响应体为通用错误文本形式(与既有行为一致,未做脱敏)。
func WriteResponse ¶
func WriteResponse(w http.ResponseWriter, r *http.Request, v interface{})
func WriteStreamChunk ¶
func WriteStreamChunk(w http.ResponseWriter, contentType string, data []byte) error
WriteStreamChunk 写出服务端流式响应(chunked transfer)的一块并立即 flush 到客户端。contentType 非空且响应尚未设置 Content-Type 时先设置 (HttpBody 首帧携带类型信息的约定)。仅用于 server-streaming 路由的 处理器;任何一帧写出后调用方应停止向响应写错误,只能中止流。
Types ¶
type Router ¶
type Router interface {
Handle(method, path string, handler http.HandlerFunc)
}
Router is implemented by httpserver.Server.