Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(1798)

Issue 5067044: code review 5067044: xml: Fix inconsistant XMLName behaviour for xml.Marshal... (Closed)

Can't Edit
Can't Publish+Mail
Start Review
Created:
13 years, 5 months ago by chrisfarms
Modified:
13 years, 4 months ago
Reviewers:
CC:
rsc, golang-dev
Visibility:
Public.

Description

xml: Fix inconsistant XMLName behaviour for xml.Marshal/Unmarshal When xml.Marshal is called on a struct it will happily reflect the information in the "tag" of an XMLName member regardless of the type to give the struct a tag-name in it's XML form. This is backed up by the documentation which says: However xml.Unmarshal *does* care about the XMLName field being of type xml.Name, and currently returns the error "field XMLName does not have type xml.Name" if you have it set to something else. This is firstly inconsistant with xml.Marshal but it also makes it impossible to use xml.Marshal alongside other Marshallers (like json/bson) without poluting the state's namespace with XMLName fields. Inorder to exclude fields from other Marshallers the convention has been started to tag fields as "omitempty"; which will cause the field not to display if it is at it's "zero" state, XMLName cannot have such as zero-state since it is a struct, so it is nicer to use a pointer/bool value for XMLName so it can be easily excluded when I want to Marshal my struct by some other wire format. Attached is the proposed minor change, that simply stops erring if it can't set the name on the XMLName field, which is just optional metadata anyway. Fixes issue 2265.

Patch Set 1 #

Patch Set 2 : diff -r b9195358cc18 https://go.googlecode.com/hg/ #

Patch Set 3 : diff -r c793d12e34c9 https://go.googlecode.com/hg/ #

Patch Set 4 : diff -r c793d12e34c9 https://go.googlecode.com/hg/ #

Unified diffs Side-by-side diffs Delta from patch set Stats (+24 lines, -3 lines) Patch
M src/pkg/xml/read.go View 1 1 chunk +2 lines, -3 lines 0 comments Download
M src/pkg/xml/read_test.go View 1 2 3 1 chunk +22 lines, -0 lines 0 comments Download

Messages

Total messages: 9
chrisfarms
Hello golang-dev@googlegroups.com (cc: golang-dev@googlegroups.com), I'd like you to review this change to https://go.googlecode.com/hg/
13 years, 5 months ago (2011-09-20 11:53:39 UTC) #1
kevlar
LGTM I would update the description, though, to be something like: xml: only write to ...
13 years, 5 months ago (2011-09-20 19:04:52 UTC) #2
kevlar
Oh, you might also add a test or two.
13 years, 5 months ago (2011-09-20 19:06:47 UTC) #3
chrisfarms
Hello golang-dev@googlegroups.com, kevlar@google.com (cc: golang-dev@googlegroups.com), Please take another look.
13 years, 5 months ago (2011-10-11 20:55:36 UTC) #4
chrisfarms
sorry didn't add the test file ... *quickly learns process*.. brb
13 years, 5 months ago (2011-10-11 20:57:07 UTC) #5
chrisfarms
Hello golang-dev@googlegroups.com, kevlar@google.com (cc: golang-dev@googlegroups.com), Please take another look.
13 years, 5 months ago (2011-10-11 21:00:23 UTC) #6
nigeltao
Setting rsc as the reviewer.
13 years, 5 months ago (2011-10-13 03:03:42 UTC) #7
rsc
LGTM
13 years, 5 months ago (2011-10-14 21:28:20 UTC) #8
rsc
13 years, 5 months ago (2011-10-14 21:29:57 UTC) #9
*** Submitted as http://code.google.com/p/go/source/detail?r=94bf75f51cbe ***

xml: match Marshal's XMLName behavior in Unmarshal

When xml.Marshal is called on a struct it will happily
reflect the information in the "tag" of an XMLName member
regardless of the type to give the struct a tag-name in
it's XML form. This is backed up by the documentation which
says:

However xml.Unmarshal *does* care about the XMLName field
being of type xml.Name, and currently returns the error
"field XMLName does not have type xml.Name" if you have it
set to something else.

This is firstly inconsistant with xml.Marshal but it also
makes it impossible to use xml.Marshal alongside other
Marshallers (like json/bson) without poluting the state's
namespace with XMLName fields. Inorder to exclude fields
from other Marshallers the convention has been started to
tag fields as "omitempty"; which will cause the field not
to display if it is at it's "zero" state, XMLName cannot
have such as zero-state since it is a struct, so it is nicer
to use a pointer/bool value for XMLName so it can be easily
excluded when I want to Marshal my struct by some other
wire format.

Attached is the proposed minor change, that simply stops
erring if it can't set the name on the XMLName field, which
is just optional metadata anyway.
Fixes issue 2265.

R=rsc
CC=golang-dev
http://codereview.appspot.com/5067044

Committer: Russ Cox <rsc@golang.org>
Sign in to reply to this message.

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b