This repository has been archived by the owner. It is now read-only.

Default field value support #7

Closed
wants to merge 1 commit into
from

Conversation

Projects
None yet
3 participants
@ibodrov
Contributor

ibodrov commented Mar 26, 2012

Patch to add default field value support.

Example:

(defcodec example {:a :int32 :b (default :int32 7)})
(encode example {:a 4})
(decode example *1) => {:a 4 :b 7}

Also, it works with nested types:

(defcodec example {:a :int32 :b (default (repeated :int32 :prefix :none) [1 2 3])})
(encode example {:a 4})
(decode example *1) => {:a 4 :b [1 2 3]}

@jamiei

This comment has been minimized.

Show comment
Hide comment
@jamiei

jamiei Mar 26, 2012

Default field value support would be great.

jamiei commented Mar 26, 2012

Default field value support would be great.

@ztellman

This comment has been minimized.

Show comment
Hide comment
@ztellman

ztellman Mar 28, 2012

Owner

Sorry for the delay on this, I've been travelling. I'm not opposed to this, but as far as I can tell your use-case can be handled by compile-frame and appropriate pre-encoder/post-decoder functions. Is there a reason you chose to not go this route?

Owner

ztellman commented Mar 28, 2012

Sorry for the delay on this, I've been travelling. I'm not opposed to this, but as far as I can tell your use-case can be handled by compile-frame and appropriate pre-encoder/post-decoder functions. Is there a reason you chose to not go this route?

@ibodrov

This comment has been minimized.

Show comment
Hide comment
@ibodrov

ibodrov Mar 28, 2012

Contributor

Because i totally missed pre-encoder/post-decoder in compile-frame definition. :-)
My bad, sorry.

This means, (default frame value) can be implemented in simple macro like this: (defmacro default [frame value] '(compile-frame ~frame #(or % ~value)), right?

Contributor

ibodrov commented Mar 28, 2012

Because i totally missed pre-encoder/post-decoder in compile-frame definition. :-)
My bad, sorry.

This means, (default frame value) can be implemented in simple macro like this: (defmacro default [frame value] '(compile-frame ~frame #(or % ~value)), right?

@ztellman

This comment has been minimized.

Show comment
Hide comment
@ztellman

ztellman Mar 29, 2012

Owner

Shouldn't have to be a macro, a function will suffice. Also, it might make more sense to have a top-level compile-frame that merges in a default map, rather than having it be per-value.

Owner

ztellman commented Mar 29, 2012

Shouldn't have to be a macro, a function will suffice. Also, it might make more sense to have a top-level compile-frame that merges in a default map, rather than having it be per-value.

@ztellman ztellman closed this Mar 29, 2012

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.