-
-
Notifications
You must be signed in to change notification settings - Fork 0
Add support for nested configuration #52
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
ac1eeb0
Add support for nested configuration
HEROgold 47295da
Update docs/examples/custom_data_type.md
HEROgold bfab9a4
Fix docs: remove non-existent `Parser`/`set_parser` references (#53)
Copilot 068db57
mark as deprecation instead of private method
HEROgold 040e450
Raise ConfigPathConflictError when trying to path through a scalar.
HEROgold cd50efa
fix ruff issue
HEROgold a4d8c40
ignore line len on deprecation warning
HEROgold f72ea93
ruff fixes
HEROgold 930d272
add missing .env parse detection
HEROgold e0a2255
Separate built-in parsers from msgspec optional dependency; fix Msgsp…
Copilot 67933e6
fix docstr
HEROgold 459fdcf
fix ruff
HEROgold File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,174 @@ | ||
| """Example of nested configuration support in confkit. | ||
|
|
||
| This example demonstrates how to use nested classes to organize | ||
| configuration values hierarchically. This works with: | ||
| - JSON files (native nested objects) | ||
| - YAML files (native nested mappings) | ||
| - TOML files (nested tables) | ||
| - INI files (using dot notation in section names) | ||
| """ | ||
| from confkit.ext.parsers import IniParser | ||
|
|
||
| from pathlib import Path | ||
| from typing import TypeVar | ||
|
|
||
| from confkit import Config | ||
|
|
||
| T = TypeVar("T") | ||
|
|
||
| # Example 1: Nested configuration with JSON | ||
| print("=== Nested JSON Configuration ===") | ||
|
|
||
|
|
||
| class JsonConfig(Config[T]): | ||
| ... | ||
|
|
||
|
|
||
| JsonConfig.set_file(Path("nested_example.json")) | ||
|
|
||
|
|
||
| class DatabaseConfig: | ||
| """Database configuration with nested credentials.""" | ||
|
|
||
| host = JsonConfig("localhost") | ||
| port = JsonConfig(5432) | ||
| name = JsonConfig("myapp_db") | ||
|
|
||
| class Credentials: | ||
| """Nested credentials configuration.""" | ||
|
|
||
| username = JsonConfig("admin") | ||
| password = JsonConfig("secret123") | ||
| use_ssl = JsonConfig(True) | ||
|
|
||
|
|
||
| # Access nested values | ||
| print(f"Database host: {DatabaseConfig.host}") | ||
| print(f"Database port: {DatabaseConfig.port}") | ||
| print(f"Username: {DatabaseConfig.Credentials.username}") | ||
| print(f"Password: {DatabaseConfig.Credentials.password}") | ||
|
||
| print(f"Use SSL: {DatabaseConfig.Credentials.use_ssl}") | ||
|
|
||
| # Example 2: Multiple levels of nesting with YAML | ||
| print("\n=== Multi-Level Nested YAML Configuration ===") | ||
|
|
||
|
|
||
| class YamlConfig(Config[T]): | ||
| ... | ||
|
|
||
|
|
||
| YamlConfig.set_file(Path("nested_example.yaml")) | ||
|
|
||
|
|
||
| class ServerConfig: | ||
| """Server configuration with multiple nesting levels.""" | ||
|
|
||
| name = YamlConfig("web-server-01") | ||
|
|
||
| class Network: | ||
| """Network configuration.""" | ||
|
|
||
| interface = YamlConfig("eth0") | ||
|
|
||
| class IPv4: | ||
| """IPv4 settings.""" | ||
|
|
||
| address = YamlConfig("192.168.1.100") | ||
| netmask = YamlConfig("255.255.255.0") | ||
| gateway = YamlConfig("192.168.1.1") | ||
|
|
||
| class IPv6: | ||
| """IPv6 settings.""" | ||
|
|
||
| address = YamlConfig("fe80::1") | ||
| prefix = YamlConfig(64) | ||
|
|
||
|
|
||
| print(f"Server name: {ServerConfig.name}") | ||
| print(f"Network interface: {ServerConfig.Network.interface}") | ||
| print(f"IPv4 address: {ServerConfig.Network.IPv4.address}") | ||
| print(f"IPv4 netmask: {ServerConfig.Network.IPv4.netmask}") | ||
| print(f"IPv6 address: {ServerConfig.Network.IPv6.address}") | ||
| print(f"IPv6 prefix: {ServerConfig.Network.IPv6.prefix}") | ||
|
|
||
| # Example 3: INI files with dot notation | ||
| print("\n=== Nested INI Configuration (using dot notation) ===") | ||
|
|
||
|
|
||
| class IniConfig(Config[T]): | ||
| ... | ||
|
|
||
|
|
||
| IniConfig._set_parser(IniParser()) | ||
| IniConfig.set_file(Path("nested_example.ini")) | ||
|
|
||
|
|
||
| class AppConfig: | ||
| """Application configuration.""" | ||
|
|
||
| version = IniConfig("1.0.0") | ||
| debug = IniConfig(False) | ||
|
|
||
| class Logging: | ||
| """Logging configuration.""" | ||
|
|
||
| level = IniConfig("INFO") | ||
| file = IniConfig("app.log") | ||
|
|
||
| class Rotation: | ||
| """Log rotation settings.""" | ||
|
|
||
| max_size = IniConfig(10) # MB | ||
| backup_count = IniConfig(5) | ||
|
|
||
|
|
||
| print(f"App version: {AppConfig.version}") | ||
| print(f"Debug mode: {AppConfig.debug}") | ||
| print(f"Log level: {AppConfig.Logging.level}") | ||
| print(f"Log file: {AppConfig.Logging.file}") | ||
| print(f"Max log size: {AppConfig.Logging.Rotation.max_size} MB") | ||
| print(f"Backup count: {AppConfig.Logging.Rotation.backup_count}") | ||
|
|
||
| # Example 4: TOML with nested tables | ||
| print("\n=== Nested TOML Configuration ===") | ||
|
|
||
|
|
||
| class TomlConfig(Config[T]): | ||
| ... | ||
|
|
||
|
|
||
| TomlConfig.set_file(Path("nested_example.toml")) | ||
|
|
||
|
|
||
| class ServiceConfig: | ||
| """Service configuration.""" | ||
|
|
||
| name = TomlConfig("my-service") | ||
| port = TomlConfig(8080) | ||
|
|
||
| class API: | ||
| """API configuration.""" | ||
|
|
||
| version = TomlConfig("v1") | ||
| timeout = TomlConfig(30) | ||
|
|
||
| class RateLimit: | ||
| """Rate limiting configuration.""" | ||
|
|
||
| enabled = TomlConfig(True) | ||
| requests_per_minute = TomlConfig(60) | ||
|
|
||
|
|
||
| print(f"Service name: {ServiceConfig.name}") | ||
| print(f"Service port: {ServiceConfig.port}") | ||
| print(f"API version: {ServiceConfig.API.version}") | ||
| print(f"API timeout: {ServiceConfig.API.timeout}s") | ||
| print(f"Rate limit enabled: {ServiceConfig.API.RateLimit.enabled}") | ||
| print(f"Requests per minute: {ServiceConfig.API.RateLimit.requests_per_minute}") | ||
|
|
||
| print("\n=== Configuration files created successfully! ===") | ||
| print("Check the following files to see the structure:") | ||
| print(" - nested_example.json") | ||
| print(" - nested_example.yaml") | ||
| print(" - nested_example.ini") | ||
| print(" - nested_example.toml") | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| [AppConfig.Logging.Rotation] | ||
| max_size = 10 | ||
| backup_count = 5 | ||
|
|
||
| [AppConfig.Logging] | ||
| level = INFO | ||
| file = app.log | ||
|
|
||
| [AppConfig] | ||
| version = 1.0.0 | ||
| debug = False | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| { | ||
| "DatabaseConfig": { | ||
| "Credentials": { | ||
| "username": "admin", | ||
| "password": "secret123", | ||
| "use_ssl": true | ||
| }, | ||
| "host": "localhost", | ||
| "port": 5432, | ||
| "name": "myapp_db" | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doc lists a
Parserfacade inext/parsers.py, but there is noParsertype exported/implemented there (onlyConfkitParser,IniParser,MsgspecParser,EnvParser). Please either add the facade described here or update the documentation to reflect the actual API surface.