metosin / ring-swagger

Swagger Spec for Clojure Web Apps
http://metosin.github.io/ring-swagger/doc/
371 stars 84 forks source link

Refactor json-type multimethods to a protocol #22

Closed Deraen closed 9 years ago

Deraen commented 9 years ago

While performance doesn't matter in this case, protocols would make sense because creating new schema types would be easier:

(defrecord RequiredSchema [s]
  s/Schema
  (walker [this]
    (let [sub-walker (s/subschema-walker s)]
      (fn [x]
        (if (seq x)
          (sub-walker x)
          (smacros/validation-error this x (list 'required (su/value-name x)))))))
  (explain [this]
    (list 'required (s/explain s)))
  rs/JsonType
  (json-type [this] (rs/json-type (:s this)))
Deraen commented 9 years ago

In addition to json-type method, other stuff should also be moved to protocol so that adding support for new type would only require extending the protocol.

(defprotocol ISwaggered []
  (json-type [this])
  ; json: coerce from string
  (coerce [this string])
  (encode [this]) ; cheshire add-encoder?
  ; EDN tag read/parse?
  ; transit
  )
Deraen commented 9 years ago

Done.