-
Notifications
You must be signed in to change notification settings - Fork 70
/
oas_servers_gen.go
174 lines (155 loc) · 3.84 KB
/
oas_servers_gen.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// Code generated by ogen, DO NOT EDIT.
package api
import (
"fmt"
"net/url"
"github.com/go-faster/errors"
)
// ProductionServer is a server URL template.
//
// Production server.
type ProductionServer struct {
Region string `json:"region" yaml:"region"`
Val string `json:"val" yaml:"val"`
}
// MustPath returns the computed path. It panics if any error occurs.
func (s ProductionServer) MustPath() string {
return errors.Must(s.Path())
}
// Path returns the computed path.
func (s ProductionServer) Path() (string, error) {
raw, err := s.Build()
if err != nil {
return "", err
}
u, err := url.Parse(raw)
if err != nil {
return "", err
}
return u.Path, nil
}
// MustBuild returns the computed server URL. It panics if any error occurs.
func (s ProductionServer) MustBuild() string {
return errors.Must(s.Build())
}
// Build returns the computed server URL.
//
// If variable is empty, it uses the default value.
// If spec defines an enum and given value is not in the enum, it returns an error.
//
// Notice that given values will not be escaped and may cause invalid URL.
func (s ProductionServer) Build() (string, error) {
zeroOr := func(s string, def string) string {
if s == "" {
return def
}
return s
}
s.Region = zeroOr(s.Region, "us")
// Validate "region"
switch s.Region {
case "us":
case "eu":
default:
return "", errors.Errorf("param %q: unexpected value %q", "region", s.Region)
}
s.Val = zeroOr(s.Val, "prod")
// Validate "val"
switch s.Val {
case "prod":
case "test":
default:
return "", errors.Errorf("param %q: unexpected value %q", "val", s.Val)
}
return fmt.Sprintf("https://%s.example.com/%s/v1",
s.Region,
s.Val,
), nil
}
// PrefixServer is a server URL template.
type PrefixServer struct {
Val string `json:"val" yaml:"val"`
}
// MustPath returns the computed path. It panics if any error occurs.
func (s PrefixServer) MustPath() string {
return errors.Must(s.Path())
}
// Path returns the computed path.
func (s PrefixServer) Path() (string, error) {
raw, err := s.Build()
if err != nil {
return "", err
}
u, err := url.Parse(raw)
if err != nil {
return "", err
}
return u.Path, nil
}
// MustBuild returns the computed server URL. It panics if any error occurs.
func (s PrefixServer) MustBuild() string {
return errors.Must(s.Build())
}
// Build returns the computed server URL.
//
// If variable is empty, it uses the default value.
// If spec defines an enum and given value is not in the enum, it returns an error.
//
// Notice that given values will not be escaped and may cause invalid URL.
func (s PrefixServer) Build() (string, error) {
zeroOr := func(s string, def string) string {
if s == "" {
return def
}
return s
}
s.Val = zeroOr(s.Val, "prod")
// Validate "val"
switch s.Val {
case "prod":
case "test":
default:
return "", errors.Errorf("param %q: unexpected value %q", "val", s.Val)
}
return fmt.Sprintf("/%s/v1",
s.Val,
), nil
}
type serverConst string
// MustPath returns the computed path. It panics if any error occurs.
//
// Kept for common interface with server URL templates.
func (s serverConst) MustPath() string {
return errors.Must(s.Path())
}
// Path returns the computed path.
//
// Kept for common interface with server URL templates.
func (s serverConst) Path() (string, error) {
raw, err := s.Build()
if err != nil {
return "", err
}
u, err := url.Parse(raw)
if err != nil {
return "", err
}
return u.Path, nil
}
// MustBuild returns the server URL.
//
// Kept for common interface with server URL templates.
func (s serverConst) MustBuild() string {
return errors.Must(s.Build())
}
// Build returns the server URL.
//
// Kept for common interface with server URL templates.
func (s serverConst) Build() (string, error) {
return string(s), nil
}
const (
// ConstServer is a server URL.
//
ConstServer serverConst = "https://cdn.example.com/v1"
)