-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathreference.go
More file actions
142 lines (116 loc) · 3.94 KB
/
Copy pathreference.go
File metadata and controls
142 lines (116 loc) · 3.94 KB
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
package swagger
import (
"context"
"errors"
"fmt"
"github.com/speakeasy-api/openapi/internal/interfaces"
"github.com/speakeasy-api/openapi/marshaller"
"github.com/speakeasy-api/openapi/pointer"
"github.com/speakeasy-api/openapi/references"
"github.com/speakeasy-api/openapi/swagger/core"
"github.com/speakeasy-api/openapi/validation"
)
type (
// ReferencedParameter represents a parameter that can either be referenced from elsewhere or declared inline.
ReferencedParameter = Reference[Parameter, *Parameter, *core.Parameter]
// ReferencedResponse represents a response that can either be referenced from elsewhere or declared inline.
ReferencedResponse = Reference[Response, *Response, *core.Response]
)
// NewReferencedParameterFromRef creates a new ReferencedParameter from a reference string.
func NewReferencedParameterFromRef(ref references.Reference) *ReferencedParameter {
return &ReferencedParameter{
Reference: &ref,
}
}
// NewReferencedParameterFromParameter creates a new ReferencedParameter from a Parameter.
func NewReferencedParameterFromParameter(parameter *Parameter) *ReferencedParameter {
return &ReferencedParameter{
Object: parameter,
}
}
// NewReferencedResponseFromRef creates a new ReferencedResponse from a reference string.
func NewReferencedResponseFromRef(ref references.Reference) *ReferencedResponse {
return &ReferencedResponse{
Reference: &ref,
}
}
// NewReferencedResponseFromResponse creates a new ReferencedResponse from a Response.
func NewReferencedResponseFromResponse(response *Response) *ReferencedResponse {
return &ReferencedResponse{
Object: response,
}
}
// Reference represents an object that can either be referenced from elsewhere or declared inline.
type Reference[T any, V interfaces.Validator[T], C marshaller.CoreModeler] struct {
marshaller.Model[core.Reference[C]]
// Reference is the reference string ($ref).
Reference *references.Reference
// If this was an inline object instead of a reference this will contain that object.
Object *T
}
var _ interfaces.Model[core.Reference[*core.Parameter]] = (*Reference[Parameter, *Parameter, *core.Parameter])(nil)
// IsReference returns true if the reference is a reference (via $ref) to an object as opposed to an inline object.
func (r *Reference[T, V, C]) IsReference() bool {
if r == nil {
return false
}
return r.Reference != nil
}
// GetReference returns the value of the Reference field. Returns empty string if not set.
func (r *Reference[T, V, C]) GetReference() references.Reference {
if r == nil || r.Reference == nil {
return ""
}
return *r.Reference
}
// GetObject returns the referenced object. If this is a reference, this will return nil.
func (r *Reference[T, V, C]) GetObject() *T {
if r == nil {
return nil
}
if r.IsReference() {
return nil
}
return r.Object
}
// Validate validates the Reference object against the Swagger Specification.
func (r *Reference[T, V, C]) Validate(ctx context.Context, opts ...validation.Option) []error {
if r == nil {
return []error{errors.New("reference is nil")}
}
c := r.GetCore()
if c == nil {
return []error{errors.New("reference core is nil")}
}
errs := []error{}
if c.Reference.Present && r.Object != nil {
// Use the validator interface V to validate the object
var validator V
if v, ok := any(r.Object).(V); ok {
validator = v
errs = append(errs, validator.Validate(ctx, opts...)...)
}
}
r.Valid = len(errs) == 0 && c.GetValid()
return errs
}
func (r *Reference[T, V, C]) Populate(source any) error {
var s *core.Reference[C]
switch src := source.(type) {
case *core.Reference[C]:
s = src
case core.Reference[C]:
s = &src
default:
return fmt.Errorf("expected *core.Reference[C] or core.Reference[C], got %T", source)
}
if s.Reference.Present {
r.Reference = pointer.From(references.Reference(*s.Reference.Value))
} else {
if err := marshaller.PopulateWithContext(s.Object, &r.Object, nil); err != nil {
return err
}
}
r.SetCore(s)
return nil
}