forked from chanced/openapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
callback.go
131 lines (114 loc) · 3.24 KB
/
callback.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
package openapi
import (
"encoding/json"
"github.com/chanced/openapi/yamlutil"
"github.com/tidwall/gjson"
)
// CallbackKind indicates whether the CallbackObj is a Callback or a Reference
type CallbackKind uint8
const (
// CallbackKindObj = CallbackObj
CallbackKindObj CallbackKind = iota
// CallbackKindRef = Reference
CallbackKindRef
)
// CallbackObj is map of possible out-of band callbacks related to the parent
// operation. Each value in the map is a Path Item Object that describes a set
// of requests that may be initiated by the API provider and the expected
// responses. The key value used to identify the path item object is an
// expression, evaluated at runtime, that identifies a URL to use for the
// callback operation.
//
// To describe incoming requests from the API provider independent from another
// API call, use the webhooks field.
type CallbackObj struct {
Paths PathItems `json:"-"`
Extensions `json:"-"`
}
type callback CallbackObj
// MarshalJSON marshals JSON
func (c CallbackObj) MarshalJSON() ([]byte, error) {
b, err := json.Marshal(c.Paths)
if err != nil {
return b, err
}
return marshalExtendedJSONInto(b, callback(c))
}
// UnmarshalJSON unmarshals JSON
func (c *CallbackObj) UnmarshalJSON(data []byte) error {
*c = CallbackObj{
Paths: PathItems{},
Extensions: Extensions{},
}
var err error
gjson.ParseBytes(data).ForEach(func(key, value gjson.Result) bool {
d := []byte(value.Raw)
if IsExtensionKey(key.String()) {
c.Extensions.SetEncodedExtension(key.String(), d)
} else {
var v Path
v, err = unmarshalPathJSON(d)
c.Paths[key.String()] = v
}
if err != nil {
return false
}
return true
})
return err
}
// MarshalYAML marshals YAML
func (c CallbackObj) MarshalYAML() (interface{}, error) {
return yamlutil.Marshal(c)
}
// UnmarshalYAML unmarshals YAML
func (c *CallbackObj) UnmarshalYAML(unmarshal func(interface{}) error) error {
return yamlutil.Unmarshal(unmarshal, c)
}
// CallbackKind returns CallbackKindCallback
func (c *CallbackObj) CallbackKind() CallbackKind { return CallbackKindObj }
// ResolveCallback resolves CallbackObj by returning itself. resolve is not called.
func (c *CallbackObj) ResolveCallback(CallbackResolver) (*CallbackObj, error) {
return c, nil
}
// Callback can either be a CallbackObj or a Reference
type Callback interface {
ResolveCallback(CallbackResolver) (*CallbackObj, error)
CallbackKind() CallbackKind
}
// Callbacks is a map of reusable Callback Objects.
type Callbacks map[string]Callback
// UnmarshalJSON unmarshals JSON
func (c *Callbacks) UnmarshalJSON(data []byte) error {
var o map[string]json.RawMessage
res := make(Callbacks, len(o))
err := json.Unmarshal(data, &o)
if err != nil {
return err
}
for k, d := range o {
if isRefJSON(d) {
v, err := unmarshalReferenceJSON(d)
if err != nil {
return err
}
res[k] = v
} else {
var v CallbackObj
if err := json.Unmarshal(d, &v); err != nil {
return err
}
res[k] = &v
}
}
*c = res
return nil
}
// MarshalYAML marshals YAML
func (c Callbacks) MarshalYAML() (interface{}, error) {
return yamlutil.Marshal(c)
}
// UnmarshalYAML unmarshals YAML
func (c *Callbacks) UnmarshalYAML(unmarshal func(interface{}) error) error {
return yamlutil.Unmarshal(unmarshal, c)
}