forked from loafle/openapi-generator-original
[Go] Add multiple servers support to Go(-experimental) client (#4635)
* [Go] Add multiple servers support to Go-experimental client * [Go] Use same configuration in go and go-experimental * [Go] Use Replace -1 instead of ReplaceAll * [Go] Fix typo and add bound check * [Go] Regenerate missing templates
This commit is contained in:
committed by
William Cheng
parent
5453edf1b6
commit
dce392336d
@@ -10,7 +10,9 @@
|
||||
package petstore
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// contextKeys are used to identify the type of value in the context.
|
||||
@@ -49,6 +51,20 @@ type APIKey struct {
|
||||
Prefix string
|
||||
}
|
||||
|
||||
// ServerVariable stores the information about a server variable
|
||||
type ServerVariable struct {
|
||||
Description string
|
||||
DefaultValue string
|
||||
EnumValues []string
|
||||
}
|
||||
|
||||
// ServerConfiguration stores the information about a server
|
||||
type ServerConfiguration struct {
|
||||
Url string
|
||||
Description string
|
||||
Variables map[string]ServerVariable
|
||||
}
|
||||
|
||||
// Configuration stores the configuration of the API client
|
||||
type Configuration struct {
|
||||
BasePath string `json:"basePath,omitempty"`
|
||||
@@ -57,6 +73,7 @@ type Configuration struct {
|
||||
DefaultHeader map[string]string `json:"defaultHeader,omitempty"`
|
||||
UserAgent string `json:"userAgent,omitempty"`
|
||||
Debug bool `json:"debug,omitempty"`
|
||||
Servers []ServerConfiguration
|
||||
HTTPClient *http.Client
|
||||
}
|
||||
|
||||
@@ -67,6 +84,11 @@ func NewConfiguration() *Configuration {
|
||||
DefaultHeader: make(map[string]string),
|
||||
UserAgent: "OpenAPI-Generator/1.0.0/go",
|
||||
Debug: false,
|
||||
Servers: []ServerConfiguration{{
|
||||
Url: "http://petstore.swagger.io:80/v2",
|
||||
Description: "No description provided",
|
||||
},
|
||||
},
|
||||
}
|
||||
return cfg
|
||||
}
|
||||
@@ -75,3 +97,31 @@ func NewConfiguration() *Configuration {
|
||||
func (c *Configuration) AddDefaultHeader(key string, value string) {
|
||||
c.DefaultHeader[key] = value
|
||||
}
|
||||
|
||||
// ServerUrl returns URL based on server settings
|
||||
func (c *Configuration) ServerUrl(index int, variables map[string]string) (string, error) {
|
||||
if index < 0 || len(c.Servers) <= index {
|
||||
return "", fmt.Errorf("Index %v out of range %v", index, len(c.Servers) - 1)
|
||||
}
|
||||
server := c.Servers[index]
|
||||
url := server.Url
|
||||
|
||||
// go through variables and replace placeholders
|
||||
for name, variable := range server.Variables {
|
||||
if value, ok := variables[name]; ok {
|
||||
found := bool(len(variable.EnumValues) == 0)
|
||||
for _, enumValue := range variable.EnumValues {
|
||||
if value == enumValue {
|
||||
found = true
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
return "", fmt.Errorf("The variable %s in the server URL has invalid value %v. Must be %v", name, value, variable.EnumValues)
|
||||
}
|
||||
url = strings.Replace(url, "{"+name+"}", value, -1)
|
||||
} else {
|
||||
url = strings.Replace(url, "{"+name+"}", variable.DefaultValue, -1)
|
||||
}
|
||||
}
|
||||
return url, nil
|
||||
}
|
||||
|
||||
@@ -11,7 +11,9 @@
|
||||
package petstore
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// contextKeys are used to identify the type of value in the context.
|
||||
@@ -50,6 +52,20 @@ type APIKey struct {
|
||||
Prefix string
|
||||
}
|
||||
|
||||
// ServerVariable stores the information about a server variable
|
||||
type ServerVariable struct {
|
||||
Description string
|
||||
DefaultValue string
|
||||
EnumValues []string
|
||||
}
|
||||
|
||||
// ServerConfiguration stores the information about a server
|
||||
type ServerConfiguration struct {
|
||||
Url string
|
||||
Description string
|
||||
Variables map[string]ServerVariable
|
||||
}
|
||||
|
||||
// Configuration stores the configuration of the API client
|
||||
type Configuration struct {
|
||||
BasePath string `json:"basePath,omitempty"`
|
||||
@@ -58,6 +74,7 @@ type Configuration struct {
|
||||
DefaultHeader map[string]string `json:"defaultHeader,omitempty"`
|
||||
UserAgent string `json:"userAgent,omitempty"`
|
||||
Debug bool `json:"debug,omitempty"`
|
||||
Servers []ServerConfiguration
|
||||
HTTPClient *http.Client
|
||||
}
|
||||
|
||||
@@ -68,6 +85,11 @@ func NewConfiguration() *Configuration {
|
||||
DefaultHeader: make(map[string]string),
|
||||
UserAgent: "OpenAPI-Generator/1.0.0/go",
|
||||
Debug: false,
|
||||
Servers: []ServerConfiguration{{
|
||||
Url: "http://petstore.swagger.io:80/v2",
|
||||
Description: "No description provided",
|
||||
},
|
||||
},
|
||||
}
|
||||
return cfg
|
||||
}
|
||||
@@ -76,3 +98,31 @@ func NewConfiguration() *Configuration {
|
||||
func (c *Configuration) AddDefaultHeader(key string, value string) {
|
||||
c.DefaultHeader[key] = value
|
||||
}
|
||||
|
||||
// ServerUrl returns URL based on server settings
|
||||
func (c *Configuration) ServerUrl(index int, variables map[string]string) (string, error) {
|
||||
if index < 0 || len(c.Servers) <= index {
|
||||
return "", fmt.Errorf("Index %v out of range %v", index, len(c.Servers) - 1)
|
||||
}
|
||||
server := c.Servers[index]
|
||||
url := server.Url
|
||||
|
||||
// go through variables and replace placeholders
|
||||
for name, variable := range server.Variables {
|
||||
if value, ok := variables[name]; ok {
|
||||
found := bool(len(variable.EnumValues) == 0)
|
||||
for _, enumValue := range variable.EnumValues {
|
||||
if value == enumValue {
|
||||
found = true
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
return "", fmt.Errorf("The variable %s in the server URL has invalid value %v. Must be %v", name, value, variable.EnumValues)
|
||||
}
|
||||
url = strings.Replace(url, "{"+name+"}", value, -1)
|
||||
} else {
|
||||
url = strings.Replace(url, "{"+name+"}", variable.DefaultValue, -1)
|
||||
}
|
||||
}
|
||||
return url, nil
|
||||
}
|
||||
|
||||
@@ -10,7 +10,9 @@
|
||||
package petstore
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// contextKeys are used to identify the type of value in the context.
|
||||
@@ -49,6 +51,20 @@ type APIKey struct {
|
||||
Prefix string
|
||||
}
|
||||
|
||||
// ServerVariable stores the information about a server variable
|
||||
type ServerVariable struct {
|
||||
Description string
|
||||
DefaultValue string
|
||||
EnumValues []string
|
||||
}
|
||||
|
||||
// ServerConfiguration stores the information about a server
|
||||
type ServerConfiguration struct {
|
||||
Url string
|
||||
Description string
|
||||
Variables map[string]ServerVariable
|
||||
}
|
||||
|
||||
// Configuration stores the configuration of the API client
|
||||
type Configuration struct {
|
||||
BasePath string `json:"basePath,omitempty"`
|
||||
@@ -57,6 +73,7 @@ type Configuration struct {
|
||||
DefaultHeader map[string]string `json:"defaultHeader,omitempty"`
|
||||
UserAgent string `json:"userAgent,omitempty"`
|
||||
Debug bool `json:"debug,omitempty"`
|
||||
Servers []ServerConfiguration
|
||||
HTTPClient *http.Client
|
||||
}
|
||||
|
||||
@@ -67,6 +84,11 @@ func NewConfiguration() *Configuration {
|
||||
DefaultHeader: make(map[string]string),
|
||||
UserAgent: "OpenAPI-Generator/1.0.0/go",
|
||||
Debug: false,
|
||||
Servers: []ServerConfiguration{{
|
||||
Url: "http://petstore.swagger.io:80/v2",
|
||||
Description: "No description provided",
|
||||
},
|
||||
},
|
||||
}
|
||||
return cfg
|
||||
}
|
||||
@@ -75,3 +97,31 @@ func NewConfiguration() *Configuration {
|
||||
func (c *Configuration) AddDefaultHeader(key string, value string) {
|
||||
c.DefaultHeader[key] = value
|
||||
}
|
||||
|
||||
// ServerUrl returns URL based on server settings
|
||||
func (c *Configuration) ServerUrl(index int, variables map[string]string) (string, error) {
|
||||
if index < 0 || len(c.Servers) <= index {
|
||||
return "", fmt.Errorf("Index %v out of range %v", index, len(c.Servers) - 1)
|
||||
}
|
||||
server := c.Servers[index]
|
||||
url := server.Url
|
||||
|
||||
// go through variables and replace placeholders
|
||||
for name, variable := range server.Variables {
|
||||
if value, ok := variables[name]; ok {
|
||||
found := bool(len(variable.EnumValues) == 0)
|
||||
for _, enumValue := range variable.EnumValues {
|
||||
if value == enumValue {
|
||||
found = true
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
return "", fmt.Errorf("The variable %s in the server URL has invalid value %v. Must be %v", name, value, variable.EnumValues)
|
||||
}
|
||||
url = strings.Replace(url, "{"+name+"}", value, -1)
|
||||
} else {
|
||||
url = strings.Replace(url, "{"+name+"}", variable.DefaultValue, -1)
|
||||
}
|
||||
}
|
||||
return url, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user