-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparameter_list.go
More file actions
81 lines (65 loc) · 1.86 KB
/
parameter_list.go
File metadata and controls
81 lines (65 loc) · 1.86 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
package openapi
import (
"errors"
"fmt"
"github.com/MarkRosemaker/errpath"
)
// ParameterList is a list of parameter references.
type ParameterList []*ParameterRef
type parameterID struct {
Name string
Location ParameterLocation
}
func (p ParameterList) Validate() error {
// The list MUST NOT include duplicated parameters. A unique parameter is defined by a combination of a name and location.
params := make(map[parameterID]error, len(p))
for i, param := range p {
// check for duplicates
id := parameterID{Name: param.Value.Name, Location: param.Value.In}
errNotUnique := &errpath.ErrIndex{
Index: i,
Err: &errpath.ErrField{
Field: "name",
Err: &errpath.ErrInvalid[string]{
Value: param.Value.Name,
Message: fmt.Sprintf("not unique in %s", param.Value.In),
},
},
}
if prevInstance := params[id]; prevInstance != nil {
// output both instances of the parameter
return errors.Join(prevInstance, errNotUnique)
}
params[id] = errNotUnique
if err := param.Validate(); err != nil {
return &errpath.ErrIndex{Index: i, Err: err}
}
}
return nil
}
// In is a convenience function to filter by a specific parameter location.
func (p ParameterList) In(in ParameterLocation) ParameterList {
var result ParameterList
for _, param := range p {
if param.Value.In == in {
result = append(result, param)
}
}
return result
}
// InPath returns all path parameters from the list.
func (p ParameterList) InPath() ParameterList {
return p.In(ParameterLocationPath)
}
// InQuery returns all query parameters from the list.
func (p ParameterList) InQuery() ParameterList {
return p.In(ParameterLocationQuery)
}
func (l *loader) resolveParameterList(p ParameterList) error {
for i, param := range p {
if err := l.resolveParameterRef(param); err != nil {
return &errpath.ErrIndex{Index: i, Err: err}
}
}
return nil
}