ninjudd / clojure-protobuf

Google protocol buffers wrapper for Clojure.
Eclipse Public License 1.0
217 stars 70 forks source link

Keywords are not converted to strings when used as map values #47

Closed ozanmakes closed 11 years ago

ozanmakes commented 11 years ago

I'm getting Exception in thread "main" java.lang.IllegalArgumentException: error setting string field foo to :bar, compiling:(client.clj:12:10) when I try to pass a map containing keyword values to protobuf without converting them to strings first.

amalloy commented 11 years ago

Well, yeah. Keywords aren't strings. Is it an advertised feature somewhere in the docs? If so, we'll look into it; otherwise, I don't know why you'd expect it to work, any more than you'd expect us to automatically convert "5" to 5 when writing to a numeric field.

ozanmakes commented 11 years ago

@amalloy It looks like protobuf treats all of the following the same

(protobuf User :name "Guest")
(protobuf User 'name "Guest" )
(protobuf User "name" "Guest" )

Since there doesn't seem to be a way to pass keyword values to protobuf (unlike 5), and Clojure usually implements keywords as strings on platforms they are not supported (like ClojureScript) I think it makes sense to convert them to strings instead of disallowing their usage as values entirely (maybe I'm missing an extension?) and forcing the user to convert them manually even when they are used in maps in a idiomatic way.

amalloy commented 11 years ago

They're not disallowed; if you have a fixed set of possible values, you can define a protobuf enum, and use keywords to represent them in clojure. Using keywords to represent arbitrary strings is not recommended, in protobuf or in clojure.

ozanmakes commented 11 years ago

@amalloy hmm since the values I'm using are not arbitrary, I was actually going to define them in an enum after I complete the specification but I got carried away and forgot my actual intention. Makes a lot of sense, sorry!

ozanmakes commented 11 years ago

@amalloy by the way, does protobuf do any conversion between :keyword-values and the names that can be used in protocol buffer enums?