如何解决模式并符合 GO
我想修剪 API 正文数据(输入数据)中的空白
所以我尝试使用这些包 github.com/gorilla/schema 和 github.com/leebenson/conform 以及由供应商 mod 管理的依赖项。
我的功能
func (hh *hiveHandler) Create() gin.HandlerFunc {
return func(ctx *gin.Context) {
p := models.Post{}
err := ctx.ShouldBindJSON(&p)
err = schema.NewDecoder().Decode(p,ctx.Request.Body) // <-- Gorilla Schema
conform.Strings(p)
fmt.Println("creating",zap.Any("post",p))
}
}
type Post struct {
HiveID uint64 `json:"hiveId" jsonschema:"minLength=27,maxLength=27" schema:"hiveId"`
IsPinnedPost bool `json:"isPinnedPost" schema:"isPinnedPost"`
PostID uint64 `json:"postId" schema:"postId"`
PostDatetime time.Time `json:"postDatetime" schema:"postDatetime"`
Edits Edits `json:"edits,omitempty" schema:"edits" `
ImpartWealthID string `json:"impartWealthId" schema:"impartWealthId"`
ScreenName string `json:"screenName" schema:"screenName" conform:"trim"`
Subject string `json:"subject" schema:"subject" conform:"trim"`
Content Content `json:"content" schema:"content" `
CommentCount int `json:"commentCount" schema:"commentCount" `
TagIDs tags.TagIDs `json:"tags" schema:"tags"`
PostCommentTrack PostCommentTrack `json:"postCommentTrack,omitempty" schema:"postCommentTrack"`
Comments Comments `json:"comments,omitempty" schema:"comments"`
NextCommentPage *NextPage `json:"nextCommentPage" schema:"nextCommentPage"`
}
但它在行中显示错误
schema.NewDecoder().Decode(p,ctx.Request.Body)
我的内容类型 = application/json
我遵循了文档
https://github.com/leebenson/conform
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。