Data Models & Typings
Relevant source files
The following files were used as context for generating this wiki page:
The Data Models & Typings module in flext-target-ldif provides the structural foundation for converting Singer-compatible data streams into LDAP Data Interchange Format (LDIF). It defines the schemas for configuration, internal data representation (entries and files), and the operational interfaces required for target execution.
This module ensures compliance with the FLEXT ecosystem patterns by extending base models from flext-core, flext-ldif, and flext-meltano. It encapsulates the logic for format validation, business rule enforcement for LDAP entries, and the configuration management for the LDIF export process.
Sources: src/flext_target_ldif/models.py:1-40, src/flext_target_ldif/settings.py:1-15
Unified Model Collection
The project utilizes a centralized model collection, FlextTargetLdifModels (aliased as m), which provides a namespace for all domain-specific models. This collection includes configuration for formatting, representations of LDIF entities, and the Sink implementation for processing Singer records.
The diagram shows the hierarchical structure of the domain models within the FlextTargetLdifModels namespace.
Sources: src/flext_target_ldif/models.py:34-45, src/flext_target_ldif/models.py:228-231
Configuration Models
Runtime Settings
The FlextTargetLdifSettings class handles typed runtime configuration. It is built on Pydantic and is immutable (frozen=True), ensuring that configuration remains consistent throughout the target's lifecycle. It manages environment variables prefixed with FLEXT_TARGET_LDIF_.
| Field | Type | Default | Description |
|---|---|---|---|
output_file | str | "output.ldif" | Target filename for LDIF output. |
output_path | str | "./output" | Directory where files are generated. |
dn_template | str | "uid={uid},ou=users,dc=example,dc=com" | Template for Distinguished Name (DN) generation. |
line_length | int | 78 | Maximum length for line wrapping in the LDIF file. |
base64_encode | bool | False | Force base64 encoding for all attribute values. |
schema_validation | bool | True | Toggle for record schema validation. |
Sources: src/flext_target_ldif/settings.py:20-100, src/flext_target_ldif/target.py:31-36
Format Options
Within the models, LdifFormatOptions provides a granular configuration for the LDIF specification itself, including line separators and character encoding.
Sources: src/flext_target_ldif/models.py:48-78
Domain Entities
LDIF Entry
The LdifEntry model represents a single record in the LDAP format. It includes validation logic to ensure the entry adheres to standard LDAP rules, such as having at least one object class and a valid Distinguished Name format.
Key Validation Rules:
- The Distinguished Name (DN) must contain
attribute=valuepairs separated by commas. - Every entry must have at least one object class assigned.
- Every entry must contain at least one attribute.
Sources: src/flext_target_ldif/models.py:80-136
LDIF File
The LdifFile model tracks the state of the generated output, including metadata like file size and entry counts.
The flow demonstrates how a record transitions through validation models before being finalized in a file representation.
Sources: src/flext_target_ldif/models.py:138-182
Operational Models (Sink & Writer)
Singer Sink
The Sink class within FlextTargetLdifModels.TargetLdif manages the lifecycle of a single data stream. It acts as the bridge between the Singer protocol and the internal FlextTargetLdifWriter.
Responsibilities:
- Initializing the output directory based on the stream name.
- Managing the
FlextTargetLdifWriterinstance. - Processing batches and individual records through
process_record. - Ensuring resources are cleaned up (closing files) via
clean_up.
Sources: src/flext_target_ldif/models.py:184-225, src/flext_target_ldif/target.py:70-80
LDIF Writer Implementation
The FlextTargetLdifWriter performs the actual transformation of Python dictionaries into LDIF strings. It handles complex tasks such as DN generation from templates and conditional base64 encoding for non-ASCII or multi-line values.
# src/flext_target_ldif/writer.py:165-171
def _generate_dn(self, record: t.JsonMapping) -> str:
"""Generate DN from record using template."""
try:
return self.dn_template.format(**record)
except KeyError as e:
msg: str = f"Missing required field for DN generation: {e}"
raise FlextTargetLdifWriterError(msg) from e
Writing Logic Details:
- Base64 Detection: Automatically encodes values starting with space, colon, or containing non-ASCII/newline characters.
- Line Wrapping: Wraps lines exceeding the configured
line_length(default 78) using the LDIF folding standard (one leading space). - Timestamping: Optional inclusion of
# Generated on: ...comments in the file header.
Sources: src/flext_target_ldif/writer.py:38-72, src/flext_target_ldif/writer.py:173-181, src/flext_target_ldif/writer.py:228-243
Summary of Type Aliases
The module uses standardized FLEXT type aliases for clarity and consistency.
| Alias | Description | Underlying Type |
|---|---|---|
t.JsonMapping | Standard JSON object | Mapping[str, Any] |
t.StrSequence | List of strings | Sequence[str] |
t.StrMapping | String-to-string map | Mapping[str, str] |
p.Result[T] | Operation result wrapper | Result[T, Error] |
Sources: src/flext_target_ldif/models.py:9-30, src/flext_target_ldif/__init__.py:46-78
The Data Models & Typings system ensures that the flext-target-ldif project maintains high data integrity by enforcing LDAP structural requirements at the model level before files are ever written to disk.