Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Troubles with spec's coercion #1

Open
WhittlesJr opened this issue Sep 18, 2019 · 2 comments · May be fixed by #2
Open

Troubles with spec's coercion #1

WhittlesJr opened this issue Sep 18, 2019 · 2 comments · May be fixed by #2

Comments

@WhittlesJr
Copy link

I'm really liking this library, but I ran into a minor roadblock.

Consider an s/or spec being given for the :spec of a cfg/def. Assuming the configured value is valid for the spec, the "coerced" value becomes a vector with the s/or key first and the actual value second:

(s/def ::transport #{:ip :serial})
(s/def ::transport-cfg (s/or :enabled  ::transport
                             :disabled #{:disabled}))

(cfg/def MODBUS_TRANSPORT
  {:doc      "Transport layer to use for Modbus."
   :spec     (cfgc/from-edn ::transport-cfg)})

(::transport comes from a different namespace IRL, hence the separation.)

In this case, if MODBUS_TRANSPORT is set to ":ip", the coerced value becomes [:enabled :ip].

Other than creating a second var like below, I can't think of any good way around this:

(def modbus-transport (some-> MODBUS_TRANSPORT second))

This doesn't strike me as a very clean solution. Ideally, I'd want to be able to specify a coercion function separately from the :spec, and have that function alone run against the raw value. Then the spec would be only used for validation, and our values would not be polluted by spec's rather odd habit of sometimes conforming values to non-valid outputs (as in this case.)

@WhittlesJr
Copy link
Author

I've been trying some other possible solutions but I can't seem to get it right. I'll keep poking at it a bit though.

@WhittlesJr
Copy link
Author

I think the answer lies in using https://github.com/wilkerlucio/spec-coerce. Hopefully there will be a PR today.

Unfortunately this would be a non-trivial change, and the interface would effectively change as well. I'll put together the PR and we can evaluate its relative merits.

@WhittlesJr WhittlesJr linked a pull request Sep 19, 2019 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant