diff --git a/result.go b/result.go index 32824ee..b9f5cda 100644 --- a/result.go +++ b/result.go @@ -4,18 +4,51 @@ // // Author : go_developer@163.com<白茶清欢> // -// Date : 2022-06-30 18:33 +// Date : 2022-06-30 18:33c package rpc +import ( + "errors" + "github.com/tidwall/gjson" + "strings" +) + // parseResponseBody 解析响应body, 支持 json + xml + ini + yaml // // Author : go_developer@163.com<白茶清欢> // // Date : 18:36 2022/6/30 -func parseResponseBody(body []byte, receiver interface{}) error { +func parseResponseBody(responseType string, body []byte, receiver interface{}) error { + responseType = strings.ToLower(responseType) + if strings.Contains(responseType, "json") { + return parser.ParseJSON(body, receiver) + } + if strings.Contains(responseType, "xml") { + return parser.ParseXml(body, receiver) + } + if strings.Contains(responseType, "yaml") || strings.Contains(responseType, "yml") { + return parser.ParseYaml(body, receiver) + } + if strings.Contains(responseType, "ini") { + return parser.ParseIni(body, receiver) + } + + // 适配 text/plan + if strings.Contains(responseType, "plain"){ + if !gjson.Valid(string(body)) { + return errors.New("text/plain 类型数据仅支持通过JSON解析") + } + return parser.ParseJSON(body, receiver) + } return nil } +var parser *result +func init() { + parser = &result{} + parser = &result{} +} + type result struct { }