FAQ
If I write a patch to marshal a duration into its String() value (and
back, and backwards compatibly), is it likely to be accepted?

--

Search Discussions

  • Kyle Lemons at Sep 25, 2012 at 10:24 pm
    What's wrong with the way it works now?

    http://play.golang.org/p/05_4_awwPf
    On Tue, Sep 25, 2012 at 9:53 AM, Dustin wrote:


    If I write a patch to marshal a duration into its String() value (and
    back, and backwards compatibly), is it likely to be accepted?

    --

    --
  • Dustin at Sep 25, 2012 at 10:31 pm

    On Tuesday, September 25, 2012 3:25:00 PM UTC-7, Kyle Lemons wrote:
    What's wrong with the way it works now?

    I don't think number of nanoseconds is the best representation we could
    give to users, much like the way time.Time presents as a human readable
    string.

    I'd rather see this:

    Encoded: {"D":"30s"}

    Decoded: {30s}

    In my case, I've got an API around a struct that I have read and write
    access to. I have it working fine with a ton of reflection, but I think
    making it look more like the %v is much nicer.

    --
  • David Symonds at Sep 25, 2012 at 11:03 pm

    On Wed, Sep 26, 2012 at 8:31 AM, Dustin wrote:

    Encoded: {"D":"30s"}
    Nothing but Go is going to parse that. At least
    duration-in-nanoseconds is easy to deal with.

    If you've got your own struct, make a custom type that's compatible
    with time.Duration, and hang MarshalJSON/UnmarshalJSON methods off it.

    --
  • Dustin at Sep 25, 2012 at 11:41 pm

    On Tuesday, September 25, 2012 4:03:16 PM UTC-7, David Symonds wrote:
    On Wed, Sep 26, 2012 at 8:31 AM, Dustin <dsal...@gmail.com <javascript:>>
    wrote:
    Encoded: {"D":"30s"}
    Nothing but Go is going to parse that. At least
    duration-in-nanoseconds is easy to deal with.

    If you've got your own struct, make a custom type that's compatible
    with time.Duration, and hang MarshalJSON/UnmarshalJSON methods off it.
    There's not really a "compatible" type with go's type checking. There
    are things I can cast to other things and things I can't. There aren't
    things I can use in place of other things. If I want to be able to use my
    struct fields as time.Durations, they have to be time.Duration, which I
    can't advise any new hooks around.

    Fundamentally, I want to be able to store fields from external packages
    in my struct, but have a different way to represent them when marshaling.
    Whether I think String() is a better JSON representation of Duration in
    general is immaterial, I suppose. The workarounds right now include either
    manually copying data in and out of a mirror structure that exists for
    serialization or doing a lot of what happens in the json package myself so
    I can add a field without having to add code elsewhere. I did the latter
    for my project but it's rather a lot of fairly difficult code.

    Is there a way to address this with the JSON parser? e.g. send me errors
    for things that it couldn't parse on an object so that I can only have to
    worry about *those* and know what they were. Then I could at least just
    have to worry about the more complex reflection stuff on marshal.

    --
  • Kyle Lemons at Sep 26, 2012 at 10:23 pm
    Create a Duration type with your own marshal and unmarshal methods; marshal
    returns the quoted string and unmarshal unquotes and parses as a date. I
    had a playground link but closed the tab and can't find it again...
    On Tue, Sep 25, 2012 at 4:41 PM, Dustin wrote:

    On Tuesday, September 25, 2012 4:03:16 PM UTC-7, David Symonds wrote:
    On Wed, Sep 26, 2012 at 8:31 AM, Dustin wrote:

    Encoded: {"D":"30s"}
    Nothing but Go is going to parse that. At least
    duration-in-nanoseconds is easy to deal with.

    If you've got your own struct, make a custom type that's compatible
    with time.Duration, and hang MarshalJSON/UnmarshalJSON methods off it.
    There's not really a "compatible" type with go's type checking. There
    are things I can cast to other things and things I can't. There aren't
    things I can use in place of other things. If I want to be able to use my
    struct fields as time.Durations, they have to be time.Duration, which I
    can't advise any new hooks around.

    Fundamentally, I want to be able to store fields from external packages
    in my struct, but have a different way to represent them when marshaling.
    Whether I think String() is a better JSON representation of Duration in
    general is immaterial, I suppose. The workarounds right now include either
    manually copying data in and out of a mirror structure that exists for
    serialization or doing a lot of what happens in the json package myself so
    I can add a field without having to add code elsewhere. I did the latter
    for my project but it's rather a lot of fairly difficult code.

    Is there a way to address this with the JSON parser? e.g. send me
    errors for things that it couldn't parse on an object so that I can only
    have to worry about *those* and know what they were. Then I could at least
    just have to worry about the more complex reflection stuff on marshal.

    --

    --

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedSep 25, '12 at 4:53p
activeSep 26, '12 at 10:23p
posts6
users3
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase