-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.go
48 lines (40 loc) · 2 KB
/
example.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
package openapi
import (
"fmt"
"net/url"
"github.com/go-json-experiment/json/jsontext"
)
// Example represents an example of a schema.
//
// In all cases, the example value is expected to be compatible with the type schema of its associated value.
// Tooling implementations MAY choose to validate compatibility automatically, and reject the example value(s) if incompatible.
type Example struct {
// Short description for the example.
Summary string `json:"summary,omitempty" yaml:"summary,omitempty"`
// Long description for the example. CommonMark syntax MAY be used for rich text representation.
Description string `json:"description,omitempty" yaml:"description,omitempty"`
// Embedded literal example. The `value` field and `externalValue` field are mutually exclusive. To represent examples of media types that cannot naturally represented in JSON or YAML, use a string value to contain the example, escaping where necessary.
Value jsontext.Value `json:"value,omitempty" yaml:"value,omitempty"`
// A URI that points to the literal example. This provides the capability to reference examples that cannot easily be included in JSON or YAML documents.
// The `value` field and `externalValue` field are mutually exclusive. See the rules for resolving [Relative References](#relative-references-in-uris).
ExternalValue *url.URL `json:"externalValue,omitempty" yaml:"externalValue,omitempty"`
// This object MAY be extended with Specification Extensions.
Extensions Extensions `json:",inline" yaml:"-"`
}
func (ex *Example) Validate() error {
if ex.Value != nil && ex.ExternalValue != nil {
return fmt.Errorf("value and externalValue are mutually exclusive")
}
return validateExtensions(ex.Extensions)
}
func (l *loader) collectExampleRef(ex *ExampleRef, ref ref) {
if ex.Value != nil {
l.collectExample(ex.Value, ref)
}
}
func (l *loader) collectExample(ex *Example, ref ref) {
l.examples[ref.String()] = ex
}
func (l *loader) resolveExampleRef(ex *ExampleRef) error {
return resolveRef(ex, l.examples, nil)
}