forked from s-mang/test2doc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
87 lines (69 loc) · 1.49 KB
/
util.go
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package doc
import (
"bytes"
"encoding/json"
"io"
"io/ioutil"
"log"
"net/http"
"strings"
"text/template"
)
type nopCloser struct {
io.Reader
}
func (nopCloser) Close() error { return nil }
func cloneBody(r io.ReadCloser) (*bytes.Buffer, *bytes.Buffer, error) {
var clone1, clone2 bytes.Buffer
rBytes, err := ioutil.ReadAll(r)
if err != nil {
return &clone1, &clone2, err
}
r.Close()
mw := io.MultiWriter(&clone1, &clone2)
_, err = mw.Write(rBytes)
return &clone1, &clone2, err
}
func CopyHeader(dst, src http.Header) {
for k, vv := range src {
for _, v := range vv {
dst.Add(k, v)
}
}
}
func render(tmpl *template.Template, i interface{}) string {
var buf bytes.Buffer
err := tmpl.Execute(&buf, i)
if err != nil {
panic(err.Error())
}
return buf.String()
}
func commaJoin(args ...interface{}) string {
var strList []string
for _, arg := range args {
strs, ok := arg.([]string)
if ok {
for _, str := range strs {
strList = append(strList, str)
}
} else {
log.Println("Error: CommaJoinStrs called with non []string argument.")
}
}
return strings.Join(strList, ", ")
}
func formatBody(body, contentType string) (fbody string, err error) {
if contentType == "application/json" {
body, err = indentJSONBody(body)
}
return
}
func indentJSONBody(bodyStr string) (outStr string, err error) {
var outJSON bytes.Buffer
err = json.Indent(&outJSON, []byte(bodyStr), " ", " ")
if err != nil {
return
}
return outJSON.String(), nil
}