增加yaml/xml结果解析

This commit is contained in:
白茶清欢 2022-07-01 11:12:28 +08:00
parent 696d31b0f2
commit 2d69d673ec

View File

@ -10,9 +10,12 @@ package rpc
import (
"bytes"
"encoding/json"
"encoding/xml"
"errors"
"github.com/tidwall/gjson"
"strings"
"github.com/tidwall/gjson"
"gopkg.in/yaml.v2"
)
// parseResponseBody 解析响应body, 支持 json + xml + ini + yaml
@ -36,7 +39,7 @@ func parseResponseBody(responseType string, body []byte, receiver interface{}) e
}
// 适配 text/plan
if strings.Contains(responseType, "plain"){
if strings.Contains(responseType, "plain") {
if !gjson.Valid(string(body)) {
return errors.New("text/plain 类型数据仅支持通过JSON解析")
}
@ -46,7 +49,8 @@ func parseResponseBody(responseType string, body []byte, receiver interface{}) e
}
var parser *result
func init() {
func init() {
parser = &result{}
parser = &result{}
}
@ -71,7 +75,8 @@ func (r *result) ParseJSON(inputContent []byte, receiver interface{}) error {
//
// Date : 18:34 2022/6/30
func (r *result) ParseYaml(inputContent []byte, receiver interface{}) error {
return nil
decoder := yaml.NewDecoder(bytes.NewReader(inputContent))
return decoder.Decode(receiver)
}
// ParseXml 解析xml数据
@ -80,9 +85,14 @@ func (r *result) ParseYaml(inputContent []byte, receiver interface{}) error {
//
// Date : 18:35 2022/6/30
func (r *result) ParseXml(inputContent []byte, receiver interface{}) error {
return nil
return xml.Unmarshal(inputContent, receiver)
}
// ParseIni 解析ini
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 11:11 2022/7/1
func (r *result) ParseIni(inputContent []byte, receiver interface{}) error {
return nil
}