This file contains 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
from pydantic import BaseModel | |
class DBCredentialsModel(BaseModel): | |
comment: str | |
password: Password | |
uri: DBUri | |
port: DBPort | |
host: DBHost | |
class Config: |
This file contains 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
import json | |
class CustomDBCredentialsEncoder(json.JSONEncoder): | |
def default(self, obj): | |
try: | |
# Intentionally omit other fields | |
return {"uri": obj.uri.get_secret()} | |
except AttributeError: | |
return super().default(obj) | |
This file contains 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
@dataclass | |
class DBCredentials: | |
password: Password | |
uri: DBUri | |
port: DBPort | |
host: DBHost |
This file contains 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
>>> print(credentials) | |
DBCredentialsWithDescriptors(password=ReadOnce[secrets=*****], uri=ReadOnce[secrets=*****], port=ReadOnce[secrets=*****], host=ReadOnce[secrets=*****]) |
This file contains 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
>>> credentials = DBCredentialsWithDescriptors() | |
>>> credentials.password.get_secret() | |
'db-password' | |
>>> credentials.password.get_secret() | |
... | |
readonce.UnsupportedOperationException: ('Not allowed on sensitive value', 'Sensitive data was already consumed') |
This file contains 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
@dataclass | |
class DBCredentialsWithDescriptors: | |
password: Password = Password("db-password") | |
uri: DBUri = DBUri("mysql://") | |
port: DBPort = DBPort(3306) | |
host: DBHost = DBHost("localhost") |
This file contains 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
from readonce import ReadOnce | |
class Password(ReadOnce): | |
def __init__(self, password: str) -> None: | |
super().__init__() | |
self.add_secret(password) | |
class DBUri(ReadOnce): | |
def __init__(self, uri: str) -> None: |
This file contains 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
DBPassword(password="awesome") | |
... | |
AttributeError: 'DBPassword' object has no attribute 'password' |
This file contains 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
from readonce import ReadOnce | |
from dataclasses import dataclass | |
@dataclass | |
class DBPassword(ReadOnce): | |
password: str | |
def __post_init__(self): | |
# This is going to fail with "AttributeError: 'DBPassword' object has no attribute 'password'" | |
self.add_secret(self.password) |
This file contains 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
>>> obj = Password(password="awesome_password") | |
>>> print(obj) | |
ReadOnce[secrets=*****] | |
>>> obj | |
ReadOnce[secrets=*****] |