forked from ogen-go/ogen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
120 lines (105 loc) · 2.29 KB
/
utils.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
package gen
import (
"fmt"
"net/http"
"go.uber.org/zap"
"github.com/ogen-go/ogen/gen/ir"
"github.com/ogen-go/ogen/jsonschema"
"github.com/ogen-go/ogen/location"
)
func unreachable(v any) string {
return fmt.Sprintf("unreachable: %v", v)
}
func isBinary(s *jsonschema.Schema) bool {
if s == nil {
return false
}
switch s.Type {
case jsonschema.Empty, jsonschema.String:
return s.Format == "binary"
default:
return false
}
}
func isStream(s *jsonschema.Schema) bool {
// https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#considerations-for-file-uploads
//
// The Spec says:
//
// Content transferred in binary (octet-stream) MAY omit schema.
//
if s == nil {
return true
}
switch s.Type {
case jsonschema.Empty, jsonschema.String:
default:
return false
}
// Allow format to be empty, stream body often defined as just string.
switch s.Format {
case "", "binary", "byte", "base64":
default:
return false
}
// TODO(tdakkota): check ContentEncoding field
return true
}
// isMultipartFile tries to map field to multipart file.
//
// Returns nil type if field is not a file parameter.
func isMultipartFile(ctx *genctx, t *ir.Type, p *jsonschema.Property) (*ir.Type, error) {
if p == nil || p.Schema == nil {
return nil, nil
}
file := ir.Primitive(ir.File, p.Schema)
switch {
case t.IsGeneric():
v := t.GenericVariant
if !isBinary(p.Schema) || !v.OnlyOptional() {
return nil, nil
}
r := ir.Generic("MultipartFile", file, v)
if err := ctx.saveType(r); err != nil {
return nil, err
}
return r, nil
case t.IsArray():
if !isBinary(p.Schema.Item) {
return nil, nil
}
r := ir.Array(file, ir.NilNull, p.Schema)
r.Validators = ir.Validators{
Array: t.Validators.Array,
}
return r, nil
case t.IsPrimitive():
if !isBinary(p.Schema) {
return nil, nil
}
return file, nil
}
return nil, nil
}
func statusText(code int) string {
r := http.StatusText(code)
if r != "" {
return r
}
return fmt.Sprintf("Code%d", code)
}
type position interface {
Position() (location.Position, bool)
File() location.File
}
func zapPosition(l position) zap.Field {
if l == nil {
return zap.Skip()
}
loc, ok := l.Position()
if !ok {
return zap.Skip()
}
file := l.File()
return zap.String("at", loc.WithFilename(file.Name))
}