-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxml.go
More file actions
59 lines (48 loc) · 1.96 KB
/
xml.go
File metadata and controls
59 lines (48 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package harsp
import (
"github.com/clbanning/mxj"
"net/http"
)
type XML struct {
Rc RetCode
Data interface{}
}
// send data to http response.
// it returns the writer, then you can use the writer continue to write.
// @param: w http.ResponseWriter, all the data will use the writer write into the response
// @param: data interface, this is the response content
// @return: w http.ResponseWriter
func (this XML) send(w http.ResponseWriter, data map[string]interface{}) (http.ResponseWriter, error) {
w.Header().Set("Content-Type", MimeXML)
mv := mxj.Map(data)
content, err := mv.Xml()
if err != nil {
return w, ErrWriteFailed
}
_, err = w.Write(content)
if err != nil {
return w, ErrWriteFailed
}
return w, nil
}
// when you want to return a success msg to the http client, you can use this func.
// this func use the default errCode string 0, if you want change the default value,
// your must to set the package variable of SuccessCode to a new value,
// you can do it when you start your application, once you do that, it will always affected.
// @param: w http.ResponseWriter, all the data will use the writer write into the response
// @param: data interface, this is the response content
// @return: w http.ResponseWriter
func (this XML) Success(w http.ResponseWriter) (http.ResponseWriter, error) {
packedData := DefaultPadding(SuccessRet, this.Data)
return this.send(w, packedData)
}
// when you want to return a failed msg to the http client, you can use this func.
// you can define your errCode in your application, when you use this func to send a failed response,
// you can assign which errCode you wish to return.
// @param: w http.ResponseWriter, all the data will use the writer write into the response
// @param: data interface, this is the response content
// @return: w http.ResponseWriter
func (this XML) Failed(w http.ResponseWriter) (http.ResponseWriter, error) {
packedData := DefaultPadding(this.Rc, this.Data)
return this.send(w, packedData)
}