configutil

package
v0.2.17 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 26, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Normalize

func Normalize[T Identifiable](activeName string, defaultItem T, configs []T) (T, []T, error)

Normalize applies a set of heuristic rules to a configuration structure to ensure consistency and provide sensible defaults. It is a generic function that operates on any configuration slice whose elements implement the Identifiable interface.

The function does not modify the input slices directly but returns the normalized default item and a new list of configuration items.

Normalization Logic:

  1. Builds a map of all provided configurations for efficient lookup.
  2. If a `defaultItem` is provided but is not present in the `configs` slice (by name), it is added.
  3. If the `configs` slice is empty after the above step, an error is returned.
  4. Determines the definitive default item based on the following priority: a. The item specified by `activeName`, if provided and exists. (Highest Priority) b. The initially provided `defaultItem`, if provided. c. The first item in the `configs` slice. (Fallback)
  5. Ensures that the returned default item is always the instance from the `configs` slice (object identity consistency).

Type Parameters:

  • T: The type of the configuration item, which must implement the Identifiable interface. T must be comparable (pointers satisfy this).

Parameters:

  • activeName: The name of the configuration item that should be active. Can be an empty string.
  • defaultItem: The default configuration item. Can be nil.
  • configs: A slice of configuration items.

Returns:

  • The normalized default configuration item.
  • The normalized slice of configuration items.
  • An error if the configuration is invalid (e.g., no configs available) or inconsistent (e.g., activeName not found).

Types

type Identifiable

type Identifiable interface {
	comparable
	GetName() string
}

Identifiable represents a configuration item that can be uniquely identified by a name. This interface is a constraint for the generic Normalize function. We include comparable to allow checking if an item is the zero value (nil for pointers).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL