Defaults for a repeated field

8 views
Skip to first unread message

Mahesh Paolini-Subramanya

unread,
Oct 16, 2013, 5:45:13 PM10/16/13
to pi...@googlegroups.com
How do I go about specify the default for a repeated field? 
e.g., given a .piqi file as follows
.record [ 
    .name foo-heartbeat
    .field [
        .name message-type
        .code 1
        .type foo-heartbeat-message-type
    ]
    .field [
        .name version
        .code 2
        .type foo-version
    ]
    .field [
        .name interfaces
        .code 7
        .type interface
        .repeated
    ]


I'd like to be set the default for the 'interfaces' field to be an empty list.
e.g., if you look at the following erlang
(ubic@paglierino)1> A1 = {ubic_foo_heartbeat,<<"M_wY>Py">>,1,[]}.
(ubic@paglierino)3> ubic_records:gen_foo_heartbeat(A1, json).
<<"{\"message_type\":\"M_wY>Py\",\"version\":1}">>
I'd prefer the output to actually look like
<<"{ \"message_type\": \"M_wY>Py\", \"version\": 1, \"interfaces\": [] }">>
I know that I can achieve this by running ubic_records:gen_foo_heartbeat(A1, json, [{json_omit_null_fields, false}])., but that has other side-effects -(you get all the other un-assigned fields to, not just this one)

Is there a way here? Or am I SOL?

Anton Lavrik

unread,
Oct 17, 2013, 5:25:14 AM10/17/13
to pi...@googlegroups.com
Currently, there is no way to control how individual missing optional and repeated fields are mapped to JSON.

If we were to add this ability, it looks like it should be controlled at the schema level. For example, adding an optional .json-omit-missing true|false property to a field would override the runtime setting.

What do you think?

Anton



--
You received this message because you are subscribed to the Google Groups "piqi" group.
To unsubscribe from this group and stop receiving emails from it, send an email to piqi+uns...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Mahesh Paolini-Subramanya

unread,
Oct 17, 2013, 5:45:36 AM10/17/13
to pi...@googlegroups.com
That, I believe, would do the trick!

Anton Lavrik

unread,
Oct 21, 2013, 3:28:12 AM10/21/13
to pi...@googlegroups.com
This is in master now: https://github.com/alavrik/piqi/commit/f84fef7e09147d1c2d42da8e46300d0a2c262fe9

To use this feature, you will need to build the piqi binary from source code and configure piqi-erlang to pick it up from a custom location as described here: https://github.com/alavrik/piqi-erlang#advanced-installation It should be fairly straightforward.

Anton

Reply all
Reply to author
Forward
0 new messages