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

Failed to acquire keys which have default value. #310

Closed
defclass opened this issue Jun 2, 2017 · 5 comments
Closed

Failed to acquire keys which have default value. #310

defclass opened this issue Jun 2, 2017 · 5 comments

Comments

@defclass
Copy link

defclass commented Jun 2, 2017

Hi, I have simple route below

Example:

(GET "/get_messages" []
     :query-params [{since :- s/Int nil}
                    {until :- s/Int nil}
                    {limit :- s/Int 20}
                    :as query-params]
     (prn query-params))

I have printed query-params Var at present , it seems didn't contains the keys which have default value.

Results:

curl /get_messages => {}
curl /get_messages?limit=10 => {:limit 10}

I expect :
curl /get_messages => {:limit 20}

Or I missed some configurations, thanks for reading this post.

@defclass defclass changed the title Is there a handy way to acquire query-params map which is coerced Failed to acquire keys which have default value. Jun 2, 2017
@ikitommi
Copy link
Member

ikitommi commented Jun 2, 2017

Hi. This is originating from Plumbing fnk-syntax. Could you write issue there and link it to this issue? The corresponding plumbing code would be something like:

(require '[plumbing.core :refer :all])

(defnk get_messages
  [{since :- s/Int nil}
   {until :- s/Int nil}
   {limit :- s/Int 20}
   :as query-params]
  query-params)

(get_messages {})
; {}

@ikitommi
Copy link
Member

ikitommi commented Jun 2, 2017

Also related:

e.g. we could add a coercer to compojure-api, which would apply the defaults, but that still doesn't woudn't populate the query-params in your example with fnk syntax as Plumbing separates coercion from destructuring. It would work only when you define the Schema as data like with :query:

(GET "/defaults" []
  :query [query-params {:limit (st/default s/Int 10)}]
  (prn query-params))

@Deraen
Copy link
Member

Deraen commented Jun 2, 2017

IMO: Not a bug, works the same as Clojure default map destructuring:

(defn foo [{:keys [a b] :or {a 1 b 2} :as x}]
  x)

(foo {})
;; => {}

Default values from destructuring only work when referring the destructured value directly.

@defclass
Copy link
Author

defclass commented Jun 2, 2017

Hi Tommi, thanks for your speedy reply and excellent solution. I've read the related links you provided. It seems need Plumbing to provide this handy notation.

According to @Deraen 's opinion, maybe I went the wrong way, I'll try st/default, or provide default value in some other place where use query-params for now.

Cheers

@ikitommi
Copy link
Member

ikitommi commented Jun 9, 2017

Closing this, continues on #174

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants