URIString returns a valid URI string by substituting the parameters with their default value if present or the first item in their enum. It returns an error if the given URI expression is not found in the host URIs.
(u URIExpr)
| 231 | // their default value if present or the first item in their enum. It returns |
| 232 | // an error if the given URI expression is not found in the host URIs. |
| 233 | func (h *HostExpr) URIString(u URIExpr) (string, error) { |
| 234 | found := slices.Contains(h.URIs, u) |
| 235 | if !found { |
| 236 | return "", fmt.Errorf("uri %s not found in host", string(u)) |
| 237 | } |
| 238 | uri := string(u) |
| 239 | // Substitute URI parameters with the corresponding variables defined in |
| 240 | // the host expression. Validations would have made sure that every |
| 241 | // URI parameter have a corresponding variable. |
| 242 | for _, p := range u.Params() { |
| 243 | for _, v := range *AsObject(h.Variables.Type) { |
| 244 | if p == v.Name { |
| 245 | def := v.Attribute.DefaultValue |
| 246 | if def == nil { |
| 247 | def = v.Attribute.Validation.Values[0] |
| 248 | } |
| 249 | uri = strings.ReplaceAll(uri, fmt.Sprintf("{%s}", p), fmt.Sprintf("%v", def)) |
| 250 | } |
| 251 | } |
| 252 | } |
| 253 | return uri, nil |
| 254 | } |
| 255 | |
| 256 | // Params return the names of the parameters used in URI if any. |
| 257 | func (u URIExpr) Params() []string { |
no test coverage detected