FAQ
I'm trying to figure out how to expose optional values in a struct being
serialized in a way that is coder and user friendly. The struct represents
members of a replica set in mongo that get serialized to bson, but the
meaning is really irrelevant to the problem. A key thing to note is that
the defaults for unset values do not always correspond to the zero values
of the corresponding type.

For example:

type Member struct {
        Host string // not optional
        Priority float // optional, defaults to 1
        Votes int // optional, defaults to 1
}

The obvious way to do this is to make the optional values pointers, and
then a nil pointer means unset, and the code around it can infer the
default.

However, there are problems with this -

    - the struct doesn't print out well (prints out the pointer value)
    - comparing two values using will compare pointer values
    - requires separate logic to interpret what unset means

The other option is to use one struct for the external facing values, and a
different struct for marshaling, with internal code to translate between
the two.

The problem with that is that the zero value of the external struct (like
Member above) has non-default settings (for example, Member{} is created
with Priority = 0 and Votes = 0, but that's not the normal default), in
addition, you lose information about what values were intentionally set and
what values were just defaults.

Does anyone have any ideas on a nice clean way to make this work well in a
way that is obvious to consumers of the package, and still friendly to
things like test packages printing out values and comparing values? I
don't really like either of the options I've presented.

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

Search Discussions

  • DisposaBoy at Nov 25, 2013 at 6:05 pm

    On Friday, November 22, 2013 11:41:52 AM UTC-5, Nate Finch wrote:
    I'm trying to figure out how to expose optional values in a struct being
    serialized in a way that is coder and user friendly. The struct represents
    members of a replica set in mongo that get serialized to bson, but the
    meaning is really irrelevant to the problem. A key thing to note is that
    the defaults for unset values do not always correspond to the zero values
    of the corresponding type.

    For example:

    type Member struct {
    Host string // not optional
    Priority float // optional, defaults to 1
    Votes int // optional, defaults to 1
    }

    The obvious way to do this is to make the optional values pointers, and
    then a nil pointer means unset, and the code around it can infer the
    default.

    However, there are problems with this -

    - the struct doesn't print out well (prints out the pointer value)
    - comparing two values using will compare pointer values
    - requires separate logic to interpret what unset means

    The other option is to use one struct for the external facing values, and
    a different struct for marshaling, with internal code to translate between
    the two.

    The problem with that is that the zero value of the external struct (like
    Member above) has non-default settings (for example, Member{} is created
    with Priority = 0 and Votes = 0, but that's not the normal default), in
    addition, you lose information about what values were intentionally set and
    what values were just defaults.

    Does anyone have any ideas on a nice clean way to make this work well in a
    way that is obvious to consumers of the package, and still friendly to
    things like test packages printing out values and comparing values? I
    don't really like either of the options I've presented.
    pre-init your variable perhaps? http://play.golang.org/p/0NXP97ql9r


    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Sugu Sougoumarane at Nov 28, 2013 at 12:17 am
    If you ask me, optionals and defaults don't belong to the wire protocol.
    Also, setting defaults for optional values is even more confusing. Any
    protocol that's tried to support these concepts has eventually become a
    mess.
    The only optimization I'd recommend is to not transmit zero values because
    go variables are inherently initialized as zero values.
    If you want to insist on something being required, implement at it as part
    of the struct, and let those flags flow through the protocol as is.

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

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedNov 22, '13 at 4:41p
activeNov 28, '13 at 12:17a
posts3
users3
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase