2

At the beginning of my Clojure program I do a:

(def db-coords
    {:classname "org.postgresql.Driver"
     :subprotocol "postgresql"
     :subname (str "//" host ":" port "/" dbname) ;; host, port and dbname are defd above
     :user      "foo"
     :password  "bar"})

I would like to store these values in an external file (.clj or otherwise) on the classpath and load them from there. Clearly one way to do it would be using java.util.Properties but I suspect there is an idiomatic way in Clojure.

3 Answers 3

5

Consider using spit and slurp with read-string.

Clojure has a built-in ability to serialize and deserialize its data structures.

To serialize to a file:

(spit "./coords.txt" db-coords)

To load the file back in as a string and deserialize:

(read-string (slurp "./coords.txt"))
Sign up to request clarification or add additional context in comments.

Comments

1

clj-config is a nice library which encapsulates the spit, read-string api

https://github.com/Raynes/clj-config

Comments

0

I have created a library for handling env specific configuration regions via Clojure maps and JVM system properties:

https://github.com/bbbates/confijulate This was a pattern that I found myself repeating over and over for every project.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.