Skip to main content

conf.go

conf.go - Overview

  1. Overview This file defines a Conf struct, which is a wrapper around the koanf library, used for managing configuration data. It provides methods for creating, merging, unmarshaling, and setting configuration values. It also includes custom decode hook functions for mapstructure.

  2. Detailed Documentation

const KoanfDelimiter

  • Purpose: Defines the delimiter used by koanf for nested configuration keys.
  • Value: "::"

type Conf

  • Purpose: Represents a configuration object, wrapping a koanf.Koanf instance.
  • Fields:
    • Koanf: A pointer to a koanf.Koanf instance, providing the underlying configuration management functionality.

func NewConf() *Conf

  • Purpose: Creates a new Conf instance with a new koanf.Koanf instance, using the defined KoanfDelimiter.
  • Parameters: None
  • Returns: A pointer to the newly created Conf instance.

func NewConfFromMap(m map[string]any) (*Conf, error)

  • Purpose: Creates a new Conf instance and loads configuration data from a map.
  • Parameters:
    • m (map[string]any): The map containing the configuration data.
  • Returns:
    • A pointer to the new Conf instance if successful, nil otherwise.
    • An error if loading the configuration from the map fails.

func MustNewConfFromMap(m map[string]any) *Conf

  • Purpose: Creates a new Conf instance from a map and panics if an error occurs during creation.
  • Parameters:
    • m (map[string]any): The map containing configuration data.
  • Returns: A pointer to the new Conf instance.
  • Panics: If NewConfFromMap returns an error.

func (conf *Conf) Merge(input *Conf) error

  • Purpose: Merges the configuration from another Conf instance into the current Conf instance.
  • Parameters:
    • input (*Conf): The Conf instance to merge into the current instance.
  • Returns: An error if the merge operation fails.

func (conf *Conf) MergeAt(input *Conf, path string) error

  • Purpose: Merges the configuration from another Conf instance into the current Conf instance at a specified path.
  • Parameters:
    • input (*Conf): The Conf instance to merge.
    • path (string): The path at which to merge the configuration.
  • Returns: An error if the merge operation fails.

func (conf *Conf) Unmarshal(path string, input any, tags ...string) error

  • Purpose: Unmarshals the configuration data at the given path into the provided input variable. It supports custom decode hooks and uses "mapstructure" tags by default.
  • Parameters:
    • path (string): The configuration path to unmarshal from.
    • input (any): The variable to unmarshal the configuration data into.
    • tags (...string): Optional tags to use for unmarshaling (defaults to "mapstructure").
  • Returns: An error if unmarshaling fails.

func (conf *Conf) Set(key string, input any) error

  • Purpose: Sets the configuration value at the given key by decoding the input and merging it into the configuration.
  • Parameters:
    • key (string): The configuration key to set.
    • input (any): The value to set at the given key.
  • Returns: An error if decoding or merging fails.

func StringToURLHookFunc() mapstructure.DecodeHookFunc

  • Purpose: Provides a mapstructure decode hook for converting strings to url.URL instances.
  • Parameters: None
  • Returns: A mapstructure.DecodeHookFunc that attempts to parse strings as URLs.

func YamlV2UnmarshalHookFunc() mapstructure.DecodeHookFunc

  • Purpose: Provides a mapstructure decode hook for unmarshaling strings as YAML using gopkg.in/yaml.v2.
  • Parameters: None
  • Returns: A mapstructure.DecodeHookFunc that attempts to unmarshal strings as YAML.
  1. Code Examples N/A

  2. Clarity and Accuracy The documentation accurately reflects the code.

  3. Markdown & MDX Perfection The markdown is valid.

  4. Edge Cases To Avoid Breaking MDX No issues.

  5. Getting Started Relevance Include in Getting Started: YES