Skip to content

Commit 256e3b6

Browse files
Warboss-rusilya.bogdanovilya.bogdanov
authored
Strict server generation (#499)
* Added strict server generation that does automatic marshaling of request and response bodies, meaning less manual code and forced schema compliance * Support any content type by using io.Reader * removed generating marshalling code from client for content types other than JSON * Multipart and formdata are now passed as forms without any binding * run make generate to fix the build * added sorting of request bodies to reduce number of random changes in generated code, fixed content type in RequestBody's comment * Implemented basic formdata binder (not yet integrated into strict server) * Added tests for strict generation, support for form marshalling, client improvements * Fixed incorrect referencing of request bodies, added sorting of response definitions * Added sorting of content types in responses * Support multipart responses using callback function, added header example * Added sorting of headers in response objects * Added proper testing to strict servers * Fix after master merge * Reuse responses defined in components section, moved strict test to tests * When multiple responses ref to a single reusable response, only the first one will use an alias, all others will generate new structs * Update generated code after merge * Some documentation for strict server * Support for AdditionalProperties when binding forms Co-authored-by: ilya.bogdanov <ilya.bogdanov@ispringsolutions.com> Co-authored-by: ilya.bogdanov <ilya.bogdanov@ispring.com>
1 parent 8c9801f commit 256e3b6

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

file.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package types
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"io"
7+
"mime/multipart"
8+
)
9+
10+
type File struct {
11+
multipart *multipart.FileHeader
12+
data []byte
13+
filename string
14+
}
15+
16+
func (file *File) InitFromMultipart(header *multipart.FileHeader) {
17+
file.multipart = header
18+
file.data = nil
19+
file.filename = ""
20+
}
21+
22+
func (file *File) InitFromBytes(data []byte, filename string) {
23+
file.data = data
24+
file.filename = filename
25+
file.multipart = nil
26+
}
27+
28+
func (file *File) MarshalJSON() ([]byte, error) {
29+
b, err := file.Bytes()
30+
if err != nil {
31+
return nil, err
32+
}
33+
return json.Marshal(b)
34+
}
35+
36+
func (file *File) UnmarshalJSON(data []byte) error {
37+
return json.Unmarshal(data, &file.data)
38+
}
39+
40+
func (file File) Bytes() ([]byte, error) {
41+
if file.multipart != nil {
42+
f, err := file.multipart.Open()
43+
if err != nil {
44+
return nil, err
45+
}
46+
defer func() { _ = f.Close() }()
47+
return io.ReadAll(f)
48+
}
49+
return file.data, nil
50+
}
51+
52+
func (file File) Reader() (io.ReadCloser, error) {
53+
if file.multipart != nil {
54+
return file.multipart.Open()
55+
}
56+
return io.NopCloser(bytes.NewReader(file.data)), nil
57+
}
58+
59+
func (file File) Filename() string {
60+
if file.multipart != nil {
61+
return file.multipart.Filename
62+
}
63+
return file.filename
64+
}
65+
66+
func (file File) FileSize() int64 {
67+
if file.multipart != nil {
68+
return file.multipart.Size
69+
}
70+
return int64(len(file.data))
71+
}

0 commit comments

Comments
 (0)