buildHostData builds the host data for the given host expression.
(host *expr.HostExpr)
| 265 | |
| 266 | // buildHostData builds the host data for the given host expression. |
| 267 | func buildHostData(host *expr.HostExpr) *HostData { |
| 268 | uris := make([]*URIData, len(host.URIs)) |
| 269 | for i, uv := range host.URIs { |
| 270 | var ( |
| 271 | t *TransportData |
| 272 | scheme string |
| 273 | port string |
| 274 | |
| 275 | ustr = string(uv) |
| 276 | ) |
| 277 | // Did not use url package to find scheme because the url may |
| 278 | // contain params (i.e. http://{version}.example.com) which needs |
| 279 | // substition for url.Parse to succeed. Also URIs in host must have |
| 280 | // a scheme otherwise validations would have failed. |
| 281 | switch { |
| 282 | case strings.HasPrefix(ustr, "https"): |
| 283 | scheme = "https" |
| 284 | port = "443" |
| 285 | t = newHTTPTransport() |
| 286 | case strings.HasPrefix(ustr, "http"): |
| 287 | scheme = "http" |
| 288 | port = "80" |
| 289 | t = newHTTPTransport() |
| 290 | case strings.HasPrefix(ustr, "grpcs"): |
| 291 | scheme = "grpcs" |
| 292 | port = "8443" |
| 293 | t = newGRPCTransport() |
| 294 | case strings.HasPrefix(ustr, "grpc"): |
| 295 | scheme = "grpc" |
| 296 | port = "8080" |
| 297 | t = newGRPCTransport() |
| 298 | |
| 299 | // No need for default case here because we only support the above |
| 300 | // possibilites for the scheme. Invalid scheme would have failed |
| 301 | // validations in the first place. |
| 302 | } |
| 303 | uris[i] = &URIData{ |
| 304 | Scheme: scheme, |
| 305 | URL: ustr, |
| 306 | Port: port, |
| 307 | Transport: t, |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | vars := expr.AsObject(host.Variables.Type) |
| 312 | var variables []*VariableData |
| 313 | if len(*vars) > 0 { |
| 314 | variables = make([]*VariableData, len(*vars)) |
| 315 | for i, v := range *vars { |
| 316 | def := v.Attribute.DefaultValue |
| 317 | var values []string |
| 318 | if def == nil { |
| 319 | def = v.Attribute.Validation.Values[0] |
| 320 | // DSL ensures v.Attribute has either a |
| 321 | // default value or an enum validation |
| 322 | values = convertToString(v.Attribute.Validation.Values...) |
| 323 | } |
| 324 | variables[i] = &VariableData{ |
no test coverage detected