On Mon, Feb 27, 2017 at 4:41 PM, Rolando Gonzalez Hernandez
<therol...@gmail.com> wrote:
> I've been dealing with a lot of structs to parse and convert XML to JSON and
> viceversa. My main problem with this method is that some of the structures
> have nested structures like this, for example:
>
> type tantu struct {
> ConfidentialAccess struct {
> ConfidentialAccessMode  string `xml:"confidentialAccessMode,"`
> ConfidentialAccessLevel string `xml:"confidentialAccessLevel,"`
> } `xml:"confidentialAccess,omitempty"`
> }
>
> <tantu>
>         <confidentialAccess>
>                 <confidentialAccessMode></confidentialAccessMode>
>                 <confidentialAccessLevel></confidentialAccessLevel>
>         </confidentialAccess>
> </tantu>
>
> In some cases I need that the nested structure doesn't convert to XML
> because it causes errors on the API that I'm working on. I know that this is
> a issue with Go and the XML package. So I was trying to change the tags
> using reflection, but I cannot change the value of the StructTag.
>
> func ChangeTags(t interface{}) {
>         x := "xml:"\-"\"
> val := reflect.ValueOf(t).Elem()
> c := reflect.New(val.Type()).Elem()
> for i := 0; i < val.NumField(); i++ {
> ov := val.Field(i)
> vK := val.Field(i).Kind()
> val.Type().Field(i).Tag = x
> }
>
> Is it possible to change the value, or is it a constraint of Go itself? Are
> there any workarounds for this?

You can not change the tag of a field of an existing struct type.  I'm
not even sure what that would mean.

The closest you can come is to define a new struct type (using
reflect.StructOf) with the same field names and types but different
struct tags.  Then you can use reflect.Value.Convert to convert to
that new type.  That sounds kind of complicated, though; I don't know
whether it makes any sense for your use case.  Perhaps it would make
more sense to define multiple types with different tags in your source
code.

Ian

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

Reply via email to