Skip to content

Add a SWAP mapping object (i.e. switch) #16

@ericpan64

Description

@ericpan64

Problem

When writing mappings, want something that emulates the logic of a switch statement / conditional branching. Maybe this could be an enum that the Mapper class (and other related classes) cleans up

Requested feature

from pydian.dicts import SWAP

some_mapping = lambda d: {
   "a": SWAP(get(d, 'some_field'), {
      1: "one", # Handle specific cases (anything hashable)
      2: "two",
      str: "Got a str", # Handle classes
      None:  None # Handle base case
   }, strict=True) # Default strict -- expect developer to handle all cases
}

Alternatives considered

Additional context

  • Fun to keep the 4-letter acronyms. SWAP ~ Switch with advanced positioning

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions