Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

encoding/xml: field tag cleanup #2426

Closed
rsc opened this Issue Nov 7, 2011 · 6 comments

Comments

Projects
None yet
5 participants
@rsc
Copy link
Contributor

rsc commented Nov 7, 2011

The field tags in package xml were one of the earliest uses of tags
and need to be cleaned up, like json was cleaned up.

Specifically, the field lookup should look for an identical match,
not a case-insensitive or 'strip the invalid characters' match.
Any custom matching should be done with field tags, not heuristics.
The field tags should use attributes like json does instead of 
overloading certain names.  For example it is impossible today
to unmarshal an XML tag <attr> because the "attr" tag means
'make this field an attribute'.  It is also impossible to choose a
name for that attribute.

Specific changes needed:
   * Fields match only if the field name matches the element name
     exactly (for field with no tag name) or matches the tag name
     exactly.
   * Field with "-" tag is ignored completely.
   * Attributes are marked by ",attr" or "x,attr" tag, not just "attr".
@rogpeppe

This comment has been minimized.

Copy link
Contributor

rogpeppe commented Nov 15, 2011

Comment 1:

Tags aren't symmetrical either. The following code panics:
package main
import (
    "fmt"
    "encoding/xml"
    "bytes"
)
type Foo struct {
    XMLName   xml.Name `xml:"foo"`
    X int  `xml:"y"`
}
func main() {
    var b bytes.Buffer
    xml.Marshal(&b, &Foo{
        X: 56,
    })
    var x Foo
    err := xml.Unmarshal(&b, &x)
    if err != nil {
        fmt.Printf("error: %v\n", err)
        return
    }
    if x.X != 56 {
        panic("unexpected unmarshalled value")
    }
}
@nsf

This comment has been minimized.

Copy link

nsf commented Nov 19, 2011

Comment 2:

I think I'll just put it here. A small feature request for future "xml" package versions.
I had a need to filter xml elements by their namespace, consider this example:
<package>
  <include name="GObject"/>
  <include name="Gtk"/>
  <c:include name="glib-object.h"/>
  <c:include name="gtk/gtk.h"/>
</package>
So, I would like to unmarshal that into a struct with two arrays for each namespace:
type Package struct {
  Includes []Include
  CIncludes []Include
}
And currently such thing is impossible as far as I can see. 
Also attributes may have namespaces as well. Parser understands that, but there is no
way to use attribute namespaces with struct tags. E.g.:
<method name="get_comments" c:identifier="gtk_about_dialog_get_comments"
version="2.6">
  ...
</method>
Maybe though attributes cannot have matching local names in xml, I don't know.
P.S. It's a shame that there is so much real world data in this perverted format.
@rsc

This comment has been minimized.

Copy link
Contributor Author

rsc commented Dec 9, 2011

Comment 3:

Labels changed: added priority-later, removed priority-medium.

@rsc

This comment has been minimized.

Copy link
Contributor Author

rsc commented Dec 12, 2011

Comment 4:

Labels changed: added priority-go1.

@niemeyer

This comment has been minimized.

Copy link
Contributor

niemeyer commented Dec 29, 2011

Comment 5:

Owner changed to @niemeyer.

Status changed to Started.

@niemeyer

This comment has been minimized.

Copy link
Contributor

niemeyer commented Jan 13, 2012

Comment 6:

This issue was closed by revision 1627b46.

Status changed to Fixed.

@rsc rsc added fixed labels Jan 13, 2012

@mikioh mikioh changed the title xml: field tag cleanup encoding/xml: field tag cleanup Jan 9, 2015

@rsc rsc added this to the Go1 milestone Apr 10, 2015

@rsc rsc removed the priority-go1 label Apr 10, 2015

@golang golang locked and limited conversation to collaborators Jun 24, 2016

This issue was closed.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.