如何访问 Malli 地图架构中分配有键的属性?

发布于 2025-01-10 11:19:17 字数 2409 浏览 3 评论 0原文

给定一个像 [:map [:key {:optional true} :int]] 这样的 Malli 地图模式,我如何获取在中分配关键字 :key 的属性写步行者时的地图?在此示例中,这些是 {:optional true}

正如您在下面的示例中看到的,walker 访问地图的所有“值”,在本例中仅访问 :int 模式,然后访问 :map架构。有没有办法到达中间步骤、“模式”和 :key 的属性?

(malli/walk                                                                                                   
  [:map [:key {:optional true} :int]]                                                                          
  (fn [schema path children options]                                                                           
    (println "Schema:" schema)                                                                                 
    (println "Path:" path)                                                                                     
    (println "Children:" children)                                                                             
    (println "Options:" options)                                                                               
    (println "Properties:" (malli/properties schema))                                                          
    (println))) => nil 


;; Schema: :int                                                                                                 
;; Path: [:key]                                                                                                  
;; Children: nil                                                                                                 
;; Options: nil                                                                                                  
;; Properties: nil                                                                                               
;;                                                                                                               
;; Schema: [:map [:key {:optional true} :int]]                                                                   
;; Path: []                                                                                                      
;; Children: [[:key {:optional true} nil]]                                                                       
;; Options: nil                                                                                                  
;; Properties: nil   

   

Given a Malli map schema like [:map [:key {:optional true} :int]], how do I get at the properties assigned the keyword :key in the map when writing a walker? In this example, these are {:optional true}.

As you can see in the below example the walker visits all the "values" of the map, in this case just the :int schema, and then out to the :map schema. Is there any way to get to the inbetween step, the "schema" and properties of :key?

(malli/walk                                                                                                   
  [:map [:key {:optional true} :int]]                                                                          
  (fn [schema path children options]                                                                           
    (println "Schema:" schema)                                                                                 
    (println "Path:" path)                                                                                     
    (println "Children:" children)                                                                             
    (println "Options:" options)                                                                               
    (println "Properties:" (malli/properties schema))                                                          
    (println))) => nil 


;; Schema: :int                                                                                                 
;; Path: [:key]                                                                                                  
;; Children: nil                                                                                                 
;; Options: nil                                                                                                  
;; Properties: nil                                                                                               
;;                                                                                                               
;; Schema: [:map [:key {:optional true} :int]]                                                                   
;; Path: []                                                                                                      
;; Children: [[:key {:optional true} nil]]                                                                       
;; Options: nil                                                                                                  
;; Properties: nil   

   

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

铃予 2025-01-17 11:19:17

您需要将选项 :malli.core/walk-entry-vals 传递给 walker 函数。

(malli/walk
 [:map [:key {:optional true} :int]]
 (fn [schema path children options]
   (println "Schema:" schema)
   (println "Path:" path)
   (println "Children:" children)
   (println "Options:" options)
   (println "Properties:" (malli/properties schema))
   (println))
 {::malli/walk-entry-vals true})

;; Schema: :int
;; Path: [:key]
;; Children: nil
;; Options: #:malli.core{:walk-entry-vals true}
;; Properties: nil
;; 
;; Schema: [:malli.core/val {:optional true} :int]
;; Path: [:key]
;; Children: (nil)
;; Options: #:malli.core{:walk-entry-vals true}
;; Properties: {:optional true}
;; 
;; Schema: [:map [:key {:optional true} :int]]
;; Path: []
;; Children: [[:key {:optional true} nil]]
;; Options: #:malli.core{:walk-entry-vals true}
;; Properties: nil

You need to pass the option :malli.core/walk-entry-vals to the walker function.

(malli/walk
 [:map [:key {:optional true} :int]]
 (fn [schema path children options]
   (println "Schema:" schema)
   (println "Path:" path)
   (println "Children:" children)
   (println "Options:" options)
   (println "Properties:" (malli/properties schema))
   (println))
 {::malli/walk-entry-vals true})

;; Schema: :int
;; Path: [:key]
;; Children: nil
;; Options: #:malli.core{:walk-entry-vals true}
;; Properties: nil
;; 
;; Schema: [:malli.core/val {:optional true} :int]
;; Path: [:key]
;; Children: (nil)
;; Options: #:malli.core{:walk-entry-vals true}
;; Properties: {:optional true}
;; 
;; Schema: [:map [:key {:optional true} :int]]
;; Path: []
;; Children: [[:key {:optional true} nil]]
;; Options: #:malli.core{:walk-entry-vals true}
;; Properties: nil
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文