-
Notifications
You must be signed in to change notification settings - Fork 2
/
encoding.go
175 lines (155 loc) · 5.38 KB
/
encoding.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
package openapi
import (
"encoding/json"
"github.com/chanced/transcode"
"gopkg.in/yaml.v3"
)
// EncodingMap is a ComponentMap between a property name and its encoding information. The
// key, being the property name, MUST exist in the schema as a property. The
// encoding object SHALL only apply to requestBody objects when the media type
// is multipart or application/x-www-form-urlencoded.
type EncodingMap = ComponentMap[*Encoding]
// Encoding definition applied to a single schema property.
type Encoding struct {
Extensions `json:"-"`
Location `json:"-"`
// The Content-Type for encoding a specific property. Default value depends
// on the property type:
//
// - for object - application/json;
// - for array – the default is defined based on the inner type;
// - for all other cases the default is application/octet-stream.
// The value can be a specific media type (e.g. application/json), a
// wildcard media type (e.g. image/*), or a comma-separated list of the two
// types.
ContentType Text `json:"contentType,omitempty"`
// A map allowing additional information to be provided as headers, for
// example Content-Disposition. Content-Type is described separately and
// SHALL be ignored in this section. This property SHALL be ignored if the
// request body media type is not a multipart.
Headers *HeaderMap `json:"headers,omitempty"`
// Describes how a specific property value will be serialized depending on
// its type. See Parameter Object for details on the style property. The
// behavior follows the same values as query parameters, including default
// values. This property SHALL be ignored if the request body media type is
// not application/x-www-form-urlencoded or multipart/form-data. If a value
// is explicitly defined, then the value of contentType (implicit or
// explicit) SHALL be ignored.
Style Text `json:"style,omitempty"`
// When this is true, property values of type array or object generate
// separate parameters for each value of the array, or key-value-pair of the
// map. For other types of properties this property has no effect. When
// style is form, the default value is true. For all other styles, the
// default value is false. This property SHALL be ignored if the request
// body media type is not application/x-www-form-urlencoded or
// multipart/form-data. If a value is explicitly defined, then the value of
// contentType (implicit or explicit) SHALL be ignored.
Explode *bool `json:"explode,omitempty"`
// Determines whether the parameter value SHOULD allow reserved characters,
// as defined by RFC3986 :/?#[]@!$&'()*+,;= to be included without
// percent-encoding. The default value is false. This property SHALL be
// ignored if the request body media type is not
// application/x-www-form-urlencoded or multipart/form-data. If a value is
// explicitly defined, then the value of contentType (implicit or explicit)
// SHALL be ignored.
AllowReserved *bool `json:"allowReserved,omitempty"`
}
func (e *Encoding) Nodes() []Node {
if e == nil {
return nil
}
return downcastNodes(e.nodes())
}
func (e *Encoding) nodes() []node {
if e == nil {
return nil
}
return appendEdges(nil, e.Headers)
}
func (e *Encoding) Refs() []Ref {
if e == nil {
return nil
}
return e.Headers.Refs()
}
func (e *Encoding) Anchors() (*Anchors, error) {
if e == nil {
return nil, nil
}
return e.Headers.Anchors()
}
// func (e *Encoding) ResolveNodeByPointer(ptr jsonpointer.Pointer) (Node, error) {
// err := ptr.Validate()
// if err != nil {
// return nil, err
// }
// return e.resolveNodeByPointer(ptr)
// }
// func (e *Encoding) resolveNodeByPointer(ptr jsonpointer.Pointer) (Node, error) {
// if ptr.IsRoot() {
// return e, nil
// }
// nxt, tok, _ := ptr.Next()
// switch nxt {
// case "headers":
// if e.Headers == nil {
// return nil, newErrNotFound(e.Location.AbsoluteLocation(), tok)
// }
// return e.Headers.resolveNodeByPointer(nxt)
// default:
// return nil, newErrNotResolvable(e.Location.AbsoluteLocation(), tok)
// }
// }
func (*Encoding) Kind() Kind { return KindEncoding }
func (*Encoding) mapKind() Kind { return KindEncodingMap }
func (*Encoding) sliceKind() Kind { return KindUndefined }
func (e *Encoding) setLocation(loc Location) error {
if e == nil {
return nil
}
e.Location = loc
return e.Headers.setLocation(loc.AppendLocation("headers"))
}
// MarshalJSON marshals e into JSON
func (e Encoding) MarshalJSON() ([]byte, error) {
type encoding Encoding
return marshalExtendedJSON(encoding(e))
}
// UnmarshalJSON unmarshals json into e
func (e *Encoding) UnmarshalJSON(data []byte) error {
type encoding Encoding
v := encoding{}
if err := unmarshalExtendedJSON(data, &v); err != nil {
return err
}
*e = Encoding(v)
return nil
}
// UnmarshalYAML satisfies gopkg.in/yaml.v3 Marshaler interface
func (e Encoding) MarshalYAML() (interface{}, error) {
j, err := e.MarshalJSON()
if err != nil {
return nil, err
}
var v interface{}
err = json.Unmarshal(j, &v)
if err != nil {
return nil, err
}
return v, nil
}
// UnmarshalYAML satisfies gopkg.in/yaml.v3 Unmarshaler interface
func (e *Encoding) UnmarshalYAML(value *yaml.Node) error {
v, err := yaml.Marshal(value)
if err != nil {
return err
}
j, err := transcode.JSONFromYAML(v)
if err != nil {
return err
}
return json.Unmarshal(j, e)
}
func (e *Encoding) isNil() bool { return e == nil }
func (*Encoding) refable() {}
var _ node = (*Encoding)(nil)