From 009fef15ca0a39674700503fd2b8034500bea066 Mon Sep 17 00:00:00 2001 From: helei Date: Wed, 16 Sep 2026 12:26:00 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20Bool=20=E6=96=B0=E5=A2=9E=20UnmarshalPa?= =?UTF-8?q?ram=20/=20UnmarshalText=20=E6=94=AF=E6=8C=81=20query/form=20?= =?UTF-8?q?=E5=8F=82=E6=95=B0=E7=BB=91=E5=AE=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dataType/bool.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/dataType/bool.go b/dataType/bool.go index 9ca32e5..b52ab70 100644 --- a/dataType/bool.go +++ b/dataType/bool.go @@ -5,6 +5,7 @@ import ( "database/sql/driver" "encoding/json" "strconv" + "strings" "gorm.io/gorm" "gorm.io/gorm/schema" @@ -162,3 +163,27 @@ func (b *Bool) GobDecode(data []byte) error { func (b Bool) ToPtr() *Bool { return &b } + +// UnmarshalParam 实现 gin binding.BindUnmarshaler(query/form 参数绑定)。 +// +// 目的:让 query/form 里的布尔原生解析("true"/"false"/"1"/"0"/"yes"/"no"…), +// 不再落到 gin 的 Struct 分支 json.Unmarshal(其遇空串会报 unexpected end of JSON input)。 +// +// 注意:gin 对**显式空值**(?x=)仍判定为"已设置"(trySetCustom 恒返回 isSet=true), +// 因此 *Bool(指针)字段会被赋成零值 false;若某筛选参数要求"空 = 未传 = nil", +// 请由前端不发送空串(现状如此),或该 DTO 改用值类型 dataType.Bool。 +func (b *Bool) UnmarshalParam(src string) error { + s := strings.TrimSpace(src) + if s == "" { + return nil + } + ok, err := tools.Any2bool(s) + if err != nil { + return err + } + b.bool = ok + return nil +} + +// UnmarshalText 兼容 encoding.TextUnmarshaler(部分三方解码器/框架偏好它) +func (b *Bool) UnmarshalText(text []byte) error { return b.UnmarshalParam(string(text)) }