diff --git a/result.go b/result.go index 198a4e9..975755c 100644 --- a/result.go +++ b/result.go @@ -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 }