【问题标题】:No implementation of method: :spec of protocol: #'schema.core/Schema没有实现方法::协议规范:#'schema.core/Schema
【发布时间】:2018-08-01 00:53:42
【问题描述】:

我写了一个宏

(defmacro defendpoint [msg-type url-key schema]
  `(defrecord ~msg-type []
     Create
     (create [entity#]
       (s/validate ~schema entity#)
       (create-entity (~url-key urls) entity#))))

我就是这样使用它的

(defendpoint Location :locations
  {... my schema ...}})

(defendpoint LocationHierarchy :location-hierarchies
  {... my schema ...}})

我第一次使用宏,它可以工作

(create  (map->Location
          {... data ...}))

=> { ... json response ...}

但是第二次失败了:

(create  (map->LocationHierarchy
          {... data ...}))

=> 1. Unhandled java.lang.IllegalArgumentException
 No implementation of method: :spec of protocol:
 #'schema.core/Schema found for class: ohds.client$fn__32303

我不确定为什么会这样。我希望第二次调用的工作方式与第一次相同,但在验证步骤中似乎存在错误。事实上,如果我从宏中删除 (s/validate...),它会按预期工作。所以我不确定这里到底发生了什么。

I've created a gist that shows the entire file I'm working with

【问题讨论】:

    标签: clojure


    【解决方案1】:

    我将介绍我是如何解决我的问题的,希望该方法对其他人有所帮助。

    tl;dr

    ;; Wrong:
    (def date-schema (s/both s/Str #(re-matches #"my-regex" %)))
    ;; Right:
    (def date-schema (s/both s/Str (s/pred #(re-matches #"my-regex" %))))
    

    方法

    我从错误开始:No implementation of method: :spec of protocol: #'schema.core/Schema found for class: ohds.client$fn__32303

    一开始我并不确定这意味着什么。 :spec of protocol: 让我失望了。但我确实看到它提到了schema.core/Schema,所以我读了the source code。我发现 Schema 是一个带有方法 spec 的协议,就像错误所说的那样:/

    下一个令人困惑的部分是for class: ohds.client$fn__32303。我想知道为什么我的命名空间需要实现协议。那没有任何意义。然后我注意到$fn_32303。这告诉我错误所在的某个地方有一个 lambda!

    此时,我假设我的架构有问题。所以我从我的模式中删除了所有特殊验证,并在任何地方使用s/Str 来查看它是否有效。确实如此,所以我来对了地方!我一次一个地添加了特殊验证,直到测试再次失败。问题出在我的日期模式中。

    我查看了我在它上面定义的架构,看看有什么不同。在那里我注意到我未能将我的 lambda 封装在 s/pred 中。

    道德

    Clojure 设计精良,因此错误消息可以准确地告诉您出了什么问题。你只需要了解它。

    【讨论】:

      【解决方案2】:

      这件事发生在我身上。原来我有一个引用自己的defschema

      (s/defschema Templates {:templates [Templates]
                              :error     s/Bool})
      

      而不是正确的参考:

      (s/defschema Templates {:templates [Template]
                              :error     s/Bool})
      

      【讨论】:

        猜你喜欢
        • 2017-05-01
        • 2011-03-17
        • 2015-12-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-06-21
        相关资源
        最近更新 更多