forked from getkin/kin-openapi
/
extension.go
37 lines (32 loc) · 1.07 KB
/
extension.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
package openapi3
import (
"github.com/getkin/kin-openapi/jsoninfo"
)
// ExtensionProps provides support for OpenAPI extensions.
// It reads/writes all properties that begin with "x-".
type ExtensionProps struct {
Extensions map[string]interface{} `json:"-"`
}
// Assert that the type implements the interface
var _ jsoninfo.StrictStruct = &ExtensionProps{}
// EncodeWith will be invoked by package "jsoninfo"
func (props *ExtensionProps) EncodeWith(encoder *jsoninfo.ObjectEncoder, value interface{}) error {
for k, v := range props.Extensions {
if err := encoder.EncodeExtension(k, v); err != nil {
return err
}
}
return encoder.EncodeStructFieldsAndExtensions(value)
}
// DecodeWith will be invoked by package "jsoninfo"
func (props *ExtensionProps) DecodeWith(decoder *jsoninfo.ObjectDecoder, value interface{}) error {
source := decoder.DecodeExtensionMap()
if len(source) > 0 {
result := make(map[string]interface{}, len(source))
for k, v := range source {
result[k] = v
}
props.Extensions = result
}
return decoder.DecodeStructFieldsAndExtensions(value)
}