// Package result 定义 HTTP 响应统一 envelope。 // // HIF-4 F9:所有响应必须是三字段固定 shape {code, msg, data},data 为空时 // 也要显式 `null` —— 而不是靠 omitempty 丢字段。App 端强类型 decoder(Retrofit / // serde / typed structs)依赖这个 shape,缺字段会解码失败。 // // 修改前: // - Success 的 Data 带 omitempty,nil payload 时 JSON 里没有 data 键 // - Error 结构体没有 Data 字段,错误响应永远缺 data 键 // 修改后:Success 和 Error 都有 Data 字段,且都 **不带 omitempty**。空场景下 JSON // 一律出现 "data":null。加字段对旧 client 无 breaking impact(JSON 忽略未知字段/ // 已知字段变 null 都能 decode 过)。 package result type ResponseSuccessBean struct { Code uint32 `json:"code"` Msg string `json:"msg"` Data interface{} `json:"data"` // F9: 不带 omitempty,空 payload 也返 data:null } type NullJson struct{} func Success(data interface{}) *ResponseSuccessBean { return &ResponseSuccessBean{200, "success", data} } // ResponseErrorBean 与 ResponseSuccessBean 结构对齐(都有 Data 字段), // Data 在错误场景永远为 nil;序列化后 JSON 里显式为 "data":null。 type ResponseErrorBean struct { Code uint32 `json:"code"` Msg string `json:"msg"` Data interface{} `json:"data"` // F9: 错误响应也必须有 data 字段(值永远为 null) } func Error(errCode uint32, errMsg string) *ResponseErrorBean { return &ResponseErrorBean{errCode, errMsg, nil} }