Json Golang Maps:如何自动剔除空字段
给定以下Json Golang Maps:如何自动剔除空字段,json,dictionary,go,mgo,Json,Dictionary,Go,Mgo,给定以下struct package models import ( "time" "gopkg.in/mgo.v2/bson" "github.com/fatih/structs" ) type User struct { Id bson.ObjectId `json:"id,omitempty" bson:"_id,omitempty"` Name string `json:"name,omitempty"
struct
package models
import (
"time"
"gopkg.in/mgo.v2/bson"
"github.com/fatih/structs"
)
type User struct {
Id bson.ObjectId `json:"id,omitempty" bson:"_id,omitempty"`
Name string `json:"name,omitempty" bson:"name,omitempty"`
BirthDate time.Time `json:"birth_date,omitempty" bson:"birth_date,omitempty"`
}
。。。我通过解析HTTP请求来填充它,如下所示:
func (userController *UserController) UpdateUser() echo.HandlerFunc {
return func(context echo.Context) error {
user := &models.User{}
// parse JSON in POST request and fill the User struct
if err := context.Bind(user); err != nil {
return err
}
// get user id from URL
query := bson.M{"_id": bson.ObjectIdHex(context.Param("id"))}
update := bson.M{
// structs.Map() converts a User struct to a Map[string]interfacce{}
"$set": structs.Map(user)
}
if err := userController.session.DB("test").C("users").Update(query, update); err != {
return err
}
return context.JSON(http.StatusOK, user)
}
}
map[%!d(string=Name):%!d(string=Bon Scott) %!d(string=BirthDate):{63108633600 0 10736640} %!d(string=Id):%!d(bson.ObjectId=)]
update := bson.M{
"name": user.Name,
"birthDate": user.BirthDate,
}
问题是当我得到像这样的更新JSON时
{
"name": "Bon Scott",
"birth_date" : "1946-07-09"
}
structs.Map()
仍然返回一个Map
,其中包含一个空id,如下所示:
func (userController *UserController) UpdateUser() echo.HandlerFunc {
return func(context echo.Context) error {
user := &models.User{}
// parse JSON in POST request and fill the User struct
if err := context.Bind(user); err != nil {
return err
}
// get user id from URL
query := bson.M{"_id": bson.ObjectIdHex(context.Param("id"))}
update := bson.M{
// structs.Map() converts a User struct to a Map[string]interfacce{}
"$set": structs.Map(user)
}
if err := userController.session.DB("test").C("users").Update(query, update); err != {
return err
}
return context.JSON(http.StatusOK, user)
}
}
map[%!d(string=Name):%!d(string=Bon Scott) %!d(string=BirthDate):{63108633600 0 10736640} %!d(string=Id):%!d(bson.ObjectId=)]
update := bson.M{
"name": user.Name,
"birthDate": user.BirthDate,
}
我会避免像这样一个字段一个字段地手动创建Map
:
func (userController *UserController) UpdateUser() echo.HandlerFunc {
return func(context echo.Context) error {
user := &models.User{}
// parse JSON in POST request and fill the User struct
if err := context.Bind(user); err != nil {
return err
}
// get user id from URL
query := bson.M{"_id": bson.ObjectIdHex(context.Param("id"))}
update := bson.M{
// structs.Map() converts a User struct to a Map[string]interfacce{}
"$set": structs.Map(user)
}
if err := userController.session.DB("test").C("users").Update(query, update); err != {
return err
}
return context.JSON(http.StatusOK, user)
}
}
map[%!d(string=Name):%!d(string=Bon Scott) %!d(string=BirthDate):{63108633600 0 10736640} %!d(string=Id):%!d(bson.ObjectId=)]
update := bson.M{
"name": user.Name,
"birthDate": user.BirthDate,
}
有没有办法自动去除空字段?在您的示例中,您已经在
bson
中使用了,省略empty
标记
基于
如果字段未设置为零,则仅包括该字段
将类型或的值设置为空切片或映射
因此,即使将映射直接传递给更新调用,也不应更新空字段。唯一的问题应该是,您应该注意\u id
没有被赋予错误的值