MCPcopy Index your code
hub / github.com/oapi-codegen/oapi-codegen / SortParamsByPath

Function SortParamsByPath

pkg/codegen/utils.go:676–704  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

674// /clients/{client-uuid}/roles/{role-name}/composites/clients/{client-uuid}),
675// duplicates are removed and only the first occurrence determines the order.
676func 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
707func IsGoKeyword(str string) bool {

Callers 2

TestSortParamsByPathFunction · 0.85
OperationDefinitionsFunction · 0.85

Calls 3

OrderedParamsFromUriFunction · 0.85
ParameterDefinitionsTypeAlias · 0.85
FindByNameMethod · 0.80

Tested by 1

TestSortParamsByPathFunction · 0.68