Skip to content

Pulling maps from sequences requires named keys #74

@flybot-nam-nguyenhoai

Description

@flybot-nam-nguyenhoai

Problem

When pulling from a sequence of maps, lasagna-pull requires all map keys to be specified up front. For example:

((pull/query ['?]) [{:a 1} {:a 2 :b 1}]) ;=> Exception: Wrong pattern
((pull/query [{:a '?}]) [{:a 1} {:a 2 :b 1}]) ;=> [{:a 1} {:a 2}]

Impact

It is difficult to work with lists of entities that may not follow fixed schemas, such as when working with Datomic or re-frame.

Probable cause

schema/patten-schema-of does not handle [:sequential :map] correctly. For example:

(def data-schema
  [:vector :map])

(def pattern-schema
  (schema/pattern-schema-of data-schema))

pattern-schema
;; => [:cat {:sg.flybot.pullable.schema/pattern? true}
;; =>  :map
;; =>  [:? [:fn #function[lvar?]]]
;; =>  [:? [:alt [:cat [:= :seq] [:vector {:min 1, :max 2} :int]]]]]

(malli.core/validate pattern-schema ['?]) ;=> false
(malli.core/validate pattern-schema [{} '?]) ;=> true

Suggestion

  • Allow using ['?] to pull whole elements (such as entire maps) from a sequence. For example:

    ((pull/query ['?]) [{:a 1} {:a 2 :b 1}])
    ;; => [{:a 1} {:a 2 :b 1}]

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