Fork me on GitHub
#clojure-spec
<
2019-09-27
>
lmergen10:09:19

is it possible to somehow tokenize a string using specs, in a s/cat-like way? e.g.

(s/def :directory (fn [x] (re-matches ...))
(s/def :filename (fn [x] (re-matches ...))
(s/def :absolute-path (s/cat :d :directory :f :filename))
(which doesn't work for obvious reasons, but that's the idea of what i'm looking for)

lmergen10:09:04

actually i'm probably looking for a way to compose regexes more than i'm looking for this spec-like functionality... :thinking_face:

alexmiller10:09:15

You can do this (by seq-ing a string) but I generally think it’s a bad idea. There is actually a regex composition library for Clojure, but I’m blanking on the name

alexmiller10:09:24

Maybe https://github.com/fhur/regie is what I’m thinking of?

lmergen10:09:22

i see, that actually looks interesting

jonpither12:09:50

Hi. is there a generalised pattern / approach for handling config, in that a) defining the specs for the config attributes, b) documenting the individual options, c) providing default values for options? I realise this Q is a bit wide ranging, but I'm wanting to take a step back and to see what others usually do for config input

taylor13:09:11

I don’t have an answer for your exact question, but I’ve spec’d CLI args before which is kinda like config https://github.com/taylorwood/clojurl/blob/c69cef7c1f2b46ac9034cf75ea1c5f7d3abf6d2b/src/clojurl.clj#L13-L42 Re: documentation, spec doesn’t really support it (yet?), and I probably wouldn’t use spec to influence config default values. That said, there’s probably an opportunity to pull pieces of this together into an “opinionated” config library :man-shrugging:

👍 4
jonpither14:09:07

Thanks @U3DAE8HMG will check this out

jumar21:09:29

@U050DD55V I've done something similar for one of our applications. It's a rather hairy code and I'm sure there's a better approach for validation and replacing the invalid values with defaults but here's essentially what we've had in production for at least 1 year: https://github.com/jumarko/clojure-experiments/blob/master/src/clojure_experiments/config/config.clj#L1 See this commit which shows all relevant parts: https://github.com/jumarko/clojure-experiments/commit/35d429e1641806bba78b470e4f4e249207739e96

jaihindhreddy08:09:58

spec might support docstrings but probably won't support metadata, because that might cause specs to become bloated and less suitable for a variety of usecases. Because ns-qualified keywords come with no strings attached, spec is able to associate global semantics in particular contexts (when calling s/validate, for example). In a same way, maybe something like aero can maintain its own global registry of documentation, defaults etc. and be useful in contexts that make sense for it, perhaps pawning off the job of validation to spec. @U050DD55V sorry for being vague 😅 And pointing to your own library (`aero`) 😛

rickmoynihan17:10:14

@U050DD55V we have an inhouse tool that we use to specify service requirements and dependencies in EDN… it’s meant to tie together CI services with what they expect at runtime etc, and help in pulling clojure services and service dependencies together across various environments… e.g. in dev you want to run the services with repls and various things set… in prod you want packer images built with config files written in the right places that define parameters to be injected into env files and config files etc. And all environments want to pull in dependent services. Anyway part of it is aero; and each service declares things like:

:omni/keys {:stardog/stardog-home {:doc "Location of the stardog home directory"
                                    :default [:install "stardog-home"]
                                    :env-var STARDOG_HOME}
             :stardog/java-args {:doc "Arguments to the JVM when running stardog"
                                 :env-var STARDOG_SERVER_JAVA_ARGS}
             :stardog/temp-dir {:doc "Java temp directory for the stardog process"
                                :env-var STARDOG_JAVA_TEMP_DIR}
             :stardog/logs-dir {:doc "Directory to write the stardog.log file to"
                                :default "."}}

rickmoynihan17:10:29

which includes the doc string, and where the variable comes from etc.

rickmoynihan17:10:17

so services can inform things further up the chain what they need to set; and display a useful error when something is missing