Skip to content

Letk destructuring of an optional renamed key with schema validation #145

@lyderic-dutillieux

Description

@lyderic-dutillieux

Hi,
I've read the docs here : https://github.com/plumatic/plumbing/tree/master/src/plumbing/fnk#fnk-syntax
and here : http://plumatic.github.io/plumbing/plumbing.fnk.schema.html
and I don't understand how to go with some destructuring scenarios.

Basically, there is "vanilla" destructuring (just bind a symbol to a key in a map) + 3 main extra features being :

  1. Make a binding optional by providing a fallback value
  2. Make a binding's type/schema explicit
  3. Rename a binding (give it another name than the key)

And I struggle to combine these extra features.

Scenario A : vanilla => destructuring a plain map

(letk [[foo] {:foo 42}] foo) 
=> 42

Result : ✅

Scenario B : renaming only => destructure the key named :foo and bind to bar

(letk [ [[:foo :as bar]] {:foo 42}] bar) 
=> 42

Result : ✅

Scenario C : fallback only => destructure a key named :bar and fallback to 0 when the key is missing

(letk [ [{bar 0}] {:foo 42}] bar) 
=> 0

Result : ✅

Scenario D : schema only => destructure a key named :foo and add a schema hint

(letk [[foo :- schema.core/Number] {:foo 42}] foo)
=> 42

Result : ✅

Alright, using at most 1 extra feature worked.
Now let's try by combining 2 or 3 extra features.

Scenario E : schema + fallback => destructure a type-hinted missing key named :bar and fallback to 0

(letk [[{bar :- schema.core/Number 0}] {:foo 42}] bar)
=> 42

Result : ✅

Where I'm stuck

However, I haven't found a way to combine :

  • schema + renaming
  • renaming + fallback
  • schema + fallback + renaming

Are these combinations supported ? If not, what would it take to support them ?

PS : Classic let bindings support renaming + fallback, which I use often, hence the current issue :

(let [{baz :bar :or {baz 0}} {:foo 42}] baz)
=> 0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions