Dear golang-nuts,

The language change mentioned in Francesc Campoy's "The State of Go" 2017 
talk caught my eye. Converting struct types while ignoring tags struck me 
as being very useful. However, it turns out that tags are not ignored 
recursively:

https://github.com/golang/go/issues/16085

All of the structs with which I would like to make use of this feature 
contain nested structs. I imagine this is quite common with structs used 
for JSON encoding or those generated from protocol buffers. I was hoping to 
be able to do something like:

package main

import (
    "encoding/json"
    "log"
)

type DBPerson struct {
    Name    string
    Address *DBAddress
}

type DBAddress struct {
    Number int
}

type JSONPerson struct {
    Name    string       `json:"name"`
    Address *JSONAddress `json:"address"`
}

type JSONAddress struct {
    Number int `json:"number"`
}

func main() {
    p0 := DBPerson{
        Name:    "Alice",
        Address: &DBAddress{42},
    }

    p1 := JSONPerson(p0)

    data, err := json.Marshal(p1)
    if err != nil {
        log.Fatal(err)
    }
    log.Printf("%s", data)
}

However, this is not possible since the address fields have different 
types. Of course, it is possible to use the new feature with the nested 
struct:

    address := JSONAddress(*p0.Address)
    p1 := JSONPerson{
        Name:    p0.Name,
        Address: &address,
    }

Is there any chance of conversions being done recursively or would this 
represent to large a change to the spec?

Yours sincerely,
Richard Lincoln

-- 
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 [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to