golang mapstructure 转出为空的问题

//struct映射
func (s *commentRedisStore) CommentMapToStruct(commentInfo *model.CommentInfo, mapValue map[string]string) error {
	if len(mapValue) == 0 {
		return errors.New("len(mapValue) == 0")
	}

	decoder, _ := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
		WeaklyTypedInput: true,
		Result:           commentInfo,
	})
	_ = decoder.Decode(mapValue)

	//重填时间
	if mapValue["createdAt"] != "" {
		timeStr := mapValue["createdAt"]
		commentInfo.CreatedAt, _ = time.Parse("2006-01-02T15:04:05Z07:00", timeStr)
	}
	if mapValue["updatedAt"] != "" {
		timeStr := mapValue["updatedAt"]
		commentInfo.UpdatedAt, _ = time.Parse("2006-01-02T15:04:05Z07:00", timeStr)
	}
	if mapValue["deletedAt"] != "" {
		timeStr := mapValue["deletedAt"]
		deletedAt, _ := time.Parse("2006-01-02T15:04:05Z07:00", timeStr)
		commentInfo.DeletedAt = &deletedAt
	} else {
		commentInfo.DeletedAt = nil
	}

	return nil
}
type CommentInfo struct {
	SN int64  `json:"sn"`
	ID string `gorm:"-" json:"id"` // 评论外部编号

	ArticleSN int64  `json:"articleSN"`
	ArticleID string `gorm:"-" json:"articleID"` // 文章外部编号

	UserID int `json:"userID"` // 用户id

	Content string `json:"content"` // 评论内容

	ReplyTo int `json:"replyTo"` // 被评论人id

	ParentSN int64  `json:"parentSN"`
	Parent string `gorm:"-" json:"parent"` // 父评论sn(空则为一级评论,否则为二级评论)

	PlateSN int64  `json:"plateSN"`
	Plate string `gorm:"-" json:"plate"` // 文章所属版块

	CreatedAt time.Time  `json:"createdAt"` // 创建时间
	UpdatedAt time.Time  `json:"updatedAt"` // 更新时间
	DeletedAt *time.Time `json:"deletedAt"` // 删除时间

	VoteUp       int64 `json:"voteUp"`       // 赞同数
	VoteDown     int64 `json:"voteDown"`     // 反对数
	RepliedTotal int64 `json:"repliedTotal"` // 被回复数

	Status       int64 `json:"status"`       // 状态(正常显示1,用户主动假删除2)
	ReviewStatus int64 `json:"reviewStatus"` // 审核状态(未审核1,通过2,审核不通过假删除3)
}

Plate string `gorm:"-" json:"plate"` // 文章所属版块

字段需要一致(大小写无所谓),否则上面的方法转不出来

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值