Skip to content

Flatten

flatten

Flatten strategy — collapse single-child wrapper dicts.

FlattenStrategy

Bases: SmeltStrategy

Collapse single-child wrapper dicts into dotted keys.

Source code in packages/axm-smelt/src/axm_smelt/strategies/flatten.py
Python
class FlattenStrategy(SmeltStrategy):
    """Collapse single-child wrapper dicts into dotted keys."""

    def __init__(self, max_depth: int | None = None) -> None:
        self._max_depth = max_depth

    @property
    def name(self) -> str:
        return "flatten"

    @property
    def category(self) -> str:
        return "structural"

    def apply(self, ctx: SmeltContext) -> SmeltContext:
        """Collapse single-child wrapper dicts into dotted keys.

        Uses ``ctx.parsed`` when available to skip
        ``json.loads``. Propagates the flattened object as
        ``parsed`` on the returned context.
        """
        parsed = ctx.parsed
        if parsed is not None:
            flattened = _flatten_node(parsed, self._max_depth)
            result = json.dumps(
                flattened,
                separators=(",", ":"),
                ensure_ascii=False,
            )
            new_ctx = SmeltContext(text=result, format=ctx.format)
            new_ctx._parsed = flattened
            return new_ctx

        text = ctx.text
        stripped = text.strip()
        if not stripped:
            return ctx

        if stripped[0] in ("{", "["):
            try:
                data = json.loads(stripped)
                flattened = _flatten_node(data, self._max_depth)
                result = json.dumps(
                    flattened,
                    separators=(",", ":"),
                    ensure_ascii=False,
                )
                new_ctx = SmeltContext(text=result, format=ctx.format)
                new_ctx._parsed = flattened
                return new_ctx
            except (json.JSONDecodeError, ValueError):
                pass

        return ctx
apply(ctx)

Collapse single-child wrapper dicts into dotted keys.

Uses ctx.parsed when available to skip json.loads. Propagates the flattened object as parsed on the returned context.

Source code in packages/axm-smelt/src/axm_smelt/strategies/flatten.py
Python
def apply(self, ctx: SmeltContext) -> SmeltContext:
    """Collapse single-child wrapper dicts into dotted keys.

    Uses ``ctx.parsed`` when available to skip
    ``json.loads``. Propagates the flattened object as
    ``parsed`` on the returned context.
    """
    parsed = ctx.parsed
    if parsed is not None:
        flattened = _flatten_node(parsed, self._max_depth)
        result = json.dumps(
            flattened,
            separators=(",", ":"),
            ensure_ascii=False,
        )
        new_ctx = SmeltContext(text=result, format=ctx.format)
        new_ctx._parsed = flattened
        return new_ctx

    text = ctx.text
    stripped = text.strip()
    if not stripped:
        return ctx

    if stripped[0] in ("{", "["):
        try:
            data = json.loads(stripped)
            flattened = _flatten_node(data, self._max_depth)
            result = json.dumps(
                flattened,
                separators=(",", ":"),
                ensure_ascii=False,
            )
            new_ctx = SmeltContext(text=result, format=ctx.format)
            new_ctx._parsed = flattened
            return new_ctx
        except (json.JSONDecodeError, ValueError):
            pass

    return ctx