Duplicate key and custom generators

44 views
Skip to first unread message

Andrea Richiardi

unread,
May 22, 2016, 3:41:28 PM5/22/16
to Plumbing and Graph: the Clojure utility belt
Hello plumatic,
I am playing with generators, which are definitely fun to use, one thing I am trying to achieve is to have custom (and meaningful) generators.

So I tried (using paraseba/faker):

(sgen/generate as/User {as/FirstName (tcgen/return (fname/first-name))
                                        as/LastName (tcgen/return (fname/last-name))})

where:

(def FirstName s/Str)
(def LastName s/Str)

(def User {(s/required-key :id) s/Int
           :first-name FirstName
           :last-name LastName
           :password s/Str})

But of course I get a java.lang.IllegalArgumentException: Duplicate key: class java.lang.String.
I also tried:

(sgen/generate as/User {as/FirstName (tcgen/return (fname/first-name))} {as/LastName (tcgen/return (fname/last-name))})

but here I get:
java.lang.ClassCastException: clojure.test.check.generators.Generator cannot be cast to clojure.lang.IFn

How can I handle this case (there must a very easy solution I am not seeing) ?

Jason Wolfe

unread,
May 22, 2016, 10:23:56 PM5/22/16
to Andrea Richiardi, Plumbing and Graph: the Clojure utility belt
Hi Andrea, 

Currently FirstName and LastName are different names for an identical schema, so there's no way to attach different generators.  

The simplest solution is to use `s/named` to ensure that FirstName and LastName are different schemas, e.g.

(def FirstName (s/named s/Str 'first))
(def LastName (s/named s/Str 'last)) 

HTH,
Jason


--
You received this message because you are subscribed to the Google Groups "Plumbing and Graph: the Clojure utility belt" group.
To unsubscribe from this group and stop receiving emails from it, send an email to prismatic-plumb...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Andrea Richiardi

unread,
May 25, 2016, 1:03:53 PM5/25/16
to Plumbing and Graph: the Clojure utility belt, a.richia...@gmail.com
On Sunday, May 22, 2016 at 7:23:56 PM UTC-7, Jason Wolfe wrote:
Hi Andrea, 

(def FirstName (s/named s/Str 'first))
(def LastName (s/named s/Str 'last)) 


This works fine, an additional question would be, is there a way to "store" this generator against my new version of User now?
I know this can be somehow done with custom functions, just asking in case you have a ready-made solution :)

Jason Wolfe

unread,
May 25, 2016, 1:18:48 PM5/25/16
to Andrea Richiardi, Plumbing and Graph: the Clojure utility belt
You can just def your map of User-related generators, and use / merge them with others when needed.  There isn't a special registry, but in my experience just normal Clojure vars usually suffice.  

Best,
Jason

--

Andrea Richiardi

unread,
May 25, 2016, 1:47:34 PM5/25/16
to Plumbing and Graph: the Clojure utility belt, a.richia...@gmail.com
That's ok, in any case it is just data, so vars are really ok here.
To unsubscribe from this group and stop receiving emails from it, send an email to prismatic-plumbing+unsub...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages