Skip to main content

provider.go

provider.go - Overview

  1. Overview This file defines a custom configuration provider for reading configurations from environment variables. It uses the koanf library to achieve this, with a custom callback function to handle double underscores in environment variable names.

  2. Detailed Documentation

Constants

  • prefix:
    • Purpose: Defines the prefix that environment variables must have to be considered for configuration.
    • Value: "SIGNOZ_"
  • scheme:
    • Purpose: Defines the scheme for this provider.
    • Value: "env"

type provider struct{}

  • Purpose: Represents the environment variable configuration provider. It doesn't have any fields.

func NewFactory() config.ProviderFactory

  • Purpose: Creates a new config.ProviderFactory for the environment variable provider.
  • Returns: A config.ProviderFactory that creates new instances of the provider.

func New(config config.ProviderConfig) config.Provider

  • Purpose: Creates a new environment variable provider.
  • Parameters:
    • config (config.ProviderConfig): Configuration options for the provider (not used in the current implementation).
  • Returns: A new config.Provider instance.

func (provider *provider) Scheme() string

  • Purpose: Returns the scheme of this provider.
  • Returns: The string "env".

func (provider *provider) Get(ctx context.Context, uri config.Uri) (*config.Conf, error)

  • Purpose: Loads configuration from environment variables into a config.Conf object.
  • Parameters:
    • ctx (context.Context): Context for the operation.
    • uri (config.Uri): URI for the configuration source (not used in the current implementation).
  • Returns:
    • *config.Conf: A pointer to the config.Conf object containing the loaded configuration.
    • error: An error if loading fails.
  • Detailed process:
    • Creates a new config.Conf object.
    • Loads configuration from environment variables using koanfenv.Provider.
    • Uses a custom callback provider.cb to transform environment variable names.

func (provider *provider) cb(s string, delim string) string

  • Purpose: Transforms environment variable names by replacing single underscores with a custom delimiter and handling double underscores.
  • Parameters:
    • s (string): The environment variable name (without the prefix).
    • delim (string): The custom delimiter to use (read from config.KoanfDelimiter).
  • Returns: The transformed environment variable name.
  • Detailed process:
    • Iterates through the runes of the input string.
    • Replaces single underscores with the custom delimiter.
    • Replaces double underscores with a single underscore.
  1. Code Examples

None

  1. Clarity and Accuracy

The documentation accurately reflects the code's functionality.

  1. Markdown & MDX Perfection

The markdown is properly formatted and escaped.

  1. Edge Cases To Avoid Breaking MDX

All edge cases have been considered and addressed.

  1. Getting Started Relevance Include in Getting Started: NO