SortParamsByPath reorders the given parameter definitions to match those in the path URI. If a parameter appears more than once in the path (e.g. Keycloak's clients/{client-uuid}/roles/{role-name}/composites/clients/{client-uuid}), duplicates are removed and only the first occurrence determines the
(path string, in []ParameterDefinition)
| 674 | // /clients/{client-uuid}/roles/{role-name}/composites/clients/{client-uuid}), |
| 675 | // duplicates are removed and only the first occurrence determines the order. |
| 676 | func SortParamsByPath(path string, in []ParameterDefinition) ([]ParameterDefinition, error) { |
| 677 | pathParams := OrderedParamsFromUri(path) |
| 678 | |
| 679 | // Deduplicate, preserving first-occurrence order. |
| 680 | seen := make(map[string]struct{}, len(pathParams)) |
| 681 | uniqueParams := make([]string, 0, len(pathParams)) |
| 682 | for _, name := range pathParams { |
| 683 | if _, exists := seen[name]; !exists { |
| 684 | seen[name] = struct{}{} |
| 685 | uniqueParams = append(uniqueParams, name) |
| 686 | } |
| 687 | } |
| 688 | |
| 689 | n := len(in) |
| 690 | if len(uniqueParams) != n { |
| 691 | return nil, fmt.Errorf("path '%s' has %d positional parameters, but spec has %d declared", |
| 692 | path, len(uniqueParams), n) |
| 693 | } |
| 694 | out := make([]ParameterDefinition, n) |
| 695 | for i, name := range uniqueParams { |
| 696 | p := ParameterDefinitions(in).FindByName(name) |
| 697 | if p == nil { |
| 698 | return nil, fmt.Errorf("path '%s' refers to parameter '%s', which doesn't exist in specification", |
| 699 | path, name) |
| 700 | } |
| 701 | out[i] = *p |
| 702 | } |
| 703 | return out, nil |
| 704 | } |
| 705 | |
| 706 | // IsGoKeyword returns whether the given string is a go keyword |
| 707 | func IsGoKeyword(str string) bool { |