# gookit/config architecture

> Architecture diagram of the GitHub repository gookit/config, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-02-16). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/gookit/config
- Repository: https://github.com/gookit/config

## Overview

##### Project Type and Purpose
- This is not a full-stack application or a standalone tool—it’s a reusable library that enables configuration management in Go applications.
- Its purpose is to provide developers with a unified API for loading configuration data from multiple sources (files, ENV, flags, remote URLs) and formats. It also includes functionality for merging configurations and triggering events on configuration changes.

##### File Structure
- At the top level, notice directories and files that indicate infrastructure:
- Several .github configuration files and workflows (for CI: lint, release, tests) that indicate the project’s automated build and release process.
- The _examples directory shows quick usage examples for various formats (INI, JSON, TOML, YAML, etc.) and even file-watching.
- The testdata directory contains configuration samples in many formats.
- There are directories named after the supported config formats: ini, json, json5, toml, yaml, yamlv3, hcl, hclv2, etc.
- Core files in the root (e.g., config.go, driver.go, export.go, load.go, read.go, write.go) form the backbone of the configuration engine and API.

##### README for Additional Architectural Insights
- The README explains the usage of the API with examples – how to add drivers, load and merge configuration files, bind configuration data to structs, load from flags/ENV, and handle events.
- It outlines the design goals: multi-format support, extensibility (by adding custom drivers), and flexible configuration sources.
- The examples show how the various components are interconnected (for instance, how options are set and hook functions are registered).

a. Core Configuration Engine:
- Modules: Core API (public functions like LoadFiles, LoadFlags, BindStruct/Decode, Set/Get) and configuration options.
- Responsibilities: Load/merge configuration data; manage read/write operations; apply hooks on configuration changes.
b. Driver Modules:
- Separate packages for each format (json, json5, ini, toml, yaml, yamlv3, hcl, hclv2, properties).
- Each driver encapsulates parsing logic and format-specific features.
c. Data Loading & Merging:
- Components that handle reading from different sources: files, ENV, command-line flags, remote URLs.
- Data merge logic to combine multiple configuration files automatically by key.
d. Event & Hook System:
- A component that fires events (e.g., load.data, set.value, reload.data) during configuration changes.
- A mechanism for registering hook functions to respond to these events.
e. Utility/Helper Modules:
- Files such as options.go, util.go, write.go providing support functions.
- Test data and examples that illustrate how the APIs are used in practice (guiding developers on integration).

- How API calls then delegate tasks to the Core Engine, which in turn interacts with different Driver Modules.
- That configuration data flows from source inputs (files, ENV, flags, remote) into the Data Loading & Merging module.
- That there’s also a Binding component that takes merged configuration data and maps it into Go structures.

##### Architectural Patterns and Design Principles
- Modularity: Each supported file format is encapsulated in its own package, making it easy to add or remove drivers as needed.
- Extensibility: With options like AddDriver(), custom configuration drivers can be plugged in.
- Separation of Concerns: Clear division between the configuration core, the parsing (driver) logic, loading/merging strategies, and event handling.
- Event-Driven: Hook functions enable reactive configuration changes.
- Generic API: Provides type-safe accessor methods for configuration values.

## Diagram (Mermaid)

```mermaid
flowchart TD
    examplesNode["Examples (Usage)"]:::examples

    subgraph "Core Configuration Engine"
        config["config.go"]:::core
        driver["driver.go"]:::core
        export["export.go"]:::core
        options["options.go"]:::core
    end

    subgraph "Data Loading & Merging Module"
        load["load.go"]:::loader
        read["read.go"]:::loader
        write["write.go"]:::loader
    end

    subgraph "Driver Modules"
        json["JSON (json.go)"]:::driver
        json5["JSON5 (json5.go)"]:::driver
        ini["INI (ini.go)"]:::driver
        toml["TOML (toml.go)"]:::driver
        yaml["YAML (yaml.go)"]:::driver
        yamlv3["YAML v3 (yamlv3.go)"]:::driver
        hcl["HCL (hcl.go)"]:::driver
        hclv2["HCL v2 (hcl_v2.go)"]:::driver
        properties["Properties (properties.go)"]:::driver
    end

    utilityNode["Utility (util.go)"]:::utility
    eventNode["Event/Hook System"]:::event

    %% Relationships
    examplesNode -->|"calls"| config
    config -->|"triggers"| load
    load -->|"loads"| json
    load -->|"loads"| json5
    load -->|"loads"| ini
    load -->|"loads"| toml
    load -->|"loads"| yaml
    load -->|"loads"| yamlv3
    load -->|"loads"| hcl
    load -->|"loads"| hclv2
    load -->|"loads"| properties
    json -->|"parses"| config
    config -->|"notifies"| eventNode
    eventNode -->|"updates"| config
    config -->|"uses"| utilityNode

    %% Styling definitions
    classDef client fill:#ADD8E6,stroke:#000,stroke-width:2px;
    classDef core fill:#E6E6FA,stroke:#000,stroke-width:2px;
    classDef loader fill:#FFDAB9,stroke:#000,stroke-width:2px;
    classDef driver fill:#FFFFCC,stroke:#000,stroke-width:2px;
    classDef utility fill:#90EE90,stroke:#000,stroke-width:2px;
    classDef event fill:#FFC0CB,stroke:#000,stroke-width:2px;
    classDef examples fill:#F0E68C,stroke:#000,stroke-width:2px;

    class examplesNode examples;

    %% Click Events
    click config "https://github.com/gookit/config/blob/master/config.go"
    click driver "https://github.com/gookit/config/blob/master/driver.go"
    click export "https://github.com/gookit/config/blob/master/export.go"
    click options "https://github.com/gookit/config/blob/master/options.go"
    click load "https://github.com/gookit/config/blob/master/load.go"
    click read "https://github.com/gookit/config/blob/master/read.go"
    click write "https://github.com/gookit/config/blob/master/write.go"
    click json "https://github.com/gookit/config/blob/master/json/json.go"
    click json5 "https://github.com/gookit/config/blob/master/json5/json5.go"
    click ini "https://github.com/gookit/config/blob/master/ini/ini.go"
    click toml "https://github.com/gookit/config/blob/master/toml/toml.go"
    click yaml "https://github.com/gookit/config/blob/master/yaml/yaml.go"
    click yamlv3 "https://github.com/gookit/config/blob/master/yamlv3/yamlv3.go"
    click hcl "https://github.com/gookit/config/blob/master/hcl/hcl.go"
    click hclv2 "https://github.com/gookit/config/blob/master/hclv2/hcl_v2.go"
    click properties "https://github.com/gookit/config/blob/master/properties/properties.go"
    click utilityNode "https://github.com/gookit/config/blob/master/util.go"
    click examplesNode "https://github.com/gookit/config/tree/master/_examples"
```
