Repository: kubernetes/kubernetes
File: staging/src/k8s.io/kubectl/pkg/config/v1beta1/conversion.go (Go)

Code before:
import (
	"fmt"

	conversion "k8s.io/apimachinery/pkg/conversion"
	config "k8s.io/kubectl/pkg/config"
)

func Convert_config_AllowlistEntry_To_v1beta1_AllowlistEntry(in *config.AllowlistEntry, out *AllowlistEntry, s conversion.Scope) error {
	in.Command = out.Command

	return nil
}

// The internal AllowlistEntry type does not have the `Name` field, which is deprecated as of v1.36. Convert `Name` to `Command` where possible, and return an error if both `Name` and `Command` are supplied.
func Convert_v1beta1_AllowlistEntry_To_config_AllowlistEntry(in *AllowlistEntry, out *config.AllowlistEntry, s conversion.Scope) error {
	if len(in.Name) != 0 && len(in.Command) != 0 {
		return fmt.Errorf("both `Name` and `Command` were supplied. `Name` is deprecated, use `Command` instead")
	}

	// when both `Name` and `Command` are empty, propagate the empty value and
	// allow validation to catch it later since it's a validation error, not a
	// conversion error
	cmd := in.Command
	if len(in.Name) != 0 {
		cmd = in.Name
	}

	out.Command = cmd
	return nil
}


Review comment (suggestion):
start by calling the auto-generated conversion, then frame the checks as a switch statement for the three cases we're handling:

```go
if err := autoConvert_v1beta1_AllowlistEntry_To_config_AllowlistEntry(in, out, s); err != nil {
  return err
}

switch {
case len(in.Name) != 0 && len(in.Command) != 0 && in.Name != in.Command:
  return fmt.Errorf("both `Name` and `Command` were supplied with different values. `Name` is deprecated, use `Command` instead")
case len(in.Command) != 0:
  out.Command = in.Command
case len(in.Name) != 0:
  out.Command = in.Name
default:
  // both `Name` and `Command` are empty, propagate the empty value and
	// allow validation to catch it later since it's a validation error, not a
	// conversion error
}
```

Code after:
import (
	"fmt"

	conversion "k8s.io/apimachinery/pkg/conversion"
	config "k8s.io/kubectl/pkg/config"
)

func Convert_config_AllowlistEntry_To_v1beta1_AllowlistEntry(in *config.AllowlistEntry, out *AllowlistEntry, s conversion.Scope) error {
	return autoConvert_config_AllowlistEntry_To_v1beta1_AllowlistEntry(in, out, s)
}

// The internal AllowlistEntry type does not have the `Name` field, which is deprecated as of v1.36. Convert `Name` to `Command` where possible, and return an error if both `Name` and `Command` are supplied.
func Convert_v1beta1_AllowlistEntry_To_config_AllowlistEntry(in *AllowlistEntry, out *config.AllowlistEntry, s conversion.Scope) error {
	if err := autoConvert_v1beta1_AllowlistEntry_To_config_AllowlistEntry(in, out, s); err != nil {
		return err
	}

	switch {
	case len(in.Name) != 0 && len(in.Command) != 0 && in.Name != in.Command:
		return fmt.Errorf("both `Name` and `Command` were supplied with different values. `Name` is deprecated, use `Command` instead")
	case len(in.Command) != 0:
		out.Command = in.Command
	case len(in.Name) != 0:
		out.Command = in.Name
	default:
		// both `Name` and `Command` are empty, propagate the empty value and
		// allow validation to catch it later since it's a validation error, not a
		// conversion error
	}
	return nil
}