Re: [go-nuts] Re: marshal multiple json documents which may have different format versions into the latest struct version

2020-05-01 Thread Tom Payne
Possibly also useful if you're dealing with JSON documents with different 
structures:
  https://github.com/twpayne/go-jsonstruct
This will generate the most specific Go struct possible that covers all the 
example documents that you give it. In your example it will generate:

package main

type T struct {
Cities   []string `json:"Cities,omitempty"`
Version  string   `json:"Version"`
Villages []string `json:"Villages,omitempty"`
}

which is the union of all versions.

If you already know exactly what structures you will receive, then Manilo's 
approach of first decoding into a struct only the Version field is better 
though, especially when combined with Go's runtime type switches.

Regards,
Tom

On Wednesday, April 29, 2020 at 9:31:00 AM UTC+1, Chris Burkert wrote:
>
> That sounds like a good plan. I'm going to try that. Thank you Manlio!
>
> Am Di., 28. Apr. 2020 um 15:11 Uhr schrieb Manlio Perillo <
> manlio...@gmail.com >:
>
>> On Tuesday, April 28, 2020 at 10:52:56 AM UTC+2, Chris Burkert wrote:
>>>
>>> Dear all,
>>>
>>> my application users shall be able to provide multiple json documents 
>>> (files and urls) which I'd like to marshall into one structure. 
>>> Additionally these json documents may have different versions. I know how 
>>> to marshal a document into a version specific struct if I know the format 
>>> version before (for simplicity of this example I don't handle errors): 
>>> https://play.golang.org/p/ixVI5CzPqFP
>>>
>>> What I would like (in the example the village field was renamed to 
>>> cities ) is a struct of type ModelV2 with all four values merged in Cities.
>>>
>>> Is there a best practice for a backwards compatible behavior which:
>>>
>>>- identifies the json format version of each document
>>>- skips that document if it is higher than the supported format 
>>>version in my application
>>>- merges supported format versions into ONE struct
>>>
>>> Of course I have to implement the semantics on my own but how can I 
>>> approach the topic?
>>>
>>>
>> You can first unmarshal a struct containing only the Version field.  As 
>> an example:
>> https://play.golang.org/p/1oDzdWlTCfC 
>> 
>>
>>
>> Manlio 
>>
>>> thanks
>>>
>> -- 
>> 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 golan...@googlegroups.com .
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/golang-nuts/274e489d-afdb-4ec9-a5b3-26440364c489%40googlegroups.com
>>  
>> 
>> .
>>
>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/d035d66f-082b-41a5-9c31-f65ffe766515%40googlegroups.com.


Re: [go-nuts] Re: marshal multiple json documents which may have different format versions into the latest struct version

2020-04-29 Thread Chris Burkert
That sounds like a good plan. I'm going to try that. Thank you Manlio!

Am Di., 28. Apr. 2020 um 15:11 Uhr schrieb Manlio Perillo <
manlio.peri...@gmail.com>:

> On Tuesday, April 28, 2020 at 10:52:56 AM UTC+2, Chris Burkert wrote:
>>
>> Dear all,
>>
>> my application users shall be able to provide multiple json documents
>> (files and urls) which I'd like to marshall into one structure.
>> Additionally these json documents may have different versions. I know how
>> to marshal a document into a version specific struct if I know the format
>> version before (for simplicity of this example I don't handle errors):
>> https://play.golang.org/p/ixVI5CzPqFP
>>
>> What I would like (in the example the village field was renamed to cities
>> ) is a struct of type ModelV2 with all four values merged in Cities.
>>
>> Is there a best practice for a backwards compatible behavior which:
>>
>>- identifies the json format version of each document
>>- skips that document if it is higher than the supported format
>>version in my application
>>- merges supported format versions into ONE struct
>>
>> Of course I have to implement the semantics on my own but how can I
>> approach the topic?
>>
>>
> You can first unmarshal a struct containing only the Version field.  As an
> example:
> https://play.golang.org/p/1oDzdWlTCfC
> 
>
>
> Manlio
>
>> thanks
>>
> --
> 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+unsubscr...@googlegroups.com.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/golang-nuts/274e489d-afdb-4ec9-a5b3-26440364c489%40googlegroups.com
> 
> .
>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/CALWqRZomuBCB_vV84WA%3DHnsRS%2BfzYOA%2B_2hLyGQAA1BU4VfXtg%40mail.gmail.com.


[go-nuts] Re: marshal multiple json documents which may have different format versions into the latest struct version

2020-04-28 Thread Manlio Perillo
On Tuesday, April 28, 2020 at 10:52:56 AM UTC+2, Chris Burkert wrote:
>
> Dear all,
>
> my application users shall be able to provide multiple json documents 
> (files and urls) which I'd like to marshall into one structure. 
> Additionally these json documents may have different versions. I know how 
> to marshal a document into a version specific struct if I know the format 
> version before (for simplicity of this example I don't handle errors): 
> https://play.golang.org/p/ixVI5CzPqFP
>
> What I would like (in the example the village field was renamed to cities 
> ) is a struct of type ModelV2 with all four values merged in Cities.
>
> Is there a best practice for a backwards compatible behavior which:
>
>- identifies the json format version of each document
>- skips that document if it is higher than the supported format 
>version in my application
>- merges supported format versions into ONE struct
>
> Of course I have to implement the semantics on my own but how can I 
> approach the topic?
>
>
You can first unmarshal a struct containing only the Version field.  As an 
example:
https://play.golang.org/p/1oDzdWlTCfC 



Manlio 

> thanks
>

-- 
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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/274e489d-afdb-4ec9-a5b3-26440364c489%40googlegroups.com.