beegoinit/response/xml.go

49 lines
902 B
Go
Raw Normal View History

2020-07-10 10:51:40 +08:00
package response
import (
"bytes"
"encoding/xml"
"log"
)
2020-07-10 10:51:40 +08:00
type Xml struct {
Code int `xml:"code"`
Data interface{} `xml:"data"`
Msg string `xml:"msg"`
Status bool `xml:"status"`
}
func (x *Xml) String() string {
b, err := xml.MarshalIndent(x, "", " ")
if err != nil {
log.Println("xml encode", err)
}
return xml.Header + bytes.NewBuffer(b).String()
}
func NewXmlByDefaultSuccess(data ...interface{}) Xml {
return NewXml(true, 0, JsonMsgDefaultOk, data...)
}
func NewXmlByDefaultFailed(data ...interface{}) Xml {
return NewXml(false, 1, JsonMsgDefaultFailed, data...)
}
func NewXml(status bool, code int, msg string, data ...interface{}) Xml {
if len(data) > 0 {
return Xml{
Code: code,
Data: data[0],
Msg: msg,
Status: status,
}
} else {
return Xml{
Code: code,
Data: nil,
Msg: msg,
Status: status,
}
}
}