You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: parameters.go
    path of file: ./repos/kubernetes/vendor/k8s.io/kube-openapi/pkg/builder
    
    the code of the file until where you have to start completion: /*
Copyright 2023 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package builder

import (
	"encoding/base64"
	"encoding/json"
	"fmt"
	"hash/fnv"
	"sort"
	"strconv"
	"strings"

	"k8s.io/kube-openapi/pkg/validation/spec"
)

// deduplicateParameters finds parameters that are shared across multiple endpoints and replace them with
// references to the shared parameters in order to avoid repetition.
//
// deduplicateParameters does not mutate the source.
func deduplicateParameters(sp *spec.Swagger) (*spec.Swagger, error) {
	names, parameters, err := collectSharedParameters(sp)
	if err != nil {
		return nil, err
	}

	if sp.Parameters != nil {
		return nil, fmt.Errorf("shared parameters already exist") // should not happen with the builder, but to be sure
	}

	clone := *sp
	clone.Parameters = parameters
	return replaceSharedParameters(names, &clone)
}

// collectSharedParameters finds parameters that show up for many endpoints. The
    
func collectSharedParameters(sp *spec.Swagger) (namesByJSON map[string]string, ret map[string]spec.Parameter, err error) {
	if sp == nil || sp.Paths == nil {
		return nil, nil, nil
	}

	countsByJSON := map[string]int{}
	shared := map[string]spec.Parameter{}
	var keys []string

	collect := func(p *spec.Parameter) error {
		if (p.In == "query" || p.In == "path") && p.Name == "name" {
			return nil // ignore name parameter as they are never shared with the Kind in the description
		}
		if p.In == "query" && p.Name == "fieldValidation" {
			return nil // keep fieldValidation parameter unshared because kubectl uses it (until 1.27) to detect server-side field validation support
		}
		if p.In == "query" && p.Name == "dryRun" {
			return nil // keep fieldValidation parameter unshared because kubectl uses it (until 1.26) to detect dry-run support
		}
		if p.Schema != nil && p.In == "body" && p.Name == "body" && !strings.HasPrefix(p.Schema.Ref.String(), "#/definitions/io.k8s.apimachinery") {
			return nil // ignore non-generic body parameters as they reference the custom schema of the kind
		}

		bs, err := json.Marshal(p)
		if err != nil {
			return err
		}

		k := string(bs)
		countsByJSON[k]++
		if count := countsByJSON[k]; count == 1 {
			shared[k] = *p
			keys = append(keys, k)
		}

		return nil
	}

	for _, path := range sp.Paths.Paths {
		// per operation parameters
		for _, op := range operations(&path) {
			if op == nil {
				continue // shouldn't happen, but ignore if it does; tested through unit test
			}
			for _, p := range op.Parameters {
				if p.Ref.String() != "" {
					// shouldn't happen, but ignore if it does
					continue
				}
				if err := collect(&p); err != nil {
					return nil, nil, err
				}
			}
		}

		// per path parameters
		for _, p := range path.Parameters {
			if p.Ref.String() != "" {
				continue // shouldn't happen, but ignore if it does
			}
			if err := collect(&p); err != nil {
				return nil, nil, err
			}
		}
	}

	// name deterministically
	sort.Strings(keys)
	ret = map[string]spec.Parameter{}
	namesByJSON = map[string]string{}
	for _, k := range keys {
		name := shared[k].Name
		if name == "" {
			// this should never happen as the name is a required field. But if it does, let's be safe.
			name = "param"
		}
		name += "-" + base64Hash(k)
		i := 0
		for {
			if _, ok := ret[name]; !ok {
				ret[name] = shared[k]
				namesByJSON[k] = name
				break
			}
			i++ // only on hash conflict, unlikely with our few variants
			name = shared[k].Name + "-" + strconv.Itoa(i)
		}
	}

	return namesByJSON, ret, nil
}