Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/niklasvonm/config-adapter
Create data models from configuration files.
https://github.com/niklasvonm/config-adapter
Last synced: 23 days ago
JSON representation
Create data models from configuration files.
- Host: GitHub
- URL: https://github.com/niklasvonm/config-adapter
- Owner: NiklasvonM
- License: mit
- Created: 2024-03-10T20:32:45.000Z (8 months ago)
- Default Branch: main
- Last Pushed: 2024-04-11T19:29:31.000Z (7 months ago)
- Last Synced: 2024-04-12T00:19:21.917Z (7 months ago)
- Language: Python
- Size: 125 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# config-adapter
Create data models from configuration files, taking already defined data models into account.
Currently, `dataclass` as well as Pydantic `BaseModel` generation is supported.
## Example Usage
```py
from config_adapter import generate_model
from pydantic import BaseModel, Fieldclass Money(BaseModel):
amount: float
currency: strexisting_models = [Money]
json_data = {
"database": {
"host": "localhost",
"port": 5432,
"known_hosts": ["*"],
"username": "user",
"password": "pass",
"schema": "public",
},
"logging": {"level": "INFO", "destination": "file"},
"costs": {
"amount": 10.0,
"currency": "USD",
},
"another_logging": {"level": "WARNING", "destination": "stdout"},
}model_code = generate_model(source=json_data, existing_models=existing_models)
print(model_code)
``````py
from pydantic import BaseModelclass ConfigAdapter(BaseModel):
database: DatabaseConfig
logging: LoggingConfig
costs: Money
another_logging: LoggingConfigclass DatabaseConfig(BaseModel):
host: str
port: int
known_hosts: list[str]
username: str
password: str
schema: strclass LoggingConfig(BaseModel):
level: str
destination: str
```