-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtext.go
216 lines (191 loc) · 5.72 KB
/
text.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package chimera
import (
"context"
"io"
"net/http"
"reflect"
"github.com/invopop/jsonschema"
)
var (
_ RequestReader = new(PlainTextRequest[Nil])
_ ResponseWriter = new(PlainTextResponse[Nil])
_ RequestReader = new(PlainText[Nil])
_ ResponseWriter = new(PlainText[Nil])
)
// PlainTextRequest is any text/plain request that results in a string body
type PlainTextRequest[Params any] struct {
request *http.Request
Body string
Params Params
}
// Context returns the context that was part of the original http.Request
func (r *PlainTextRequest[Params]) Context() context.Context {
if r.request != nil {
return r.request.Context()
}
return nil
}
func readPlainTextRequest[Params any](req *http.Request, body *string, params *Params) error {
defer req.Body.Close()
b, err := io.ReadAll(req.Body)
if err != nil {
return err
}
*body = string(b)
if _, ok := any(params).(*Nil); !ok {
err = UnmarshalParams(req, params)
if err != nil {
return err
}
}
return nil
}
// ReadRequest reads the body of an http request and assigns it to the Body field using io.ReadAll.
// This function also reads the parameters using UnmarshalParams and assigns it to the Params field.
// NOTE: the body of the request is closed after this function is run.
func (r *PlainTextRequest[Params]) ReadRequest(req *http.Request) error {
r.request = req
return readPlainTextRequest(req, &r.Body, &r.Params)
}
func textRequestSpec[Params any](schema *RequestSpec) {
schema.RequestBody = &RequestBody{
Content: map[string]MediaType{
"text/plain": {
Schema: &jsonschema.Schema{
Type: "string",
},
},
},
}
pType := reflect.TypeOf(new(Params))
for ; pType.Kind() == reflect.Pointer; pType = pType.Elem() {
}
if pType != reflect.TypeOf(Nil{}) {
schema.Parameters = CacheRequestParamsType(pType)
}
}
// OpenAPIRequestSpec describes the RequestSpec for text/plain requests
func (r *PlainTextRequest[Params]) OpenAPIRequestSpec() RequestSpec {
schema := RequestSpec{}
textRequestSpec[Params](&schema)
return schema
}
// PlainTextRequest[Params] is any text/plain response that uses a string body
type PlainTextResponse[Params any] struct {
Body string
Params Params
}
// WriteBody writes the response
func (r *PlainTextResponse[Params]) WriteBody(write BodyWriteFunc) error {
_, err := write([]byte(r.Body))
return err
}
func textResponsesSpec[Params any](schema Responses) {
response := ResponseSpec{}
response.Content = map[string]MediaType{
"text/plain": {
Schema: &jsonschema.Schema{
Type: "string",
},
},
}
pType := reflect.TypeOf(*new(Params))
for ; pType.Kind() == reflect.Pointer; pType = pType.Elem() {
}
if pType != reflect.TypeOf(Nil{}) {
response.Headers = make(map[string]Parameter)
for _, param := range CacheResponseParamsType(pType) {
response.Headers[param.Name] = Parameter{
Schema: param.Schema,
Description: param.Description,
Deprecated: param.Deprecated,
AllowReserved: param.AllowReserved,
AllowEmptyValue: param.AllowEmptyValue,
Required: param.Required,
Explode: param.Explode,
Example: param.Example,
Examples: param.Examples,
}
}
}
schema[""] = response
}
// OpenAPIResponsesSpec describes the Responses for text/plain requests
func (r *PlainTextResponse[Params]) OpenAPIResponsesSpec() Responses {
schema := make(Responses)
textResponsesSpec[Params](schema)
return schema
}
// WriteHead write the header for this response object
func (r *PlainTextResponse[Params]) WriteHead(head *ResponseHead) error {
head.Headers.Set("Content-Type", "text/plain")
h, err := MarshalParams(&r.Params)
if err != nil {
return err
}
for k, v := range h {
for _, x := range v {
head.Headers.Add(k, x)
}
}
return nil
}
// NewPlainTextResponse creates a PlainTextResponse from a string and params
func NewPlainTextResponse[Params any](body string, params Params) *PlainTextResponse[Params] {
return &PlainTextResponse[Params]{
Body: body,
Params: params,
}
}
// PlainText[Params] is a helper type that effectively works as both a PlainTextRequest[Params] and PlainTextResponse[Params]
// This is mostly here for convenience
type PlainText[Params any] struct {
request *http.Request
Body string
Params Params
}
// Context returns the context that was part of the original http.Request
func (r *PlainText[Params]) Context() context.Context {
if r.request != nil {
return r.request.Context()
}
return nil
}
// ReadRequest reads the body of an http request and assigns it to the Body field using io.ReadAll.
// This function also reads the parameters using UnmarshalParams and assigns it to the Params field.
// NOTE: the body of the request is closed after this function is run.
func (r *PlainText[Params]) ReadRequest(req *http.Request) error {
r.request = req
return readPlainTextRequest(req, &r.Body, &r.Params)
}
// OpenAPIRequestSpec describes the RequestSpec for text/plain requests
func (r *PlainText[Params]) OpenAPIRequestSpec() RequestSpec {
schema := RequestSpec{}
textRequestSpec[Params](&schema)
return schema
}
// WriteBody writes the response body
func (r *PlainText[Params]) WriteBody(write BodyWriteFunc) error {
_, err := write([]byte(r.Body))
return err
}
// OpenAPIResponsesSpec describes the Responses for text/plain requests
func (r *PlainText[Params]) OpenAPIResponsesSpec() Responses {
schema := make(Responses)
textResponsesSpec[Params](schema)
return schema
}
// WriteHead writes the header for this response object
func (r *PlainText[Params]) WriteHead(head *ResponseHead) error {
head.Headers.Set("Content-Type", "text/plain")
h, err := MarshalParams(&r.Params)
if err != nil {
return err
}
for k, v := range h {
for _, x := range v {
head.Headers.Add(k, x)
}
}
return nil
}