-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparser.go
195 lines (183 loc) · 4.65 KB
/
parser.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// Package sRequest -----------------------------
// file : parser.go
// author : JJXu
// contact : [email protected]
// time : 2022/12/10 00:48:45
// -------------------------------------------
package simpleRequest
import (
"bytes"
"encoding/json"
"encoding/xml"
"fmt"
"io"
"mime/multipart"
"net/url"
"os"
"path/filepath"
"strings"
)
// 通用类型解析器
var bodyEntryParsers = map[string]IBodyEntryParser{
jsonContentType: new(JsonParser),
formDataType: new(FormDataParser),
xmlDataType: new(XmlParser),
}
type IBodyEntryParser interface {
Unmarshal(bodyType EntryMark, BodyEntry map[string]any) io.Reader
}
type JsonParser struct{}
func (JsonParser) Unmarshal(bodyType EntryMark, BodyEntry map[string]any) io.Reader {
switch bodyType {
case StringEntryType:
return GetStringEntryTypeBody(BodyEntry)
case BytesEntryType:
return GetBytesEntryTypeBody(BodyEntry)
case ModelEntryType:
jsonData, err := json.Marshal(BodyEntry[ModelEntryType.string()])
if err == nil {
return bytes.NewReader(jsonData)
}
return strings.NewReader("{}")
case MapEntryType:
jsonData, err := json.Marshal(BodyEntry)
if err == nil {
return bytes.NewReader(jsonData)
} else {
return strings.NewReader("{}")
}
default:
if len(BodyEntry) > 0 {
jsonData, err := json.Marshal(BodyEntry)
if err == nil {
return bytes.NewReader(jsonData)
}
}
return strings.NewReader("{}")
}
}
type FormDataParser struct {
ContentType string
}
func (f *FormDataParser) Unmarshal(bodyType EntryMark, BodyEntry map[string]any) (body io.Reader) {
switch bodyType {
case MapEntryType:
body, f.ContentType = multipartCommonParse(BodyEntry)
case ModelEntryType:
tb := BodyEntry[ModelEntryType.string()]
buffer, err := json.Marshal(tb)
if err != nil {
panic(err.Error())
}
var mapper map[string]any
err = json.Unmarshal(buffer, &mapper)
if err != nil {
panic(err.Error())
}
body, f.ContentType = multipartCommonParse(mapper)
case StringEntryType:
return GetStringEntryTypeBody(BodyEntry)
case BytesEntryType:
return GetBytesEntryTypeBody(BodyEntry)
default:
body, f.ContentType = multipartCommonParse(BodyEntry)
}
return
}
func multipartCommonParse(BodyEntry map[string]any) (reader io.Reader, contentType string) {
body := &bytes.Buffer{}
formWriter := multipart.NewWriter(body)
for k, sv := range BodyEntry {
if strings.Contains(k, FormFilePathKey.string()) {
fieldName := k[len(FormFilePathKey):]
fp := sv.(string)
filename := filepath.Base(fp)
//way1
filePart, _ := formWriter.CreateFormFile(fieldName, filename)
content, err := os.ReadFile(fp)
if err != nil {
panic(err)
}
_, _ = filePart.Write(content)
} else {
switch multValue := sv.(type) {
case string:
_ = formWriter.WriteField(k, multValue)
case []string:
sss, _ := sv.([]string)
for _, v := range sss {
_ = formWriter.WriteField(k, v)
}
case *multipart.FileHeader:
filePart, _ := formWriter.CreateFormFile(k, multValue.Filename)
src, err := multValue.Open()
if err != nil {
panic(err)
return
}
defer src.Close()
_, err = io.Copy(filePart, src)
if err != nil {
panic(err)
return
}
case []byte:
formWriter.WriteField(k, string(multValue))
case int:
formWriter.WriteField(k, fmt.Sprintf("%v", multValue))
}
}
}
err := formWriter.Close()
if err != nil {
panic(err)
}
return body, formWriter.FormDataContentType()
}
type XmlParser struct{}
func (f XmlParser) Unmarshal(bodyType EntryMark, BodyEntry map[string]any) (body io.Reader) {
switch bodyType {
case MapEntryType:
xmlData, err := xml.Marshal(BodyEntry[bodyType.string()])
if err == nil {
return bytes.NewReader(xmlData)
} else {
return strings.NewReader("")
}
case ModelEntryType:
xmlData, err := xml.Marshal(BodyEntry[bodyType.string()])
if err == nil {
return bytes.NewReader(xmlData)
} else {
return strings.NewReader("")
}
case StringEntryType:
return GetStringEntryTypeBody(BodyEntry)
case BytesEntryType:
return GetBytesEntryTypeBody(BodyEntry)
default:
return strings.NewReader("")
}
}
type CommonParser struct {
}
func (f CommonParser) Unmarshal(bodyType EntryMark, BodyEntry map[string]any) (body io.Reader) {
tmpData := url.Values{}
for k, v := range BodyEntry {
switch k {
case StringEntryType.string():
body = GetStringEntryTypeBody(BodyEntry)
break
case BytesEntryType.string():
body = GetBytesEntryTypeBody(BodyEntry)
break
default:
if strings.HasPrefix(k, FormFilePathKey.string()) {
k = k[len(FormFilePathKey):]
}
tmpData.Set(k, fmt.Sprintf("%v", v))
}
}
body = strings.NewReader(tmpData.Encode())
return
}