You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
naza/pkg/nazahttp/body.go

42 lines
1005 B
Go

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

// Copyright 2020, Chef. All rights reserved.
// https://github.com/q191201771/naza
//
// Use of this source code is governed by a MIT-style license
// that can be found in the License file.
//
// Author: Chef (191201771@qq.com)
package nazahttp
import (
"encoding/json"
"io/ioutil"
"net/http"
"github.com/q191201771/naza/pkg/nazajson"
)
// @brief 从http请求中解析body中的json字符串并反序列化至结构体中
//
// @param r http请求对象
// @param info 输出参数,用于接收反序列化之后的数据
// @param keyFieldList 可选参数可指定一个或多个json中必须存在的字段
func UnmarshalRequestJsonBody(r *http.Request, info interface{}, keyFieldList ...string) error {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
return err
}
j, err := nazajson.New(body)
if err != nil {
return err
}
for _, kf := range keyFieldList {
if !j.Exist(kf) {
return ErrParamMissing
}
}
return json.Unmarshal(body, info)
}