Documentation
¶
Overview ¶
Package configtype provides a flexible and type-safe way to load and manage configuration from various sources. It supports multiple configuration formats including JSON, YAML, and TOML, with built-in environment variable expansion support.
The package implements the encoding.TextUnmarshaler interface to allow configuration loading from environment variables, making it easy to integrate with various configuration management systems.
Key features:
- Support for multiple configuration formats (JSON, YAML, TOML)
- Environment variable expansion in both file paths and configuration content
- Generic type support for type-safe configuration loading
- Base64 encoding support for sensitive data
- Hot reloading capability for configuration files
Example usage:
type DBConfig struct { Host string `json:"host"` Port int `json:"port"` Database string `json:"database"` } type AppConfig struct { DB configtype.JSONFile[DBConfig] `env:"DB_CONFIG"` Secret configtype.Base64 `env:"API_SECRET"` } // Set environment variables // export DB_CONFIG=/path/to/db_config.json // export API_SECRET=dGVzdC1zZWNyZXQ= // Load configuration var config AppConfig if err := env.Parse(&config); err != nil { log.Fatal(err) } // Access configuration fmt.Printf("Database: %s:%d/%s\n", config.DB.Data.Host, config.DB.Data.Port, config.DB.Data.Database) fmt.Printf("Secret: %s\n", config.Secret)
The package provides the following types:
- JSONFile[T]: For loading JSON configuration files
- YAMLFile[T]: For loading YAML configuration files
- TOMLFile[T]: For loading TOML configuration files
- Base64: For handling base64-encoded configuration values
Each file-based configuration type supports:
- Environment variable expansion in file paths
- Environment variable expansion in configuration content
- Hot reloading via the Reload() method
- Type-safe configuration loading through generics
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Base64 ¶
type Base64 string
Base64 represents a base64-encoded string value. It implements encoding.TextUnmarshaler to allow loading from environment variables.
Example usage:
type AppConfig struct { Secret configtype.Base64 `env:"SECRET_CONFIG"` } // Set environment variable with base64-encoded string // export SECRET_CONFIG=dGVzdC1zZWNyZXQ= // The base64-encoded string contains: "test-secret" // Load configuration var config AppConfig if err := env.Parse(&config); err != nil { log.Fatal(err) } // Access configuration fmt.Printf("Secret: %s\n", config.Secret)
func (*Base64) Reload ¶
Reload reloads the base64 configuration. This is useful when the configuration has been updated and you want to load the new values.
func (*Base64) UnmarshalText ¶
UnmarshalText implements the encoding.TextUnmarshaler interface. It decodes the base64-encoded text into a string.
type JSONFile ¶
type JSONFile[T any] struct { // FilePath is the path to the JSON configuration file FilePath string // Data contains the parsed configuration data Data T }
JSONFile represents a configuration file in JSON format. It implements encoding.TextUnmarshaler to allow loading from environment variables. The generic type T specifies the type of the configuration data.
Example usage:
type DBConfig struct { Host string `json:"host"` Port int `json:"port"` Database string `json:"database"` } type AppConfig struct { DB configtype.JSONFile[DBConfig] `env:"DB_CONFIG"` } // Set environment variable to point to JSON config file // export DB_CONFIG=/path/to/db_config.json // The JSON file at /path/to/db_config.json should contain: // { // "host": "localhost", // "port": 5432, // "database": "mydb" // } // Load configuration var config AppConfig if err := env.Parse(&config); err != nil { log.Fatal(err) } // Access configuration fmt.Printf("Database: %s:%d/%s\n", config.DB.Data.Host, config.DB.Data.Port, config.DB.Data.Database)
func (*JSONFile[T]) Reload ¶
Reload reloads the JSON configuration file. It is useful when the configuration file has been modified and needs to be reloaded. If no file path is set, it returns nil without doing anything.
func (*JSONFile[T]) UnmarshalText ¶
UnmarshalText implements the encoding.TextUnmarshaler interface. It reads the JSON file path from the provided text and loads the configuration. The file path can contain environment variables that will be expanded.
type TOMLFile ¶
type TOMLFile[T any] struct { // FilePath is the path to the TOML configuration file FilePath string // Data contains the parsed configuration data Data T }
TOMLFile represents a configuration file in TOML format. It implements encoding.TextUnmarshaler to allow loading from environment variables. The generic type T specifies the type of the configuration data.
Example usage:
type DBConfig struct { Host string `toml:"host"` Port int `toml:"port"` Database string `toml:"database"` } type AppConfig struct { DB configtype.TOMLFile[DBConfig] `env:"DB_CONFIG"` } // Set environment variable to point to TOML config file // export DB_CONFIG=/path/to/db_config.toml // export DB_HOST=localhost // export DB_PORT=5432 // The TOML file at /path/to/db_config.toml can contain environment variables: // host = "$DB_HOST" // port = $DB_PORT // database = "mydb" // Load configuration var config AppConfig if err := env.Parse(&config); err != nil { log.Fatal(err) } // Access configuration fmt.Printf("Database: %s:%d/%s\n", config.DB.Data.Host, config.DB.Data.Port, config.DB.Data.Database)
func (*TOMLFile[T]) Reload ¶
Reload reloads the TOML configuration file. This is useful when the configuration file has been updated and you want to load the new values.
func (*TOMLFile[T]) UnmarshalText ¶
UnmarshalText implements the encoding.TextUnmarshaler interface. It reads the TOML file path from the provided text and loads the configuration. The file path can contain environment variables that will be expanded.
type YAMLFile ¶
type YAMLFile[T any] struct { // FilePath is the path to the YAML configuration file FilePath string // Data contains the parsed configuration data Data T }
YAMLFile represents a configuration file in YAML format. It implements encoding.TextUnmarshaler to allow loading from environment variables. The generic type T specifies the type of the configuration data.
Example usage:
type DBConfig struct { Host string `yaml:"host"` Port int `yaml:"port"` Database string `yaml:"database"` } type AppConfig struct { DB configtype.YAMLFile[DBConfig] `env:"DB_CONFIG"` } // Set environment variable to point to YAML config file // export DB_CONFIG=/path/to/db_config.yaml // export DB_HOST=localhost // export DB_PORT=5432 // The YAML file at /path/to/db_config.yaml can contain environment variables: // host: $DB_HOST // port: $DB_PORT // database: mydb // Load configuration var config AppConfig if err := env.Parse(&config); err != nil { log.Fatal(err) } // Access configuration fmt.Printf("Database: %s:%d/%s\n", config.DB.Data.Host, config.DB.Data.Port, config.DB.Data.Database)
func (*YAMLFile[T]) Reload ¶
Reload reloads the YAML configuration file. This is useful when the configuration file has been updated and you want to load the new values.
func (*YAMLFile[T]) UnmarshalText ¶
UnmarshalText implements the encoding.TextUnmarshaler interface. It reads the YAML file path from the provided text and loads the configuration. The file path can contain environment variables that will be expanded.