-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.go
141 lines (120 loc) · 3.27 KB
/
server.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
132
133
134
135
136
137
138
139
140
141
package openapi
import (
"encoding/json"
"github.com/chanced/transcode"
"gopkg.in/yaml.v3"
)
type (
ServerSlice = ObjSlice[*Server]
ServerVariableMap = ObjMap[*ServerVariable]
)
// Server representation of a Server.
type Server struct {
Location `json:"-"`
Extensions `json:"-"`
// A URL to the target host. This URL supports Server Variables and MAY be
// relative, to indicate that the host location is relative to the location
// where the OpenAPI document is being served. Variable substitutions will
// be made when a variable is named in {brackets}.
URL Text `json:"url"`
// Description of the host designated by the URL. CommonMark syntax MAY be
// used for rich text representation.
Description Text `json:"description,omitempty"`
// A map between a variable name and its value. The value is used for
// substitution in the server's URL template.
Variables *ServerVariableMap `json:"variables,omitempty"`
}
func (s *Server) Nodes() []Node {
if s == nil {
return nil
}
return downcastNodes(s.nodes())
}
func (s *Server) nodes() []node {
if s == nil {
return nil
}
return appendEdges(nil, s.Variables)
}
func (s *Server) Refs() []Ref {
if s == nil {
return nil
}
var refs []Ref
if s.Variables != nil {
refs = append(refs, s.Variables.Refs()...)
}
return refs
}
// func (s *Server) ResolveNodeByPointer(ptr jsonpointer.Pointer) (Node, error) {
// if err := ptr.Validate(); err != nil {
// return nil, err
// }
// return s.resolveNodeByPointer(ptr)
// }
// func (s *Server) resolveNodeByPointer(ptr jsonpointer.Pointer) (Node, error) {
// if ptr.IsRoot() {
// return s, nil
// }
// nxt, tok, _ := ptr.Next()
// switch tok {
// case "variables":
// if s.Variables == nil {
// return nil, newErrNotFound(s.AbsoluteLocation(), tok)
// }
// return s.Variables.resolveNodeByPointer(nxt)
// default:
// return nil, newErrNotResolvable(s.Location.AbsoluteLocation(), tok)
// }
// }
func (*Server) Kind() Kind { return KindServer }
func (*Server) mapKind() Kind { return KindUndefined }
func (*Server) sliceKind() Kind { return KindServerSlice }
func (*Server) Anchors() (*Anchors, error) { return nil, nil }
func (s *Server) setLocation(loc Location) error {
if s == nil {
return nil
}
s.Location = loc
return s.Variables.setLocation(loc.AppendLocation("variables"))
}
// MarshalJSON marshals JSON
func (s Server) MarshalJSON() ([]byte, error) {
type server Server
return marshalExtendedJSON(server(s))
}
// UnmarshalJSON unmarshals JSON
func (s *Server) UnmarshalJSON(data []byte) error {
type server Server
var v server
err := unmarshalExtendedJSON(data, &v)
*s = Server(v)
return err
}
// UnmarshalYAML satisfies gopkg.in/yaml.v3 Marshaler interface
func (s Server) MarshalYAML() (interface{}, error) {
j, err := s.MarshalJSON()
if err != nil {
return nil, err
}
var v interface{}
err = json.Unmarshal(j, &v)
if err != nil {
return nil, err
}
return v, nil
}
// UnmarshalYAML satisfies gopkg.in/yaml.v3 Unmarshaler interface
func (s *Server) UnmarshalYAML(value *yaml.Node) error {
v, err := yaml.Marshal(value)
if err != nil {
return err
}
j, err := transcode.JSONFromYAML(v)
if err != nil {
return err
}
return json.Unmarshal(j, s)
}
func (s *Server) isNil() bool { return s == nil }
var _ node = (*Server)(nil)