Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
go
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
go
Commits
ca3e6d13
Commit
ca3e6d13
authored
Jan 19, 2012
by
Gustavo Niemeyer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
encoding/xml: marshal/unmarshal xml.Name in field
R=rsc CC=golang-dev
https://golang.org/cl/5542052
parent
fec7aa95
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
24 additions
and
1 deletion
+24
-1
src/pkg/encoding/xml/marshal_test.go
src/pkg/encoding/xml/marshal_test.go
+17
-0
src/pkg/encoding/xml/read.go
src/pkg/encoding/xml/read.go
+4
-0
src/pkg/encoding/xml/typeinfo.go
src/pkg/encoding/xml/typeinfo.go
+3
-1
No files found.
src/pkg/encoding/xml/marshal_test.go
View file @
ca3e6d13
...
...
@@ -150,6 +150,10 @@ type XMLNameWithoutTag struct {
Value
string
",chardata"
}
type
NameInField
struct
{
Foo
Name
`xml:"ns foo"`
}
type
AttrTest
struct
{
Int
int
`xml:",attr"`
Lower
int
`xml:"int,attr"`
...
...
@@ -483,6 +487,19 @@ var marshalTests = []struct {
UnmarshalOnly
:
true
,
},
// xml.Name works in a plain field as well.
{
Value
:
&
NameInField
{
Name
{
Space
:
"ns"
,
Local
:
"foo"
}},
ExpectXML
:
`<NameInField><foo xmlns="ns"></foo></NameInField>`
,
},
// Marshaling zero xml.Name uses the tag or field name.
{
Value
:
&
NameInField
{},
ExpectXML
:
`<NameInField><foo xmlns="ns"></foo></NameInField>`
,
MarshalOnly
:
true
,
},
// Test attributes
{
Value
:
&
AttrTest
{
...
...
src/pkg/encoding/xml/read.go
View file @
ca3e6d13
...
...
@@ -271,6 +271,10 @@ func (p *Parser) unmarshal(val reflect.Value, start *StartElement) error {
case
reflect
.
Struct
:
sv
=
v
typ
:=
sv
.
Type
()
if
typ
==
nameType
{
v
.
Set
(
reflect
.
ValueOf
(
start
.
Name
))
break
}
tinfo
,
err
=
getTypeInfo
(
typ
)
if
err
!=
nil
{
return
err
...
...
src/pkg/encoding/xml/typeinfo.go
View file @
ca3e6d13
...
...
@@ -46,6 +46,8 @@ const (
var
tinfoMap
=
make
(
map
[
reflect
.
Type
]
*
typeInfo
)
var
tinfoLock
sync
.
RWMutex
var
nameType
=
reflect
.
TypeOf
(
Name
{})
// getTypeInfo returns the typeInfo structure with details necessary
// for marshalling and unmarshalling typ.
func
getTypeInfo
(
typ
reflect
.
Type
)
(
*
typeInfo
,
error
)
{
...
...
@@ -56,7 +58,7 @@ func getTypeInfo(typ reflect.Type) (*typeInfo, error) {
return
tinfo
,
nil
}
tinfo
=
&
typeInfo
{}
if
typ
.
Kind
()
==
reflect
.
Struct
{
if
typ
.
Kind
()
==
reflect
.
Struct
&&
typ
!=
nameType
{
n
:=
typ
.
NumField
()
for
i
:=
0
;
i
<
n
;
i
++
{
f
:=
typ
.
Field
(
i
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment