My idea was to change the struct tag i.e. 
"xml:\"confidentialAccess,omitempty\"" 
to "xml:\"-\"" so that when there is no value so the nested struct the XML 
marshalling process doesn't generate the xml tags.

So in short my idea is to use the ChangeTags function to change the struct 
tag to avoid the xml tag from being created. 

type tantu struct {
ConfidentialAccess struct {
ConfidentialAccessMode  string `xml:"confidentialAccessMode,"`
ConfidentialAccessLevel string `xml:"confidentialAccessLevel,"`
} `xml:"confidentialAccess,omitempty"`
}

*======================* ChangeTags *========================*

type tantu struct {
ConfidentialAccess struct {
ConfidentialAccessMode  string `xml:"confidentialAccessMode,"`
ConfidentialAccessLevel string `xml:"confidentialAccessLevel,"`
} `xml:"-"`
}

*====================* XML Encoding *========================*

<tantu>
</tantu>


The main problem is that I'm working with a SOAP API and it consists of a 
thousand or more operations, each with at least 40 tags. I've been trying 
different approaches to deal with a WSDL and XSD, but I've made very little 
advance because of the several layers of complexity inside both files. 
The alternative, however a little complicated,  is to work the API with 
structs, and validate different cases in which some fields can be "denied" 
to avoid the encoding of empty XML tags. But so far it seems I might need 
to change the approach. 

Warping the question a little, is there any way of achieving my goal using 
structs? 

Thanks for the quick answer!

On Monday, 27 February 2017 18:51:16 UTC-6, Ian Lance Taylor wrote:
>
> On Mon, Feb 27, 2017 at 4:41 PM, Rolando Gonzalez Hernandez 
> <ther...@gmail.com <javascript:>> 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