Skip to content

Commit

Permalink
otk: merge _op_{seq,map}_join back into op_join()
Browse files Browse the repository at this point in the history
The helpers are so small that they can be merged back into the
`op_join()` and this avoids untested code and extra checks that
are redundant.
  • Loading branch information
mvo5 committed Oct 2, 2024
1 parent 61ca42d commit 2197595
Showing 1 changed file with 6 additions and 31 deletions.
37 changes: 6 additions & 31 deletions src/otk/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import os.path
import pathlib
import re
from typing import Any, List
from typing import Any

import yaml

Expand Down Expand Up @@ -258,40 +258,15 @@ def op_join(_: Context, state: State, tree: dict[str, Any]) -> Any:
f"seq join received values of the wrong type, was expecting a list of lists but got {values!r}", state)

if all(isinstance(sl, list) for sl in values):
return _op_seq_join(state, values)
return list(itertools.chain.from_iterable(values))
if all(isinstance(sl, dict) for sl in values):
return _op_map_join(state, values)
result = {}
for value in values:
result.update(value)
return result
raise TransformDirectiveTypeError(f"cannot join {values}", state)


def _op_seq_join(state: State, values: List[list]) -> Any:
"""Join to sequences by concatenating them together."""

if not all(isinstance(sl, list) for sl in values):
# XXX: untested
raise TransformDirectiveTypeError(
f"seq join received values of the wrong type, was expecting a list of lists but got {values!r}", state)

return list(itertools.chain.from_iterable(values))


def _op_map_join(state: State, values: List[dict]) -> Any:
"""Join two dictionaries. Keys from the second dictionary overwrite keys
in the first dictionary."""

if not all(isinstance(sl, dict) for sl in values):
# XXX: untested
raise TransformDirectiveTypeError(
f"map join received values of the wrong type, was expecting a list of dicts but got {values!r}", state)

result = {}

for value in values:
result.update(value)

return result


@tree.must_be(str)
def substitute_vars(ctx: Context, state: State, data: str) -> Any:
"""Substitute variables in the `data` string.
Expand Down

0 comments on commit 2197595

Please sign in to comment.