Skip to content

Commit

Permalink
[Fix #12] Introduce feature flags to make extension of io/Coercions o…
Browse files Browse the repository at this point in the history
…ptional

These allow dependent libraries to use `fs` without the side-effect of extend.

Otherwise one can unawarely create issues, as seen in https://github.com/clojure-emacs/clj-refactor.el/issues/508.

https://clojure.org/reference/protocols#_guidelines_for_extension says:

> If you don’t own the protocol or the target type, you should only extend in app (not public lib) code, and expect to maybe be broken by either owner.
  • Loading branch information
vemv authored Dec 14, 2021
1 parent 04c7b1a commit 6002681
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/me/raynes/fs.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
(:refer-clojure :exclude [name parents])
(:require [clojure.zip :as zip]
[clojure.java.io :as io]
[clojure.java.shell :as sh])
[clojure.java.shell :as sh]
[me.raynes.fs.feature-flags :as feature-flags])
(:import [java.io File FilenameFilter]
[java.nio.file Files Path LinkOption CopyOption]
[java.nio.file.attribute FileAttribute]))
Expand Down Expand Up @@ -141,10 +142,11 @@
[path]
(predicate isHidden (file path)))

(extend-protocol io/Coercions
Path
(as-file [this] (.toFile this))
(as-url [this] (.. this (toFile) (toURL))))
(when feature-flags/extend-coercions?
(extend-protocol io/Coercions
Path
(as-file [this] (.toFile this))
(as-url [this] (.. this (toFile) (toURL)))))

(defn- ^Path as-path
"Convert `path` to a `java.nio.file.Path`.
Expand Down
12 changes: 12 additions & 0 deletions src/me/raynes/fs/feature_flags.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
(ns me.raynes.fs.feature-flags
"Compile-time feature flags.
In order to use them:
* `require` this ns before any other ns from this lib.
* `alter-var-root` a given feature flag within this ns to a different, desired value
* proceed to `require` the rest of this library.")

(def extend-coercions?
"Should the clojure.java.io/Coercions protocol be extended by this library?"
true)

0 comments on commit 6002681

Please sign in to comment.